Mar 97 Getting Started
Volume Number: 13
Issue Number: 3
Column Tag: Getting Started
An Expanded ShapeWorld Applet
By Dave Mark
Last month's Getting Started column introduced the ShapeWorld applet. The initial
version of ShapeWorld featured four classes: The ShapeWorld class extends the Applet
class and creates and controls all the applet objects. The ShapeCanvas class extends the
Canvas class and implements the canvas we'll do all our drawing in. The Shape class is
designed to be extended and provides the basic functions for implementing a shape. The
RectShape class extends Shape and overrides the Shape classes' abstract draw()
method to draw a rectangular shape.
Why divide the shapes into two separate classes? After all, we could have just done the
drawing in the Shape class. However, that is what this month's column is all about. We
are going to add a second Shape subclass to our applet, along with some code that lets
you drag your shapes around in the ShapeCanvas.
The ShapeWorld Project
Here's the source code to this month's applet. For the most part, we've added code to
last month's project with only a few lines changed (mostly to change the way
highlighting works - more on this in a moment).
import java.awt.*;
import java.util.*;
abstract class Shape
{
booleanhighlighted;
ShapeCanvasshapeCanvas;
int shapeX, shapeY, shapeWidth, shapeHeight;
RectangleboundsRect;
Shape( ShapeCanvas canv, int x, int y,
int width, int height )
{
shapeCanvas = canv;
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 erase( Graphics g )
{
g.clearRect( shapeX, shapeY, shapeWidth, shapeHeight );
}
public void setHighlight( boolean newHighlight )
{
highlighted = newHighlight;
}
public boolean isHighlighted()
{
return highlighted;
}
public boolean isPointInShape( int x, int y )
{
return boundsRect.inside( x, y );
}
public voidmoveThisMuch( int x, int y )
{
boundsRect.move( shapeX + x, shapeY + y );
shapeX = boundsRect.x;
shapeY = boundsRect.y;
shapeWidth = boundsRect.width;
shapeHeight = boundsRect.height;
}
}
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 OvalShape extends Shape
{
OvalShape( 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.fillOval( shapeX, shapeY, shapeWidth, shapeHeight );
g.setColor( Color.red );
g.fillOval( shapeX+2, shapeY+2,
shapeWidth-4, shapeHeight-4 );
}
else
{
g.setColor( Color.red );
g.fillOval( shapeX, shapeY, shapeWidth, shapeHeight );
}
}
}
class ShapeCanvas extends Canvas
{
Vector shapes;
Shape curShape;
Point mousePosition;
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.draw( getGraphics() );
return s;
}
}
return null;
}
public void update (Graphics g)
{
paint(g);
}
public boolean mouseDown( Event e, int x, int y )
{