import java.awt.*; import java.applet.*; public class SimpleTrackMouse extends Applet { int mouse_x, mouse_y; // mouse coordinates String modifierKeys = ""; // special keys that are held down Color displayColor = Color.black; public void init() { // I want a white background setBackground(Color.white); } public void paint(Graphics g) { int w = size().width; // get the width of the applet int h = size().height; // get the height of the applet g.setColor(Color.blue); g.drawRect(0,0,w-1,h-1); // draw a frame for the applet area g.setColor(Color.black); g.drawString(modifierKeys, 4, 20); g.setColor(displayColor); g.drawString("(" + mouse_x + "," + mouse_y + ")", mouse_x, mouse_y); } // end of paint() void setInfo(Event evt, int x, int y) { // set up the information about event for display mouse_x = x; mouse_y = y; modifierKeys = ""; if (evt.shiftDown()) modifierKeys += "Shift "; if (evt.controlDown()) modifierKeys += "Control "; if (evt.metaDown()) modifierKeys += "META "; if ((evt.modifiers & Event.ALT_MASK) != 0) modifierKeys += "ALT "; } public boolean mouseMove(Event evt, int x, int y) { setInfo(evt, x, y); displayColor = Color.black; repaint(); return true; } public boolean mouseDrag(Event evt, int x, int y) { setInfo(evt, x, y); displayColor = Color.red; // red when mouse is down repaint(); return true; } } // end of class SimpleMouseTracker