Feb 97 Getting Started
Volume Number: 13
Issue Number: 2
Column Tag: Getting Started
The ShapeWorld Applet
By Dave Mark
A few months ago I promised an applet that did double-buffered animation. When I
started work on the sample applet, I realized that I was definitely jumping the gun and
there was still a lot of ground to cover to get to double-buffering. Since then, I've
explored the AWT event-handling mechanism, as well as the various shape drawing
routines in the Graphics class.
This month I am going to bring these concepts (and a few new ones) together into an
applet called ShapeWorld. This month's version of ShapeWorld (I'll extend it next
month) randomly scatters some rectangles into a Canvas, then responds to mouse
clicks by selecting and deselecting the rectangles. As you'll see next month,
ShapeWorld was designed to be extended. For now, let's get the first version up and
running.
The ShapeWorld Project
Launch the CodeWarrior IDE and create a new project named ShapeWorld.ยต using the
Java applet stationery. Create a new source file named ShapeWorld.java and add it to
the project. Here's the ShapeWorld.java source code:
import java.awt.*;
import java.util.*;
abstract class Shape
ShapeCanvasshapeCanvas;
int shapeX, shapeY, shapeWidth, shapeHeight;
RectangleboundsRect;
Shape( ShapeCanvas canv, int x, int y,
int width, int height )
highlighted = false;
shapeX = x;
shapeY = y;
shapeWidth = width;
shapeHeight = height;
boundsRect = new Rectangle( x, y, width, height );
}
abstract public void draw( Graphics g );
public void setHighlight( boolean newHighlight )
highlighted = newHighlight;
}
public boolean isHighlighted()
public boolean isPointInShape( int x, int y )
return boundsRect.inside( x, y );
}
}
class RectShape extends Shape
RectShape( ShapeCanvas canv, int x, int y,
int width, int height )
super( canv, x, y, width, height );
}
public void draw( Graphics g )
if ( isHighlighted() )
g.setColor( Color.black );
g.fillRect( shapeX, shapeY, shapeWidth, shapeHeight );
g.setColor( Color.red );
g.fillRect( shapeX+2, shapeY+2,
shapeWidth-4, shapeHeight-4 );
}
else
g.setColor( Color.red );
g.fillRect( shapeX, shapeY, shapeWidth, shapeHeight );
}
}
}
class ShapeCanvas extends Canvas
Shape curShape;
ShapeCanvas( int width, int height )
shapes = new Vector();
curShape = null;
setBackground( Color.yellow );
resize( width, height );
}
public void addShape( Shape newShape )
shapes.addElement( newShape );
}
public void paint( Graphics g )
for ( Enumeration e = shapes.elements();
e.hasMoreElements(); )
Shape s = (Shape)e.nextElement();
s.draw( g );
}
}
public Shape findInShapeList( int x, int y )
for ( Enumeration e = shapes.elements();
e.hasMoreElements(); )
Shape s = (Shape)e.nextElement();
if ( s.isPointInShape( x, y ) )
s.setHighlight( ! s.isHighlighted() );
s.draw( getGraphics() );
return s;
}
}
return null;
}
public void update (Graphics g)
public boolean mouseDown( Event e, int x, int y )
curShape = findInShapeList( x, y );
return true;
}
}
public class ShapeWorld extends java.applet.Applet
final intshapeWidth = 20;
final intshapeHeight = 20;
public void init()
sCanvas = new ShapeCanvas( 440, 290 );
add( sCanvas );
Random ran = new Random();
Rectangle b = sCanvas.bounds();
for ( int i=1; i<=10; i++ )
x = b.x + (int)((float)(b.width) * ran.nextFloat() );