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

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