# Drawing a castle and towers with acm.graphics

The following code is my solution of exercise 10 of chapter 5 of The Art and Science of Java. So here, using acm program and graphics, I need to draw a castle with two side towers and other three consecutive towers some distance away from the main building(castle and its two side towers). Is there any way to do this faster than that?

(The drawing is perfect as I spent plenty of time searching for the perfect location for every object on the canvas.)

import acm.program.*;
import acm.graphics.*;

public class Hw1 extends GraphicsProgram {

final double tower_width = 150;
final double tower_height = 600;
final double main_house_width = 400;
final double main_house_height = 500;
final double s_tower_width = 75;
final double s_tower_height = 300;
final double door_width = 50;
final double door_height = 100;
final double window_width = 100;
final double window_height = 100;

GRect create_tower(double x, double y) {
GRect rect = new GRect(tower_width, tower_height);
rect.move(x, y);
return rect;
}

GRect create_main_home(double x, double y) {
GRect rect = new GRect(main_house_width, main_house_height);
rect.move(x, y);
return rect;
}

GRect create_s_tower(double x, double y) {
GRect rect = new GRect(s_tower_width, s_tower_height);
rect.move(x, y);
return rect;
}

GRect create_door(double x, double y) {
GRect rect = new GRect(door_width, door_height);
rect.move(x, y);
return rect;
}

GOval create_window(double x, double y) {
GOval circle = new GOval(window_width, window_height);
circle.move(x, y);
return circle;
}

GPolygon create_arch(double x, double y) {
GPolygon arch = new GPolygon();
return arch;
}

GPolygon create_s_arch(double x, double y) {
GPolygon arch = new GPolygon();
return arch;
}

GPolygon create_arch_door(double x, double y) {
GPolygon arch = new GPolygon();
return arch;
}

GPolygon create_house_arch(double x, double y) {
GPolygon arch = new GPolygon();
return arch;
}

public void run() {

}

}

• Doesn't THE ART AND SCIENCE OF JAVA teach to use camel case for method names? And upper case for constants? If it really doesn't, try a different book. And as you introduced constants for the object geometries, you should have eliminated the x+400 or y-200 expressions as well. – Ralf Kleberhoff Jan 15 '18 at 12:25
• @RalfKleberhoff Propably, it talks about camel case etc... and propably i forgot to follow this rule when i wrote this code or propably i didn't care or propably i don't know what...I don't think that answers my question though. – Κωνσταντίνος Κορναράκης Jan 15 '18 at 12:34

There is not much to optimize but the following will still speed up things:

final double tower_width = 150;
-------------------------------------------------- Compiler can fill in better
private static final double TOWER_WIDTH = 150;


And

GRect create_tower(double x, double y) {
-------------------------------------------------- Compiler can inline method body
private GRect createTower(double x, double y) {


Stylistic:

public void run() {
-------------------------------------------------- Code Style, detects misspelling
@Override
public void run() {


Real optimisations can be done, by the variations offered by the GObject API:

GRect create_main_home(double x, double y) {
GRect rect = new GRect(main_house_width, main_house_height);
rect.move(x, y);
return rect;
}
-------------------------------------------------- Optimised API usage
GRect createMainHome(double x, double y) {
return new GRect(x, y, MAIN_HOUSE_WIDTH, MAIN_HOUSE_HEIGHT);
}


I took the liberty to use the java style that is quite a hard convention in java (so I do not get downvoted).

Questionable is whether to use int or float instead of double. I would leave it at double.

• I agree with your corrections in my code, i could have saved quite many lines in my code that way. I am not really sure if i get the reason you added @Override before public void run(). – Κωνσταντίνος Κορναράκης Jan 15 '18 at 13:56
• My assumption on the functioning of the code, is that you have to implement run. I have seen some errors, where @Override was left out, but the function signature had a small error: a wrong parameter class. Suppose you would have written rum instead of run (psychologically telling). Then you would wonder why the function was not called. – Joop Eggen Jan 15 '18 at 14:09

1) instead of having a create method for objects, why not have classes that you can instantiate? seems more clear to me

public class Tower extends GRect {
final double width = 150;
final double height = 600;
public Tower() {
super(width, height);
}
}


2) instead of creating a GRect and then moving it, you can specify the location when you add it: add(new Tower(), 700, 250); this shows more clearly the intention of placing the tower at a certain location

• I agree about your second comment... The problem is that I am not allowed to use super(), so even if your solution for my problem is right, I can't implement what exercise says in this way. Useful, answer thanks! – Κωνσταντίνος Κορναράκης Jan 15 '18 at 16:46