home *** CD-ROM | disk | FTP | other *** search
/ Learn Java Now / Learn_Java_Now_Microsoft_1996.iso / JavaNow / Code / Chap13 / ConnectTheDots2 / ConnectTheDots.java < prev    next >
Encoding:
Java Source  |  1996-06-17  |  2.6 KB  |  114 lines

  1. //**************************************************************
  2. // ConnectTheDots.java:    Applet
  3. //
  4. //**************************************************************
  5. import java.applet.*;
  6. import java.awt.*;
  7. import java.util.Vector;
  8.  
  9. public class ConnectTheDots extends Applet
  10. {
  11.     // the following is a recording of all
  12.     // previous clicked locations
  13.     Vector m_vLocs;
  14.  
  15.     // the current cursor location
  16.     Dimension m_dimCursorLoc;
  17.  
  18.     public ConnectTheDots()
  19.     {
  20.         m_vLocs = new Vector();
  21.         m_dimCursorLoc = new Dimension(0, 0);
  22.     }
  23.  
  24.     public String getAppletInfo()
  25.     {
  26.         return "Name: ConnectTheDots\r\n" +
  27.                "Author: Stephen R. Davis\r\n" +
  28.                "Created for Learn Java Now (c)";
  29.     }
  30.  
  31.  
  32.     public void init()
  33.     {
  34.         resize(400, 400);
  35.  
  36.     }
  37.  
  38.     public void destroy()
  39.     {
  40.     }
  41.  
  42.     public void paint(Graphics g)
  43.     {
  44.         // put a cross where the cursor is located
  45.         int nX = m_dimCursorLoc.width;
  46.         int nY = m_dimCursorLoc.height;
  47.         g.drawLine(nX - 2,     nY, nX + 2,     nY);
  48.         g.drawLine(    nX, nY - 2,     nX, nY + 2);
  49.  
  50.         // now draw a line from each clicked location
  51.         // to every other clicked location
  52.         Dimension dimFrom;
  53.         Dimension dimTo;
  54.         int nSize = m_vLocs.size();
  55.         for (int i = 0; i < nSize - 1; i++)
  56.         {
  57.             dimFrom = (Dimension)m_vLocs.elementAt(i);
  58.             for (int j = i + 1; j < nSize; j++)
  59.             {
  60.                 dimTo = (Dimension)m_vLocs.elementAt(j);
  61.                 g.drawLine(dimFrom.width, dimFrom.height,
  62.                            dimTo.width,   dimTo.height   );
  63.             }
  64.         }
  65.     }
  66.  
  67.     public void start()
  68.     {
  69.     }
  70.     
  71.     public void stop()
  72.     {
  73.     }
  74.  
  75.  
  76.     public boolean mouseDown(Event evt, int x, int y)
  77.     {
  78.         // in the event of a double click...
  79.         if (evt.clickCount > 1)
  80.         {
  81.             // ...remove all elements from the vector...
  82.             m_vLocs.removeAllElements();
  83.         }
  84.         else
  85.         {
  86.             // ...otherwise, add an element
  87.             m_vLocs.addElement(new Dimension(x, y));
  88.         }
  89.         repaint();
  90.         return true;
  91.     }
  92.  
  93.     public boolean mouseUp(Event evt, int x, int y)
  94.     {
  95.         // ignore the mouseUp event
  96.         return true;
  97.     }
  98.  
  99.     public boolean mouseDrag(Event evt, int x, int y)
  100.     {
  101.         // ignore the mouseDrag event
  102.         return true;
  103.     }
  104.  
  105.     public boolean mouseMove(Event evt, int x, int y)
  106.     {
  107.         // record the mouse location and repaint it
  108.         m_dimCursorLoc = new Dimension(x, y);
  109.         repaint();
  110.         return true;
  111.     }
  112.  
  113. }
  114.