点击鼠标画圆(Java)

以JFrame作为画布整个视窗内的范围均为绘图区的范围,以JPanel为画布,视窗内只有某一区域可作为画布。

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JFrame;


public class Can extends JFrame implements MouseListener{
	
	public static void main(String[] args) {
		Can can = new Can();
	}
	
	public Can(){
		
		addMouseListener(this);
		setSize(500,300);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
		
	}
	
	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	int x,y;
	
	
	public void paint(Graphics g){
		super.paint(g);
		g.setColor(Color.red);
		g.drawOval(this.x, this.y, 20, 20);
	}
	
	@Override
	public void mouseClicked(MouseEvent e) {
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		x = e.getX();
		y = e.getY();
		setX(x);
		setY(y);
		repaint();
	}
	

	@Override
	public void mouseReleased(MouseEvent e) {
		
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		
	}

}


你可能感兴趣的:(个人用软件)