/* DrawBox allows the user to draw a box on the applet using the mouse. The following methods are overridden: init() To set a background color mouseDown() To set the starting point of the box mouseDrag() To "rubberband" the box during drag operation paint() To perform the actual drawing of the box */ import java.awt.*; import java.applet.*; public class DrawBox extends Applet { int currMouseX, currMouseY, startMouseX, startMouseY; public void init() { setBackground(Color.white); } public boolean mouseDown(Event evt, int x, int y) { startMouseX = x; startMouseY = y; return true; } public boolean mouseDrag(Event evt, int x, int y) { currMouseX = x; currMouseY = y; repaint(); return true; } public void paint(Graphics g) { int beginX, beginY, w, h; beginX = (startMouseX < currMouseX) ? startMouseX : currMouseX; beginY = (startMouseY < currMouseY) ? startMouseY : currMouseY; w = Math.abs(currMouseX - startMouseX); h = Math.abs(currMouseY - startMouseY); g.drawRect(beginX, beginY, w, h); } }