java例程练习(简单的画图程序[鼠标事件处理])
//简单的画图程序
//注意repaint()方法的使用
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class TestMyMouseAdater {
public static void main(String[] args) {
new MyFrame("Drawing ......");
}
}
class MyFrame extends Frame {
ArrayList<Point> points = null;
MyFrame(String s) {
super(s);
points = new ArrayList<Point>();
setLayout(null);
setBounds(300, 300, 400, 400);
setBackground(new Color(204, 204, 255));
setVisible(true);
this.addMouseListener(new Monitor());
}
public void paint(Graphics g) {
Iterator<Point> i = points.iterator();
while(i.hasNext()) {
Point p = i.next();
g.setColor(Color.blue);
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 f = (MyFrame)e.getSource();
f.addPoint(new Point(e.getX(), e.getY()));
f.repaint();
}
}
摘自 Yours风之恋
补充:软件开发 , Java ,