2

I am trying to learn java using Stanford's online CS106A course. The course uses the acm library packages from http://jtf.acm.org/

Can anyone tell me why is my code below doesn't centered the label along the y axis?

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

public class CenterLabel extends GraphicsProgram {
    public void run() {
        GLabel text = new GLabel("HELLO!");
        text.setFont("Arial-200");
        double cy = (getHeight() / 2) + (text.getAscent() / 2);
        text.setLocation(0, cy);
        add(text);
    }
}

When I run this program I get this: https://i.stack.imgur.com/9U4L8.jpg

Edit: Another thing that's troubling me is that the sample run in the assignment also seem to have the same alignment problem. (See link below, Ex3)

http://see.stanford.edu/materials/icspmcs106a/13-assignment-2-simple-java.pdf

If you look at the picture carefully, you can see that the label inside the class box is also not centered properly?

Robin Green
  • 32,079
  • 16
  • 104
  • 187
user1296058
  • 173
  • 1
  • 12

2 Answers2

1

You can use the setBounds method for positioning the label where you want. It takes four int arguments. e.g:

text.setBounds(20,20,20,20);
pjumble
  • 16,880
  • 6
  • 43
  • 51
0

Edit: Oh dear, I misentered your code when I was testing it out! I accidentally was subtracting the two values, not adding them. getAscent() return a negative number representing how many pixels above the baseline the text ascends, so the correct formula for the position is

double cy = (getHeight() / 2) - (text.getAscent() / 2);

The logic is the same, but the fact that getAscent() is negative means that you have to flip the sign. Sorry about that!


In CS106A, we typically don't run Java programs as applets precisely because the default applet viewer gets the dimensions of the window incorrectly (that is, calling getWidth() or getHeight() will produce incorrect results). If you download Stanford Eclipse from the CS106A website, you'll get an Eclipse plugin that can run the Java programs you write as standalone applications. When this happens, the window size and shape is actually correct, so getWidth() and getHeight() will report correct values.

Alternatively, consider adding the following code to your programs:

public static void main(String[] args) {
    new /* name of your class */.start(args);
}

You can then run the program directly as a Java application rather than an applet, and this problem should be resolved.

Hope this helps!

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
  • I've downloaded the eclipse in the link you provided and ran the code in my original post. Still the same problem. I've also tried the older version of eclipse in the original online course. That also produces the same result. Are you able to center the label properly in your computer? – user1296058 Mar 31 '12 at 19:09
  • How are you running the program? Are you using the little running guy icon? This works just fine on my machine. – templatetypedef Mar 31 '12 at 19:13
  • yes I am using the running guy icon. The bigger the font size, the more off center it is. :( Here is a screen shot with a font size of 400 using the eclipse in the link you provided. http://i.imgur.com/z07RO.jpg – user1296058 Mar 31 '12 at 19:15
  • @user1296058- Ooops! My mistake - I misentered your code! Check the answer for an update. – templatetypedef Apr 01 '12 at 01:00
  • That's weird. getAscent() seem to return a positive value for me. I think maybe getAscent() is returning the wrong value? In my program getHeight() returns a value of 492 so I set the font size to 490 **text.setFont("Arial-490")**, then **text.setLocation(0, 490)**. This should make the font almost as big as the window. But when I run the program the font is still only 3/4 of the screen height. – user1296058 Apr 01 '12 at 12:31