Jan 97 Getting Started
Volume Number: 13
Issue Number: 1
Column Tag: Getting Started
Java's AWT Shapes
By Dave Mark
Two of the first things any Mac programmer learns are the concept of event-loop
programming and drawing using QuickDraw. The Java programming learning curve is
quite similar in this respect. Last month, we introduced Java's event handling
mechanism. This month, we'll explore the java.awt.Graphics class. Specifically, we'll
look at the java.awt.Graphics member functions that allow you to draw shapes in your
applet window.
The java.awt.Graphics Shapes
This month's applet is called javaDraw. Before you dig into the code, take some time to
read the java.awt.Graphics web page (part of the JDK API Documentation from Sun.)
Pay special attention to the function names that start with either the words draw or
frame. The draw routines are analogous to the QuickDraw paint routines (like
PaintRect()) and the frame routines do the same as the QuickDraw frame routines
(like FrameRect()).
Creating the javaDraw Project
Create a new project called javaDraw.ยต using the Java applet stationery. Create a new
source code file named javaDraw.html and add it to the project (replace any existing
html file that may have been added to the project.) Here's the html code:
javaDraw

height=160>

The source.
Next, create a second source code file named javaDraw.java and add it to the project as
well (replace any default .java file that may have been added to the project.) Here's the
Java code:
import java.awt.*;
public class shapeCanvas extends Canvas
{
final int rectangle=0,
rect3D=1,
roundRect=2,
arc=3,
oval=4,
line=5,
polygon=6;
int whichShape = rectangle;
booleanisFilled = true;
shapeCanvas( int width, int height )
{
setBackground( Color.yellow );
setForeground( Color.red );
resize( width, height );
}
public void SetShape( int newShape )
{
whichShape = newShape;
}
public void SetIsFilled( boolean newIsFilled )
{
isFilled = newIsFilled;
}
public void paint( Graphics g )
{
Rectangler, b;
Polygonpoly;
b = bounds();
r = new Rectangle( 10, 10, b.width - 20,
b.height - 20 );
poly = new Polygon();
poly.addPoint( r.x, r.y );
poly.addPoint( r.x, r.y + r.height );
poly.addPoint( r.x + r.width, r.y + r.height );
poly.addPoint( r.x, r.y + r.height/2 );
poly.addPoint( r.x + r.width, r.y + r.height/2 );
poly.addPoint( r.x, r.y );
if ( isFilled )
{
switch ( whichShape )
{
case rectangle:
g.fillRect( r.x, r.y, r.width, r.height );
break;
case rect3D:
g.fill3DRect( r.x, r.y, r.width, r.height, true );
break;
case roundRect:
g.fillRoundRect( r.x, r.y, r.width, r.height,
16, 16 );
break;
case arc:
g.fillArc( r.x, r.y, r.width, r.height, 0, 270 );
break;
case oval:
g.fillOval( r.x, r.y, r.width, r.height );
break;
case line:
g.drawLine( r.x, r.y, r.x + r.width, r.y + r.height
);
break;
case polygon:
g.fillPolygon( poly );
break;
}
}
else
{
switch ( whichShape )
{
case rectangle:
g.drawRect( r.x, r.y, r.width, r.height );
break;
case rect3D:
g.draw3DRect( r.x, r.y, r.width, r.height, true );
break;
case roundRect:
g.drawRoundRect( r.x, r.y, r.width, r.height,
16, 16 );
break;
case arc:
g.drawArc( r.x, r.y, r.width, r.height, 0, 270 );
break;
case oval:
g.drawOval( r.x, r.y, r.width, r.height );
break;
case line:
g.drawLine( r.x, r.y, r.x + r.width,
r.y + r.height );
break;
case polygon:
g.drawPolygon( poly );
break;
}
}
}
}
public class javaDraw extends java.applet.Applet
{
private Choice shapePopup, fillPopup;
private shapeCanvas sCanvas;
public void init()
{
shapePopup = new Choice();
shapePopup.addItem( "Rectangle" );
shapePopup.addItem( "3D Rectangle" );