Java web start contains an example,download it from java.sun.com

解决方案 »

  1.   

    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");
       }
    }