Methods Summary |
---|
public void | init()Tell the system we're interested in mouse events, mouse motion events,
and keyboard events. This is required or events won't be sent.
this.enableEvents(AWTEvent.MOUSE_EVENT_MASK |
AWTEvent.MOUSE_MOTION_EVENT_MASK |
AWTEvent.KEY_EVENT_MASK);
this.requestFocus(); // Ask for keyboard focus so we get key events
|
public void | processKeyEvent(java.awt.event.KeyEvent e)Called on key events: clear the screen when 'c' is typed
if ((e.getID() == KeyEvent.KEY_TYPED) && (e.getKeyChar() == 'c")) {
Graphics g = this.getGraphics();
g.setColor(this.getBackground());
g.fillRect(0, 0, this.getSize().width, this.getSize().height);
}
else super.processKeyEvent(e); // pass unhandled events to our superclass
|
public void | processMouseEvent(java.awt.event.MouseEvent e)Invoked when a mouse event of some type occurs
if (e.getID() == MouseEvent.MOUSE_PRESSED) { // check the event type
lastx = e.getX(); lasty = e.getY();
}
else super.processMouseEvent(e); // pass unhandled events to superclass
|
public void | processMouseMotionEvent(java.awt.event.MouseEvent e)Invoked when a mouse motion event occurs
if (e.getID() == MouseEvent.MOUSE_DRAGGED) { // check type
int x = e.getX(), y = e.getY();
Graphics g = this.getGraphics();
g.drawLine(lastx, lasty, x, y);
lastx = x; lasty = y;
}
else super.processMouseMotionEvent(e);
|