- 相關(guān)推薦
實(shí)現(xiàn)鼠標(biāo)畫圖的Java程序
用鼠標(biāo)畫圖有很多樂趣和便利之處,那么如何才能實(shí)現(xiàn)鼠標(biāo)畫圖呢?下面YJBYS小編為大家整理了關(guān)于實(shí)現(xiàn)鼠標(biāo)畫圖的Java程序,希望對你有所幫助。
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
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 MyFrame5(“drawing”);
class MyFrame5 extends Frame{
ArrayList points=null;
MyFrame5(String s){
super(s);
points=new ArrayList();
setLayout(null);
setBounds(300,300,400,300);
this.setBackground(new Color(204,204,255));
setVisible(true);
this.addMouseListener(new Monitor5());
}
public void paint(Graphics g){
Iterator i=points.iterator();
while(i.hasNext()){
Point p=(Point)i.next();
g.setColor(Color.blue);
g.fillOval(p.x,p.y, 10, 10);
}
}
public void addPoint(Point p){
points.add(p);
}
}
class Monitor5 extends MouseAdapter{ //MouseAdapter實(shí)現(xiàn)了MouseListener接口
public void mousePressed(MouseEvent e){
MyFrame5 f=(MyFrame5)e.getSource();
f.addPoint(new Point(e.getX(),e.getY()));
f.repaint(); //讓Frame強(qiáng)制經(jīng)行重畫
}
}
【實(shí)現(xiàn)鼠標(biāo)畫圖的Java程序】相關(guān)文章:
關(guān)于JAVA實(shí)現(xiàn)httpClient的實(shí)例11-05
使用JavaScript實(shí)現(xiàn)Java的List功能08-09
Java如何實(shí)現(xiàn)點(diǎn)的在線添加08-31
java編寫即時聊天程序06-12
java實(shí)現(xiàn)web服務(wù)器的方法09-26
sun認(rèn)證java程序員08-05
sun認(rèn)證java程序員須知Java日志框架09-05
Java怎么編寫走馬燈程序10-07