Question

I am taking an iTunesU class that uses Eric S. Roberts' book "The Art and Science of Java". The book claims that if I extend GraphicsProgram (part of the acm.program JAR) then I can simply declare constants named APPLICATION_WIDTH and APPLICATION_HEIGHT and give them values and GraphicsProgram will look to see if I've done that and resize the window accordingly. I can resize my window by adding:

setSize(APPLICATION_WIDTH, APPLICATION_HEIGHT +CITATION_MARGIN);

in my run() method but according to the book I shouldn't have to. Is the book lying to me or have I missed something? Heres the code:

/*
 * File: FryImage.java
 * -------------------
 * This program displays an image with a citation to the
 * graphics window.
 */


package chap9;

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

public class FryImage extends GraphicsProgram {
// Citation constants
private static final String CITATION_FONT = "SansSerif-10";
private static final int CITATION_MARGIN = 30;

// dimensions of window
private static final int APPLICATION_WIDTH = 640;
private static final int APPLICATION_HEIGHT = 640 + CITATION_MARGIN;

public void run(){
    add(new GImage("ProfAlive.jpg"));
    addCitation("Courtesy of Reddit Weekly");
}

// Adds citation along bottom of window
private void addCitation(String text) {
    GLabel label = new GLabel(text);
    label.setFont(CITATION_FONT);
        double x = (getWidth() - label.getWidth()) / 2;
    double y = getHeight() - CITATION_MARGIN + label.getAscent();
    add(label, x, y);
    }
}

I saw questions similar to mine but none that addressed the ability to simply declare constants to resize the window.

Was it helpful?

Solution

Professor Roberts wouldn't lie to you. A closer examination of the code you are using from his fine book will show that the CONSTANTS in question (APPLICATION_WIDTH & APPLICATION_HEIGHT) need to be declared as PUBLIC, not PRIVATE as you have them. As you have them, the ACM Program Class can't find your declarations.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top