package drawLine;
/**
* @author FlyingFire
* @date:2011-6-1 下午04:44:47
* @summary:画板窗口最小化以后,所有的直线全部消失
*/
public class DrawUI extends javax.swing.JFrame{
java.awt.Graphics g;
UIListener listener;
public void showUI(){
this.setTitle("简易画板");
this.setSize(600,500);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(3);
this.setVisible(true);
g=this.getGraphics();
listener=new UIListener(g);
this.addMouseListener(listener);
}
public void paint(){
System.out.println("listener");
for(int i=0;i<listener.list.size();i++){
g.drawLine(listener.list.get(i).x1,listener.list.get(i).y1,listener.list.get(i).x2,listener.list.get(i).y2);
}
}
public static void main(String a[]){
DrawUI dui=new DrawUI();
dui.showUI();
dui.paint();
}
}
*********************************************************
package drawLine;
public class LinePointers {
public int x1;
public int y1;
public int x2;
public int y2;
public LinePointers(int x1,int y1,int x2,int y2){
this.x1=x1;
this.y1=y1;
this.x2=x2;
this.y2=y2;
}
}
****************************************************
package drawLine;
import java.awt.event.MouseEvent;
public class UIListener extends java.awt.event.MouseAdapter{
java.util.ArrayList<LinePointers> list=new java.util.ArrayList<LinePointers>();
private java.awt.Graphics g;
private int x1,y1,x2,y2;
private int countClick=1;
public UIListener(java.awt.Graphics g){
this.g=g;
}
public void mouseClicked(MouseEvent e) {
System.out.println("监听了");
if(countClick==1){
x1=e.getX();
y1=e.getY();
countClick++;
System.out.println(x1+"x1------y1"+y1);
}
else{
x2=e.getX();
y2=e.getY();
System.out.println(x2+"x2------y2"+y2);
g.drawLine(x1, y1, x2, y2);
countClick=1;
LinePointers twoPoints=new LinePointers(x1,y1,x2,y2);
list.add(twoPoints);
}
}
}