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

  1. import java.applet.Applet;
  2. import java.awt.Color;
  3. import java.awt.Component;
  4. import java.awt.Dimension;
  5. import java.awt.Event;
  6. import java.awt.Font;
  7. import java.awt.FontMetrics;
  8. import java.awt.Graphics;
  9.  
  10. public class CrossHair extends Applet {
  11.    private Dimension m_dimCursorLoc;
  12.    private boolean m_bDrag;
  13.    private FontMetrics m_fm;
  14.  
  15.    public void start() {
  16.    }
  17.  
  18.    public void stop() {
  19.    }
  20.  
  21.    public boolean mouseMove(Event evt, int x, int y) {
  22.       this.m_bDrag = false;
  23.       this.m_dimCursorLoc = new Dimension(x, y);
  24.       ((Component)this).repaint();
  25.       return true;
  26.    }
  27.  
  28.    public boolean mouseDrag(Event evt, int x, int y) {
  29.       this.m_bDrag = true;
  30.       this.m_dimCursorLoc = new Dimension(x, y);
  31.       ((Component)this).repaint();
  32.       return true;
  33.    }
  34.  
  35.    public String getAppletInfo() {
  36.       return "Name: CrossHair\r\n" + "Author: Stephen R. Davis\r\n" + "Created for Learn Java Now (c)";
  37.    }
  38.  
  39.    public void destroy() {
  40.    }
  41.  
  42.    public void init() {
  43.       ((Applet)this).resize(640, 480);
  44.       Font f = ((Component)this).getFont();
  45.       this.m_fm = ((Component)this).getFontMetrics(f);
  46.    }
  47.  
  48.    public void paint(Graphics g) {
  49.       String sCursorLoc = "(" + this.m_dimCursorLoc.width + "," + this.m_dimCursorLoc.height + ")";
  50.       int nY = 2 * this.m_fm.getHeight();
  51.       Dimension dimWinSize = ((Component)this).size();
  52.       int nWidth = this.m_fm.stringWidth(sCursorLoc);
  53.       int nX = dimWinSize.width - (nWidth + 5);
  54.       g.drawString(sCursorLoc, nX, nY);
  55.       if (this.m_bDrag) {
  56.          g.setColor(Color.red);
  57.       } else {
  58.          g.setColor(Color.black);
  59.       }
  60.  
  61.       nX = this.m_dimCursorLoc.width;
  62.       nY = this.m_dimCursorLoc.height;
  63.       g.drawLine(nX - 2, nY, nX + 2, nY);
  64.       g.drawLine(nX, nY - 2, nX, nY + 2);
  65.    }
  66. }
  67.