home *** CD-ROM | disk | FTP | other *** search
- import java.applet.Applet;
- import java.awt.Color;
- import java.awt.Component;
- import java.awt.Dimension;
- import java.awt.Event;
- import java.awt.Font;
- import java.awt.FontMetrics;
- import java.awt.Graphics;
-
- public class CrossHair extends Applet {
- private Dimension m_dimCursorLoc;
- private boolean m_bDrag;
- private FontMetrics m_fm;
-
- public void start() {
- }
-
- public void stop() {
- }
-
- public boolean mouseMove(Event evt, int x, int y) {
- this.m_bDrag = false;
- this.m_dimCursorLoc = new Dimension(x, y);
- ((Component)this).repaint();
- return true;
- }
-
- public boolean mouseDrag(Event evt, int x, int y) {
- this.m_bDrag = true;
- this.m_dimCursorLoc = new Dimension(x, y);
- ((Component)this).repaint();
- return true;
- }
-
- public String getAppletInfo() {
- return "Name: CrossHair\r\n" + "Author: Stephen R. Davis\r\n" + "Created for Learn Java Now (c)";
- }
-
- public void destroy() {
- }
-
- public void init() {
- ((Applet)this).resize(640, 480);
- Font f = ((Component)this).getFont();
- this.m_fm = ((Component)this).getFontMetrics(f);
- }
-
- public void paint(Graphics g) {
- String sCursorLoc = "(" + this.m_dimCursorLoc.width + "," + this.m_dimCursorLoc.height + ")";
- int nY = 2 * this.m_fm.getHeight();
- Dimension dimWinSize = ((Component)this).size();
- int nWidth = this.m_fm.stringWidth(sCursorLoc);
- int nX = dimWinSize.width - (nWidth + 5);
- g.drawString(sCursorLoc, nX, nY);
- if (this.m_bDrag) {
- g.setColor(Color.red);
- } else {
- g.setColor(Color.black);
- }
-
- nX = this.m_dimCursorLoc.width;
- nY = this.m_dimCursorLoc.height;
- g.drawLine(nX - 2, nY, nX + 2, nY);
- g.drawLine(nX, nY - 2, nX, nY + 2);
- }
- }
-