熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

實現鼠標畫圖的Java程序

2022-06-13   來源: Java核心技術 

  

  import javaawtColor;

  import javaawtFrame;

  import javaawtGraphics;

  import javaawtPoint;

  import javaawteventMouseAdapter;

  import javaawteventMouseEvent;

  import javaawteventMouseListener;

  import javautilArrayList;

  import javautilIterator;

  public class MyMouseAdapter {

  public static void main(String[] args) {

  new MyFrame(drawing);

  }

  }

  class MyFrame extends Frame{

  ArrayList points=null;

  MyFrame(String s){

  super(s);

  points=new ArrayList();

  setLayout(null);

  setBounds();

  thissetBackground(new Color());

  setVisible(true);

  thisaddMouseListener(new Monitor());

  }

  public void paint(Graphics g){

  Iterator i=erator();

  while(ihasNext()){

  Point p=(Point)inext();

  gsetColor(Colorblue);

  gfillOval(pxpy );

  }

  }

  public void addPoint(Point p){

  pointsadd(p);

  }

  }

  class Monitor extends MouseAdapter{ //MouseAdapter實現了MouseListener接口

  public void mousePressed(MouseEvent e){

  MyFrame f=(MyFrame)egetSource();

  faddPoint(new Point(egetX()egetY()));

  frepaint(); //讓Frame強制經行重畫

  }

  }


From:http://tw.wingwit.com/Article/program/Java/hx/201311/26376.html
  • 上一篇文章:

  • 下一篇文章:
  • 推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.