0

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.

Jonas Schäfer
  • 20,140
  • 5
  • 55
  • 69
  • setSize is a method inherited from class java.awt.Component, you need to invoque it in order to change the window size. – Ricardo Rivaldo Feb 10 '14 at 13:59
  • I do recognize that that works. But according to the book I'm working with any subclass of GraphicsProgram should only need constant declarations of APPLICATION_WIDTH and APPLICATION_HEIGHT. I know this may seem silly since writing setSize works just fine, but I was curious if declaring the constant works for anybody and if so, do they see why its not working here. – Eric Lehmann Feb 12 '14 at 03:52

1 Answers1

0

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.

San Lewy
  • 136
  • 1
  • 3
  • 13
  • Thanks so much! I didn't really mean that he was lying to me, i knew in the end I was missing something. Its a really great book and I'm enjoying working with it. Thanks again for answering. – Eric Lehmann Feb 20 '14 at 19:09
  • @Eric I was just trying to poke a little fun your way. Sometimes one can't see the obvious error no matter how long you look at it. Glad I could help. And I agree, that is a great book - well worth the cost. – San Lewy Feb 21 '14 at 06:14