求用鼠标绘图代码 Java web start contains an example,download it from java.sun.com 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 import java.awt.*;import java.awt.event.*;import java.util.*;public class Test extends Frame{ public static void main(String[] args) { new Test(); } public Test() { this.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){dispose();System.exit(0);}}); this.setSize(300,300); this.setLocation(300,0); this.add(new Draw()); this.show(); } protected void processWindowEvent(WindowEvent event) { super.processWindowEvent(event); if(event.getID() == WindowEvent.WINDOW_CLOSING) { System.exit(0); } }}class Draw extends Canvas{ int x1=-1; int y1=-1; int x2=-1; int y2=-1; public Draw() { this.setBackground(Color.white); this.addMouseListener(new MyMouseListener(this)); this.addMouseMotionListener(new MyMouseMotionListener(this)); } public void paint(Graphics g) { if(x1==-1 || y1==-1) { x1=x2; y1=y2; } g.drawLine(x1,y1,x2,y2); x1=x2; y1=y2; } public void update(Graphics g) { this.paint(g); }}class MyMouseListener implements MouseListener{ Draw draw; public MyMouseListener(Draw draw) { this.draw=draw; } public void mouseClicked(MouseEvent e) { //System.out.println("mouseClicked"); } public void mouseEntered(MouseEvent e) { //System.out.println("mouseEntered"); } public void mouseExited(MouseEvent e) { //System.out.println("mouseExited"); } public void mousePressed(MouseEvent e) { //System.out.println("mousePressed"); draw.x1=-1; draw.y1=-1; } public void mouseReleased(MouseEvent e) { //System.out.println("mouseReleased"); draw.x1=draw.x2; draw.y1=draw.y2; }}class MyMouseMotionListener implements MouseMotionListener{ Draw draw; public MyMouseMotionListener(Draw draw) { this.draw=draw; } public void mouseDragged(MouseEvent e) { //System.out.println("mouseDragged"); draw.x2=e.getX(); draw.y2=e.getY(); draw.repaint(); } public void mouseMoved(MouseEvent e) { //System.out.println("mouseMoved"); }} 论WEB之消亡,B/S之覆灭,错误的开发方向 几道笔试题大家瞧一瞧,看一看,做一做!!! blowfish解密密钥长度修改 30分求专业的画图程序界面 javac找不到文件(能够运行文件,但不能编译源文件) JSpinner的输入事件怎么捕捉? 魔方问题的Java实现,求完整程序。 udp 传输 为什么我的服务器程序只能在第一次运行时收到信息 我刚学java请教一个socket的问题 请进!有分! java中如何生成树形控件? 如何实现文件的拷贝与剪切?
import java.awt.event.*;
import java.util.*;public class Test extends Frame
{
public static void main(String[] args)
{
new Test();
}
public Test()
{
this.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){dispose();System.exit(0);}});
this.setSize(300,300);
this.setLocation(300,0);
this.add(new Draw());
this.show();
}
protected void processWindowEvent(WindowEvent event)
{
super.processWindowEvent(event);
if(event.getID() == WindowEvent.WINDOW_CLOSING)
{
System.exit(0);
}
}
}
class Draw extends Canvas
{
int x1=-1;
int y1=-1;
int x2=-1;
int y2=-1;
public Draw()
{
this.setBackground(Color.white);
this.addMouseListener(new MyMouseListener(this));
this.addMouseMotionListener(new MyMouseMotionListener(this));
}
public void paint(Graphics g)
{
if(x1==-1 || y1==-1)
{
x1=x2;
y1=y2;
}
g.drawLine(x1,y1,x2,y2);
x1=x2;
y1=y2;
}
public void update(Graphics g)
{
this.paint(g);
}
}
class MyMouseListener implements MouseListener
{
Draw draw;
public MyMouseListener(Draw draw)
{
this.draw=draw;
}
public void mouseClicked(MouseEvent e)
{
//System.out.println("mouseClicked");
}
public void mouseEntered(MouseEvent e)
{
//System.out.println("mouseEntered");
}
public void mouseExited(MouseEvent e)
{
//System.out.println("mouseExited");
}
public void mousePressed(MouseEvent e)
{
//System.out.println("mousePressed");
draw.x1=-1;
draw.y1=-1;
}
public void mouseReleased(MouseEvent e)
{
//System.out.println("mouseReleased");
draw.x1=draw.x2;
draw.y1=draw.y2;
}
}
class MyMouseMotionListener implements MouseMotionListener
{
Draw draw;
public MyMouseMotionListener(Draw draw)
{
this.draw=draw;
}
public void mouseDragged(MouseEvent e)
{
//System.out.println("mouseDragged");
draw.x2=e.getX();
draw.y2=e.getY();
draw.repaint();
}
public void mouseMoved(MouseEvent e)
{
//System.out.println("mouseMoved");
}
}