画图板
1、画图工具:
Graphics
需要在哪个组件上绘图,哪个组件就获取Graphics
例如:窗体组件获取Graphics
//添加监听器:
DrawListener DrawL=new DrawListener();
frame.addMouseListener(DrawL);
frame.setVisible(true);
//以下部分要写在Visible下面
Graphics g=frame.getGraphics();
DrawL.setG(g);
System.out.println("g");
2、创建监听器:
public class DrawListener implements MouseListener{
	
	int x1,x2,y1,y2;
	Graphics g;
	
	public void setG(Graphics g){
		this.g=g;
	}
	 public void mouseClicked(MouseEvent e){
		 System.out.println("点击");
	 }
public void mousePressed(MouseEvent e){
	    	x1=e.getX();
	    	y1=e.getY();
	    	
public void mouseReleased(MouseEvent e){
 x2=e.getX();
	    	y2=e.getY();
	    	
g.setColor(Color.blue);
	    	g.fillRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2-x1),Math.abs(y2-y1));       坐标:(0,0)向右x增大,向下y增大
	    	g.setColor(Color.YELLOW);
	    	g.fillOval(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2-x1), Math.abs(y2-y1));
	    	g.setColor(Color.red);
	    	g.drawLine(x1, y1, x2, y2);
	    	g.drawLine(x1, y2, x2, y1);
	    	g.drawLine((x1+x2)/2, y1, (x1+x2)/2, y2);
	    	g.drawLine(x1, (y1+y2)/2, x2,(y1+y2)/2 );
}
	    public void mouseEntered(MouseEvent e){
	    	System.out.println("进入");
	    }
	    public void mouseExited(MouseEvent e){
	    	System.out.println("离开");
	    }
	
}
                    
                
                
            
        
浙公网安备 33010602011771号