home *** CD-ROM | disk | FTP | other *** search
/ Java by Example / jbecd.bin / JBE-CD / NTUsers / JBECODE.ZIP / JavaByExample / chap25 / MouseApplet3.java < prev    next >
Text File  |  1996-03-11  |  1KB  |  55 lines

  1. import java.awt.*;
  2. import java.applet.*;
  3.  
  4. public class MouseApplet3 extends Applet
  5. {
  6.     Point startPoint;
  7.     Point points[];
  8.     int numPoints;
  9.     boolean drawing;
  10.  
  11.     public void init()
  12.     {
  13.         startPoint = new Point(0, 0);
  14.         points = new Point[1000];
  15.         numPoints = 0;
  16.         drawing = false;
  17.         resize(400, 300);
  18.     }
  19.  
  20.     public void paint(Graphics g)
  21.     {
  22.         int oldX = startPoint.x;
  23.         int oldY = startPoint.y;
  24.  
  25.         for (int x=0; x<numPoints; ++x)
  26.         {
  27.             g.drawLine(oldX, oldY, points[x].x, points[x].y);
  28.             oldX = points[x].x;
  29.             oldY = points[x].y;
  30.         }
  31.     }
  32.  
  33.     public boolean handleEvent(Event evt)
  34.     {
  35.         switch(evt.id)
  36.         {
  37.             case Event.MOUSE_DOWN:
  38.                 drawing = true;
  39.                 startPoint.x = evt.x;
  40.                 startPoint.y = evt.y;
  41.                 return true;
  42.             case Event.MOUSE_MOVE:
  43.                 if ((drawing) && (numPoints < 1000))
  44.                 {
  45.                     points[numPoints] = new Point(evt.x, evt.y);
  46.                     ++numPoints;
  47.                     repaint();
  48.                 }
  49.                 return true;
  50.             default:
  51.                 return false;
  52.         }
  53.     }
  54. }
  55.