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

  1. import java.applet.*;
  2. import java.awt.*;
  3.  
  4. public class CrossHair extends Applet
  5. {
  6.     // User data here
  7.     private Dimension m_dimCursorLoc;  // location of cursor
  8.     private boolean m_bDrag;   // TRUE->we are currently dragging
  9.  
  10.     public CrossHair()
  11.     {
  12.     }
  13.  
  14.     public String getAppletInfo()
  15.     {
  16.         return "Name: CrossHair\r\n" +
  17.                "Author: Stephen R. Davis\r\n" +
  18.                "Created for Learn Java Now (c)";
  19.     }
  20.  
  21.  
  22.     public void init()
  23.     {
  24.         resize(640, 480);
  25.  
  26.     }
  27.  
  28.     public void destroy()
  29.     {
  30.     }
  31.  
  32.     // CrossHair Paint Handler
  33.     //-----------------------------------------------------------
  34.     public void paint(Graphics g)
  35.     {
  36.         // first build a string containing the current cursor
  37.         // location in the format (xx,yy) and output that
  38.         String sCursorLoc = "("
  39.                             + m_dimCursorLoc.width
  40.                             + ","
  41.                             + m_dimCursorLoc.height
  42.                             + ")";
  43.         g.drawString(sCursorLoc, 10, 20);
  44.  
  45.         // now put an x where the cursor is located
  46.         // (make it red if we are dragging and black if not)
  47.         if (m_bDrag)
  48.         {
  49.             g.setColor(Color.red);
  50.         }
  51.         else
  52.         {
  53.             g.setColor(Color.black);
  54.         }
  55.         int nX = m_dimCursorLoc.width;
  56.         int nY = m_dimCursorLoc.height;
  57.         g.drawLine(nX - 2,     nY, nX + 2,     nY);
  58.         g.drawLine(    nX, nY - 2,     nX, nY + 2);
  59.     }
  60.  
  61.     public void start()
  62.     {
  63.     }
  64.     
  65.     public void stop()
  66.     {
  67.     }
  68.  
  69.  
  70.  
  71.     // MOUSE SUPPORT:
  72.     //-----------------------------------------------------------
  73.     public boolean mouseDrag(Event evt, int x, int y)
  74.     {
  75.         // set the drag mode to true
  76.         m_bDrag = true;
  77.  
  78.         // record the mouse location
  79.         m_dimCursorLoc = new Dimension(x, y);
  80.  
  81.         // now force a repaint - this will repaint the window 
  82.         // with the cross hair at the current mouse location
  83.         repaint();
  84.  
  85.         // returning true indicates that we handled the event
  86.         return true;
  87.     }
  88.  
  89.     // MOUSE SUPPORT:
  90.     //-----------------------------------------------------------
  91.     public boolean mouseMove(Event evt, int x, int y)
  92.     {
  93.         // same as mouseDrag with drag mode turned off
  94.         m_bDrag = false;
  95.         m_dimCursorLoc = new Dimension(x, y);
  96.         repaint();
  97.  
  98.         return true;
  99.     }
  100.  
  101. }
  102.