home *** CD-ROM | disk | FTP | other *** search
/ Learn Java Now / Learn_Java_Now_Microsoft_1996.iso / JavaNow / Code / Chap13 / CrossHair / CrossHair.class (.txt) next >
Encoding:
Java Class File  |  1996-07-29  |  2.1 KB  |  58 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.Graphics;
  7.  
  8. public class CrossHair extends Applet {
  9.    private Dimension m_dimCursorLoc;
  10.    private boolean m_bDrag;
  11.  
  12.    public void start() {
  13.    }
  14.  
  15.    public void stop() {
  16.    }
  17.  
  18.    public boolean mouseMove(Event evt, int x, int y) {
  19.       this.m_bDrag = false;
  20.       this.m_dimCursorLoc = new Dimension(x, y);
  21.       ((Component)this).repaint();
  22.       return true;
  23.    }
  24.  
  25.    public boolean mouseDrag(Event evt, int x, int y) {
  26.       this.m_bDrag = true;
  27.       this.m_dimCursorLoc = new Dimension(x, y);
  28.       ((Component)this).repaint();
  29.       return true;
  30.    }
  31.  
  32.    public String getAppletInfo() {
  33.       return "Name: CrossHair\r\n" + "Author: Stephen R. Davis\r\n" + "Created for Learn Java Now (c)";
  34.    }
  35.  
  36.    public void destroy() {
  37.    }
  38.  
  39.    public void init() {
  40.       ((Applet)this).resize(640, 480);
  41.    }
  42.  
  43.    public void paint(Graphics g) {
  44.       String sCursorLoc = "(" + this.m_dimCursorLoc.width + "," + this.m_dimCursorLoc.height + ")";
  45.       g.drawString(sCursorLoc, 10, 20);
  46.       if (this.m_bDrag) {
  47.          g.setColor(Color.red);
  48.       } else {
  49.          g.setColor(Color.black);
  50.       }
  51.  
  52.       int nX = this.m_dimCursorLoc.width;
  53.       int nY = this.m_dimCursorLoc.height;
  54.       g.drawLine(nX - 2, nY, nX + 2, nY);
  55.       g.drawLine(nX, nY - 2, nX, nY + 2);
  56.    }
  57. }
  58.