Graphic by Keith Ohlfs

CS111, Wellesley College, Fall 1997

Lab #4
[CS111 Home Page] [Syllabus] [Students] [Lecture Notes] [Assignments] [Programs] [Documentation] [Software Installation] [FAQ] [CS Dept.] [CWIS]

CS111 Lab #4: Methods

Due Date: Thursday, October 2, 6:00 p.m.

In lecture you have seen how to create and use methods to draw a pattern of different sizes and positions without having to write the same lines of code repeatedly. In lab 4, you will be using methods to build a castle. In prelab, you created a method for one component of this castle: an arch. Now we will add a method to draw a turret. We will then see how to combine methods into new methods to draw larger and more complex components of castles.

1) First, download the folder, turret, from the download directory for cs111 on Nike. This folder contains 4 files:

2) View the applet using the appletViewer and opening the turret.html file. You should see something like the following:

3) Open the project window and double click on turret.java to look at the source code. You will see only one method, the paint method, that contains all the drawing statements to draw a turret. We will copy the code that draws the turret into our castle.java file to create a new method, drawTurret. The parameters for drawTurret are as follows:

The drawTurret method will be introduced with the following:

public void drawTurret(Graphics g, Color turretClr, int x, int y, int width, int height)
{

The code to draw the turret goes here.

}

4) Now we will modify the java source file to draw a wall segment as follows:

5) Now, make a new method, drawWallSeg that draws the above wall segment. Make the width of the turret 1/4 of the total width of the wall segment, and make the arch width to be 3/4 of the total width. The height of the wall segment should specify the height of the turret and the arch should be 3/4 the height of the turret. The drawWallSeg method should be introduced as follows:

public void drawWallSeg(Graphics g, Color tClr, Color aClr, Color gClr, int x, int y, int width, int height)

6) Building further on your program, create a method, drawWall, that uses three invocations of drawWallSeg, and one invocation of drawTurret to draw the following wall:

The drawWall method will have the following parameters:

public void drawWall(Graphics g, Color tClr, Color aClr, Color gClr, int x, int y, int width, int height)

Each wall segment should be specified as 1/3 the total width of the wall and the height of the wall should be the height of each wall segment (and therefore equal to the turret height). The extra turret at the right edge will make the total width of the wall slightly longer than specified by the width parameter. The paint method will invoke the drawWall method as follows:

this.drawWall(g, Color.red, Color.blue, Color.yellow, 60, 125, 210, 100);

7) Finally, in the paint method, invoke the drawWall method three times, using the following parameters:
 Turret
Color
Arch
Color
Gate
Color
xywidthheight
Wall 1RedBlueYellow60125210100
Wall 2GreenRedBlue25250250 150
Wall 3YellowBlueGreen40385300 175

Your final drawing should look like this:

Turning in Lab 4

When you are finished. Save the four new castle files (castle.java, castle.proj, castle.class and castle.html) in a folder named "username_castle", where username is your username. Upload this folder into the lab4 drop folder on Nike.