// adapted from java in a nutshell import java.applet.*; import java.awt.*; import java.awt.event.*; public class scribble extends Applet { private int x1, y1; public void init() { this.enableEvents(AWTEvent.MOUSE_EVENT_MASK | AWTEvent.MOUSE_MOTION_EVENT_MASK | AWTEvent.KEY_EVENT_MASK); this.requestFocus(); } public void processMouseEvent(MouseEvent e) { if (e.getID() == MouseEvent.MOUSE_PRESSED) { x1 = e.getX(); y1 = e.getY(); } else super.processMouseEvent(e); } public void processMouseMotionEvent(MouseEvent e) { if (e.getID() == MouseEvent.MOUSE_DRAGGED) { int x = e.getX(), y = e.getY(); Graphics g = this.getGraphics(); g.setColor(new Color(255,0,0)); g.drawLine(x1, y1, x, y); x1 = x; y1 = y; } else super.processMouseMotionEvent(e); } public void processKeyEvent(KeyEvent e) { if ((e.getID() == KeyEvent.KEY_TYPED)) { Graphics g = this.getGraphics(); if (e.getKeyChar() == 'b') { g.setColor(new Color(0,0,255)); } if (e.getKeyChar() == 'c') { g.setColor(new Color(255,255,255)); } if (e.getKeyChar() == 'g') { g.setColor(new Color(0,255,0)); } if (e.getKeyChar() == 'r') { g.setColor(new Color(255,0,0)); } g.fillRect(0, 0, this.getSize().width, this.getSize().height); } else super.processKeyEvent(e); } }