/* File: Castle.java For Lab 3, CMSC 220, Fall 1998 U. Wolz */ /* There are syntatic and run time bugs in this class. The applet "GoodCastle" with corresponding html shows you what the display is supposed to look like." */ import java.awt.*; import java.applet.Applet; public class Castle extends Applet { /* The following two methods do not have run time errors, but do have syntax errors */ // A castle consists of a drawbridge between two watchtowers. public void aCastle(Graphics g, int x, int y, int towerWidth, int bridgeSize) // The left watchtower at position x. g.setColor(Color.blue); watchTower(g,x,y,towerWidth); // The drawbridge is placed to the right of the watchtower at x + towerWidth drawbridge(g,x+towerWidth y,bridgeSize,Color.red); // The right watchtower is placed to the right of the bridge at x + towerWidth + bridgeSize. g.setColor(Color.blue); watchTower(g,x + towerWidth + bridgeSize,y,towerWidth); } // A drawbridge consists of two rectangles, the larger is the bridge, the smaller is the door. public void drawbridge(Graphics g, x, int y, int size, Color door) { // The bridge itself g.fillRect(x,y-size,size,size) // The door g.setColor(door); int doorSize = size/2; g.fillRect(x + size/2 - doorSize/2, y - DoorSize, doorSize, doorSize); /* This method watchTower is supposed to create a watch tower. The starting position is the upper left corner of the figure. The width of the entire structure is defined by the argument width. The height of the entire sturcture is twice the width. The lower tower part is 75% of the height, the turrets are 25% of the height. The turret structure consists of 3 blocks, each is 25% of the tower's width) Two turrets appear on the edges of the tower, one is centered in the middle. There are no syntax errors in this method, but there are run time errors. */ public void watchTower(Graphics g, int x, int y, int width) { int height = 2*width; int towerHeight = 3/4*height; int turretHeight = height/4; int towerWidth = height/2; int turretWidth = towerWidth/4; // The leftmost turret g.fillRect(x,y ,turretWidth,turretHeight); // The middle turret g.fillRect(x+towerWidth/2 - turretWidth/2,y,turretWidth,turretHeight); // The rightmost turret g.fillRect(x+towerWidth -turretWidth,y - height,turretWidth,turretHeight); // The bottom of the tower g.fillRect(x,y - towerHeight,towerWidth,turretHeight); } /* There are no errors in this method */ public void paint( Graphics g ) { aCastle(g, 10,400,100,150); watchTower(g,100,100,10); watchTower(g,110,110,20); watchTower(g,130,120,30); watchTower(g,160,130,40); aCastle(g, 40,50,10,15); } }