Question

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?

appwindow

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);
}
Was it helpful?

Solution

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:

  • a border supplied by the OS. It is 400x600 on the inside.
  • a menu bar.
  • a drawable area (canvas) below the the menu bar. It is shorter than 400x600 by the menu bar width.

You either

  • need to read the canvas size and use it instead of the window size: getCanvas().getHeight()
  • need to set the canvas size instead of the window size and let the window size itself as needed. As GraphicsProgram is an Applet, this doesn't seem possible.
  • need to find out the menu height and increase the window height by that amount (last resort).

OTHER TIPS

The size of the window can be changed using the setSize() method. Doing than, you are still facing a coordinates issue. When reading the size of the working area, it is constantly decreased by 16 for the width, and 58 for the heigth (probably due to the menu and W7 borders, as mentioned before).

You just have to take care of it when calling setSize().

[OFF]I'm also working on this breakout assignment and had the same issue. Posted a few minutes ago on this site.[/OFF]

I've made it a habit to explicitly set application size @ the load-time of all my applets. This is how I usually set window size:

@Override

public void init(){
    resize(APPLICATION_WIDTH, APPLICATION_HEIGHT);
}

Also, you don't need to call init() from run(); it's automatically called by your appletviewer at initialization time.

I think you should use:

b = new GRect(5,5,getWidth()-10,getHeight()-10); //gets window width & height

or:::

b = new GRect(5,5,b.getWidth()-10,b.getHeight()-10); //gets former 'b' width & height

instead of:

b = new GRect(5,5,APPLICATION_WIDTH-10,APPLICATION_HEIGHT-10);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top