home *** CD-ROM | disk | FTP | other *** search
/ Learn Java Now / Learn_Java_Now_Microsoft_1996.iso / JavaNow / Code / Chap13 / ConnectTheDots2 / ConnectTheDots.class (.txt) next >
Encoding:
Java Class File  |  1996-07-29  |  2.4 KB  |  72 lines

  1. import java.applet.Applet;
  2. import java.awt.Component;
  3. import java.awt.Dimension;
  4. import java.awt.Event;
  5. import java.awt.Graphics;
  6. import java.util.Vector;
  7.  
  8. public class ConnectTheDots extends Applet {
  9.    Vector m_vLocs = new Vector();
  10.    Dimension m_dimCursorLoc = new Dimension(0, 0);
  11.  
  12.    public void start() {
  13.    }
  14.  
  15.    public void stop() {
  16.    }
  17.  
  18.    public boolean mouseMove(Event evt, int x, int y) {
  19.       this.m_dimCursorLoc = new Dimension(x, y);
  20.       ((Component)this).repaint();
  21.       return true;
  22.    }
  23.  
  24.    public boolean mouseDown(Event evt, int x, int y) {
  25.       if (evt.clickCount > 1) {
  26.          this.m_vLocs.removeAllElements();
  27.       } else {
  28.          this.m_vLocs.addElement(new Dimension(x, y));
  29.       }
  30.  
  31.       ((Component)this).repaint();
  32.       return true;
  33.    }
  34.  
  35.    public boolean mouseDrag(Event evt, int x, int y) {
  36.       return true;
  37.    }
  38.  
  39.    public String getAppletInfo() {
  40.       return "Name: ConnectTheDots\r\n" + "Author: Stephen R. Davis\r\n" + "Created for Learn Java Now (c)";
  41.    }
  42.  
  43.    public boolean mouseUp(Event evt, int x, int y) {
  44.       return true;
  45.    }
  46.  
  47.    public void destroy() {
  48.    }
  49.  
  50.    public void init() {
  51.       ((Applet)this).resize(400, 400);
  52.    }
  53.  
  54.    public void paint(Graphics g) {
  55.       int nX = this.m_dimCursorLoc.width;
  56.       int nY = this.m_dimCursorLoc.height;
  57.       g.drawLine(nX - 2, nY, nX + 2, nY);
  58.       g.drawLine(nX, nY - 2, nX, nY + 2);
  59.       int nSize = this.m_vLocs.size();
  60.  
  61.       for(int i = 0; i < nSize - 1; ++i) {
  62.          Dimension dimFrom = (Dimension)this.m_vLocs.elementAt(i);
  63.  
  64.          for(int j = i + 1; j < nSize; ++j) {
  65.             Dimension dimTo = (Dimension)this.m_vLocs.elementAt(j);
  66.             g.drawLine(dimFrom.width, dimFrom.height, dimTo.width, dimTo.height);
  67.          }
  68.       }
  69.  
  70.    }
  71. }
  72.