import java.awt.*; import java.applet.Applet; public class EventMonitor extends Applet { final int maxLines = 20; TextArea theEvents = new TextArea(maxLines,50); Button aButton = new Button("A button"); int eventCount = 0, lines = 0; Button mode = new Button("Show Enter/Exit/+-Focus/keyUp/mouseUp"); boolean showEntersEtc = false; private void addLine(String msg) { while (lines>=maxLines) { theEvents.replaceText("",0,1+theEvents.getText().indexOf('\n')); lines--; } eventCount++; theEvents.appendText(eventCount+": "+msg+"\n"); lines++; } public void init() { add(aButton); theEvents.setEditable(false); add(theEvents); add(mode); addLine("init"); repaint(); } public void paint( Graphics g ) { g.drawRect(bounds().x+1, bounds().y+1, bounds().width-2, bounds().height-2); addLine("paint"); } public void start() { moveCount = 0; dragCount = 0; addLine("start"); } public void stop() { addLine("stop"); int i; for (i=0; i<10000000; i++); } public void destroy() { addLine("destroy"); int i; for (i=0; i<10000000; i++); } public boolean mouseEnter(Event e, int x, int y) { if (showEntersEtc) { moveCount = 0; dragCount = 0; addLine("("+x+","+y+") mouseEnter into "+e.target.getClass().getName()); } return true; } public boolean mouseExit(Event e, int x, int y) { if (showEntersEtc) { moveCount = 0; dragCount = 0; addLine("("+x+","+y+") mouseExit from "+e.target.getClass().getName()); } return true; } public boolean mouseDown(Event e, int x, int y) { moveCount = 0; dragCount = 0; addLine("("+x+","+y+") mouseDown in "+e.target.getClass().getName()); return true; } public boolean mouseUp(Event e, int x, int y) { if (showEntersEtc) { moveCount = 0; dragCount = 0; addLine("("+x+","+y+") mouseUp in "+e.target.getClass().getName()); } return true; } int moveCount = 0; public boolean mouseMove(Event e, int x, int y) { if (moveCount==0) addLine("("+x+","+y+") mouseMove in "+e.target.getClass().getName()); moveCount++; if (moveCount==5) moveCount = 0; return true; } int dragCount = 0; public boolean mouseDrag(Event e, int x, int y) { if (dragCount==0) addLine("("+x+","+y+") mouseDrag in "+e.target.getClass().getName()); dragCount++; if (dragCount==5) dragCount = 0; return true; } public boolean gotFocus(Event e, Object o) { if (showEntersEtc) { moveCount = 0; dragCount = 0; addLine("gotFocus for "+e.target.getClass().getName()); } return true; } public boolean lostFocus(Event e, Object o) { if (showEntersEtc) { moveCount = 0; dragCount = 0; addLine("lostFocus from "+e.target.getClass().getName()); } return true; } public boolean keyDown(Event e, int key) { moveCount = 0; dragCount = 0; addLine("keyDown in "+e.target.getClass().getName()); return true; } public boolean keyUp(Event e, int key) { if (showEntersEtc) { moveCount = 0; dragCount = 0; addLine("keyUp in "+e.target.getClass().getName()); } return true; } public boolean action(Event e, Object o) { moveCount = 0; dragCount = 0; addLine("action in "+e.target.getClass().getName()); if (e.target==mode) { showEntersEtc = !showEntersEtc; if (showEntersEtc) mode.setLabel("Ignore Enter/Exit/+-Focus/keyUp/mouseUp"); else mode.setLabel("Show Enter/Exit/+-Focus/keyUp/mouseUp"); } return true; } public boolean handleEvent(Event e) { if (!super.handleEvent(e)) { moveCount = 0; dragCount = 0; addLine("handleEvent: other event in "+e.target.getClass().getName()); } return true; } }