码迷,mamicode.com
首页 > 其他好文 > 详细

画图板

时间:2016-08-24 17:13:16      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:

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("离开");
}

 

}

 

画图板

标签:

原文地址:http://www.cnblogs.com/zjc-66/p/5803464.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!