// mas964 ps5 // A calligraphy applet // this one is really simple, but has some nice properties. import java.applet.*; import java.awt.*; import java.lang.Math; public class Calligraph extends Applet { Graphics g; int lastX, lastY; public boolean mouseDown(Event e, int mouseX, int mouseY) { lastX = mouseX; lastY = mouseY; return true; } // draw a rectangle between the last mouse event and this one public boolean mouseDrag(Event e, int mouseX, int mouseY) { Graphics g = getGraphics(); g.fillRect(Math.min(lastX, mouseX), Math.min(lastY, mouseY), Math.abs(lastX - mouseX) + 1, Math.abs(lastY - mouseY) + 1); lastX = mouseX; lastY = mouseY; return true; } // space bar clears the screen public boolean keyDown(Event e, int key) { if ((char)(e.key) == ' ') { repaint(); return true; } else return super.keyDown(e, key); } public void init() { g = getGraphics(); this.setForeground(Color.black); this.setBackground(Color.white); super.init(); } }