Graphic by Keith Ohlfs |
|
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:
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:
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:
this.drawTurret(g, Color.red,50, 150, 25, 100);
this.drawArch(g, Color.blue, Color.red, 75, 150, 75, 75);
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 | x | y | width | height |
Wall 1 | Red | Blue | Yellow | 60 | 125 | 210 | 100 |
Wall 2 | Green | Red | Blue | 25 | 250 | 250 | 150 |
Wall 3 | Yellow | Blue | Green | 40 | 385 | 300 | 175 |
Your final drawing should look like this:
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.