import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class TestMyMouseAdapter {
public static void main(String[] args) {
new MyFrame("Drawing....");
}
}
class MyFrame extends Frame{
ArrayList points = null;
MyFrame(String str){
super(str);
points = new ArrayList();
super.setLayout(null);
super.setBounds(200, 140, 640, 480);
this.setBackground(new Color(200, 220,110));
super.setVisible(true);
super.addMouseListener(new Monitor());
this.addWindowListener(new WindowAdapter(){//匿名类
public void windowClosing(WindowEvent e){
setVisible(false);
System.exit(-1);
}
});
}
public void paint(Graphics g){
Iterator it = points.iterator();//Arraylist中的迭代器
while(it.hasNext()){
Point p = (Point)it.next();
g.setColor(new Color(200,50,100));
g.fillOval(p.x, p.y, 10, 10);
}
}
public void addPoint(Point p){
points.add(p);
}
}
class Monitor extends MouseAdapter{//响应鼠标事件
public void mousePressed(MouseEvent e){
MyFrame mf = (MyFrame)e.getSource();
mf.addPoint(new Point(e.getX(), e.getY()));
mf.repaint();
}
}
匿名类,迭代器,WindowAdapter,Point,MouseAdapter,paint
最新推荐文章于 2021-08-15 10:49:04 发布