I've done quite a bit with the acm graphics library in the past and I've just begun working on the breakout problem that Stanford assigns. One of the issues which I've had several times in the past with graphics is the coordinates do not accurately reflect where the shapes are actually appearing although I've double and triple checked them. In the end, for those assignments I ended up eyeballing it and making modifications until it look kind of right. So obvious I'm missing something here cause a simple graphics program should not be this difficult.
For example, using acm.graphics my code looks like this.
public static final int APPLICATION_WIDTH = 400;
public static final int APPLICATION_HEIGHT = 600;
public void setupBorder(){
// **Sets Black Background
GRect b = new GRect(0,0,400,600); //** Sets Black Background
fillObj(b,Color.black);
add(b);
//**Sets White Square so border is 5 pixels wide on each side
b = new GRect(5,5,APPLICATION_WIDTH-10,APPLICATION_HEIGHT-10);
fillObj(b,Color.white);
add(b);
}
So my thinking here, is its moved 5 pixels right and 5 pixels down I need to subtract those and then an extra 5 pixels to have a border 5 pixels on each side (basic 2b+x = 1 side, 2a+y= vertical side). You would think this creates a black border 5 pixels on each side with a white square in the middle, but it doesn't. It creates a border on the left, right, and top side of the window but not the bottom. So with something this simple, what can possibly go wrong? I've double, triple, even quad checked the calculations on paper and it should be a white square in the middle with a 5 pixel black border, but it isn't. I've tried changing pixels for percentages, with worse effect. Manually editing the box till it looks close at about -35 for the Yvalue although that's just a bit too far. Anyone have an idea about what's going wrong?
Breakout with these changes made to run and other methods within class.
public void run() {
/* You fill this in, along with any subsidiary methods */
init();
}
public void init(){
setupBorder();
}
public void setupBorder(){
//**Black Background
GRect b = new GRect(0,0,400,600); //**Black Background
fillObj(b,Color.black);
add(b);
b = new GRect(5,5,APPLICATION_WIDTH-10,APPLICATION_HEIGHT-10);
fillObj(b,Color.white);
add(b);
}
public void fillObj(GFillable a, Color argC)
{
a.setFillColor(argC);
a.setFilled(true);
}
It looks like you're confusing the size of the application window (excluding the window border but including the menu, and which is 400*600 in your case) with the size of your drawable area (excluding the menu), which means you're painting outside the drawable area (which is below the menu.
Your window has:
You either
getCanvas().getHeight()
GraphicsProgram
is an Applet
, this doesn't seem possible.