Back to Applets page
Click in the boxes below, select all and copy to grab the applet
HTML listing:
<HTML> <HEAD> <TITLE>Listing 7.2</TITLE> </HEAD> <BODY BGCOLOR=WHITE> <TABLE BORDER=1 CELLPADDING=0 CELLSPACING=0><TR><TD> <APPLET CODEBASE="classes" CODE="Applet2.class" WIDTH=500 HEIGHT=100> </APPLET> </TD></TR></TABLE> </BODY> </HTML>
Applet listing:
import java.awt.*; import java.applet.*; import java.lang.*; public class Applet2 extends Applet { Point startPt; Point endPt; public void init() { setBackground(Color.white); } public boolean mouseDown(Event evt, int x, int y) { startPt = new Point(x, y); return true; } public boolean mouseDrag(Event evt, int x, int y) { endPt = new Point(x, y); repaint(); return true; } public boolean mouseUp(Event evt, int x, int y) { endPt = new Point(x,y); repaint(); return true; } public void paint(Graphics g) { int rectWidth, rectHeight, startX, startY; if (endPt != null) { startX = Math.min(startPt.x,endPt.x); startY = Math.min(startPt.y,endPt.y); rectWidth = Math.abs(endPt.x - startPt.x); rectHeight = Math.abs(endPt.y - startPt.y); g.fillRect(startX, startY, rectWidth, rectHeight); } } }