home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / Java++ / VJ / SAMPLES / JAVANOW / CHAP13 / CROSSHAIR2 / CROSSHAIR.JAVA < prev    next >
Encoding:
Java Source  |  1996-06-17  |  3.0 KB  |  118 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.     private FontMetrics m_fm;  // font metrics
  10.  
  11.     public CrossHair()
  12.     {
  13.     }
  14.  
  15.     public String getAppletInfo()
  16.     {
  17.         return "Name: CrossHair\r\n" +
  18.                "Author: Stephen R. Davis\r\n" +
  19.                "Created for Learn Java Now (c)";
  20.     }
  21.  
  22.  
  23.     public void init()
  24.     {
  25.         resize(640, 480);
  26.  
  27.         // Let's get the font metrics so we know how big our
  28.         // font really is
  29.         Font f = getFont();
  30.         m_fm = getFontMetrics(f);
  31.     }
  32.  
  33.     public void destroy()
  34.     {
  35.     }
  36.  
  37.     // CrossHair Paint Handler
  38.     //-----------------------------------------------------------
  39.     public void paint(Graphics g)
  40.     {
  41.         // first build a string containing the current cursor
  42.         // location in the format (xx,yy) and output that
  43.         String sCursorLoc = "("
  44.                             + m_dimCursorLoc.width
  45.                             + ","
  46.                             + m_dimCursorLoc.height
  47.                             + ")";
  48.         // calculate the y offset of line 2
  49.         // (this is 2 * the height of the font)
  50.         int nY = 2 * m_fm.getHeight();
  51.  
  52.         // (set x so that the string ends 5 pixels from
  53.         // the right hand side of the window)
  54.         Dimension dimWinSize = size();    // size of the window
  55.         int nWidth = m_fm.stringWidth(sCursorLoc);
  56.         int nX = dimWinSize.width - (nWidth + 5);
  57.  
  58.         // now paint the string at that location
  59.         g.drawString(sCursorLoc, nX, nY);
  60.  
  61.         // now put an x where the cursor is located
  62.         // (make it red if we are dragging and black if not)
  63.         if (m_bDrag)
  64.         {
  65.             g.setColor(Color.red);
  66.         }
  67.         else
  68.         {
  69.             g.setColor(Color.black);
  70.         }
  71.         nX = m_dimCursorLoc.width;
  72.         nY = m_dimCursorLoc.height;
  73.         g.drawLine(nX - 2,     nY, nX + 2,     nY);
  74.         g.drawLine(    nX, nY - 2,     nX, nY + 2);
  75.     }
  76.  
  77.     public void start()
  78.     {
  79.     }
  80.     
  81.     public void stop()
  82.     {
  83.     }
  84.  
  85.  
  86.  
  87.     // MOUSE SUPPORT:
  88.     //-----------------------------------------------------------
  89.     public boolean mouseDrag(Event evt, int x, int y)
  90.     {
  91.         // set the drag mode to true
  92.         m_bDrag = true;
  93.  
  94.         // record the mouse location
  95.         m_dimCursorLoc = new Dimension(x, y);
  96.  
  97.         // now force a repaint - this will repaint the window 
  98.         // with the cross hair at the current mouse location
  99.         repaint();
  100.  
  101.         // returning true indicates that we handled the event
  102.         return true;
  103.     }
  104.  
  105.     // MOUSE SUPPORT:
  106.     //-----------------------------------------------------------
  107.     public boolean mouseMove(Event evt, int x, int y)
  108.     {
  109.         // same as mouseDrag with drag mode turned off
  110.         m_bDrag = false;
  111.         m_dimCursorLoc = new Dimension(x, y);
  112.         repaint();
  113.  
  114.         return true;
  115.     }
  116.  
  117. }
  118.