import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Paint;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Iterator;
public class MyMouseAdapter {
public static void main(String[] args) {
new MyFrame("富奥斯本");
}
}
class MyFrame extends Frame{
ArrayList<Point> points=null;
public MyFrame(String s) {
// TODO Auto-generated constructor stub
super(s);//标题栏的名字
points=new ArrayList<Point>();//新建一个ArrayList存储点用
setLayout(null);//布局格式设为空
setBounds(100,100,100,100);//位置与大小
this.setBackground(new Color(204,205,255));//背景颜色
this.addMouseListener((MouseListener) new Monitor());
setVisible(true);//是否可见
}
@Override
public void paint(Graphics g) {
// TODO Auto-generated method stub
Iterator<Point> integer=points.iterator();
while(integer.hasNext()) {
Point point=(Point)integer.next();
g.setColor(Color.blue);
g.fillOval(point.x, point.y, 5, 5);
}
}
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();
}
}
鼠标事件适配器
最新推荐文章于 2021-09-16 12:34:41 发布