
import java.awt.*;
import java.applet.Applet;

public class DrawBox extends Applet {
	int currentX, currentY, startX, startY;
	boolean dragMode = false;

	// if in drag mode, paint the current rectangle
	public void paint(Graphics g) {
		int beginX, beginY, width, height;
		if (dragMode == true) {
			beginX = Math.min(startX, currentX);
			beginY = Math.min(startY, currentY);
			width = Math.abs(currentX - startX);
			height = Math.abs(currentY - startY);

			g.drawRect(beginX, beginY, width, height);
		}
                else {
                        g.drawString("Draw a box by dragging the mouse", 10, 25);
                }
	}

	// mouse down starts drag
	public boolean mouseDown(Event event, int x, int y) {
		dragMode = true;
		startX = x;
		startY = y;
		return true;
	}

	// mouse up ends drag
	public boolean mouseUp(Event event, int x, int y) {
		dragMode = false;
		return true;
	}

	// mouse drag, calculate new rectangle
	public boolean mouseDrag(Event event, int x, int y) {
		if (dragMode == true) {
			currentX = x;
			currentY = y;
			repaint();
		}
		return true;
	}
}


	
