java语言gui编程之内部类和鼠标事件经典2之两个问题的解决

/*
 时间:2015年3月21日20:06:23
 程序目的:对于上一个程序中出现的两个问题
  注: TestMouseMotion.java使用了未经检查或不安全的操作。
  注: 有关详细信息, 请使用 -Xlint:unchecked 重新编译。
 和
  需要完整的罗列MouseMotionListener中所有方法,
  该程序做出调整。
  加入泛型之后,不需要强制转换了。
  使用适配器MoseAdapter,不需要完整罗列所有方法,这是因为在MouseAdapter类中,已经为我们提供
  所有方法的空的实现。所以我们需要做的只是需要重写覆盖其中某个我们所需要的方法就行了
  
*/
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class TestMouseMotionGenric {
 public static void main(String[] args) {
  new MyFrame("Drawing...");
 }
}



class MyFrame extends Frame {
 ArrayList<Point> points = null;
 public MyFrame(String title) {
  super(title);
  points = new ArrayList<Point>();
  setLayout(null);
  setBounds(300, 300, 500, 600);
  addMouseMotionListener(new MyAdapter(this));
  setVisible(true);
 }
 
 public void paint(Graphics g) {
  Iterator<Point> it = points.iterator();
  
  while (it.hasNext()) {
   
   //Point p = (Point)it.next();
   
   Point p = it.next();
   Color c = g.getColor();
   g.setColor(Color.PINK);
   g.fillOval(p.x, p.y, 5, 5);
   g.setColor(c);
  }
 }
 
 public void addPoint(Point p) {
  points.add(p);
 }
}

class MyAdapter extends MouseMotionAdapter {
 private int num = 0;
 private MyFrame mf = null;
 
 public MyAdapter(MyFrame mf) {
  this.mf = mf;
 }
  
 public void mouseMoved(MouseEvent e) {
  mf.addPoint(e.getPoint());
  if (num++ > 5) {
   mf.repaint(); 
  }
 }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值