Results 1 to 4 of 4
  1. #1
    Brain_Child is offline Member
    Join Date
    Nov 2009
    Posts
    5
    Rep Power
    0

    Default centering a label inside a rectangle

    hey,

    I'm trying to complete q3 from this pdf.

    stanford.edu/class/cs106a/handouts/13-assignment-2.pdf

    here is what ive written, so far it draws the middle line and the top box, but to me it looks like the label is off.

    ive checked it and gone through it on paper and to me it should work fine (in theory), so now id like some help, see if someone else can spot my problem

    here is my source. i have indicated the problem area in red.

    Java Code:
    import acm.graphics.*;
    import acm.program.*;
    import java.awt.*;
    
    public class ProgramHierarchy extends GraphicsProgram {	
    	
    	private static final int BOX_WIDTH = 120;
    
    	private static final int BOX_HEIGHT = 36;
    	
    	private static final int MIDDLE_LINE_HEIGHT = 36;
    	
    	private static final int GAP_BETWEEN_BOXES = 18;
    
    	public void run() {
    
    		placeMiddleLine();
    		placeTopBox();
    		
    	}
    	
    	private void placeMiddleLine() {
    		
    		GLine middleLine = new GLine (getWidth()/2, (getHeight()/2) - (MIDDLE_LINE_HEIGHT)/2 , getWidth()/2, (getHeight()/2) + (MIDDLE_LINE_HEIGHT)/2);
    		add(middleLine);
    		
    	}
    	
    	private void placeTopBox() {
    		
    		GRect topBox = new GRect ((getWidth()/2)-(BOX_WIDTH/2), ((getHeight()/2) - (BOX_HEIGHT)/2)-BOX_HEIGHT , BOX_WIDTH, BOX_HEIGHT);
    		add(topBox);
    		GLabel topLabel = new GLabel ("Program");
    		topLabel.setLocation((getWidth()/2) - (topLabel.getWidth()/2), (getHeight()/2) - (MIDDLE_LINE_HEIGHT/2) - (BOX_HEIGHT/2) + [COLOR="Red"](topLabel.getAscent()/2[/COLOR]));
    		add(topLabel);
    	}
    }
    also, in the pdf it says this

    The labels should be centered in their boxes. You can find the width of a label by calling label.getWidth() and the height it extends above the baseline by calling label.getAscent(). If you want to center a label, you need to shift its origin by half of these distances in each direction.
    and to me that last part doesnt make sense. to me it should read something like

    If you want to center a label, you need to shift its origin by half of these distances in each direction relative to...
    im not sure if these two issues are directly connected, but help on either one would be appreciated :)

  2. #2
    Brain_Child is offline Member
    Join Date
    Nov 2009
    Posts
    5
    Rep Power
    0

    Default

    I tried replacing

    Java Code:
    topLabel.getAscent()/2
    with
    Java Code:
    (topLabel.getAscent()-topLabel.getDescent())/2
    and now the label looks centered. is this the proper method?

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,680
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Brain_Child View Post
    I tried replacing

    Java Code:
    topLabel.getAscent()/2
    with
    Java Code:
    (topLabel.getAscent()-topLabel.getDescent())/2
    and now the label looks centered. is this the proper method?
    Yep, a font is described by three sizes: an ascent and a descent and a font character width; the first two are vertical measurements and the last one is a horizontal measurement. Sometimes the descent is measured by negative numbers, sometimes by positive numbers; check your manuals.

    kind regards,

    Jos

  4. #4
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    hey Brain_Child did I play with you and SoulSlayer last night on Modern Warfare 2? I'm pretty sure that's the same moniker he was using.
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

Similar Threads

  1. Centering inside a JFrame
    By kahaj in forum AWT / Swing
    Replies: 9
    Last Post: 09-23-2009, 08:23 PM
  2. Replies: 9
    Last Post: 07-21-2009, 03:41 AM
  3. Centering text of output
    By dch414 in forum New To Java
    Replies: 2
    Last Post: 10-02-2008, 11:08 PM
  4. How To Add A Jbutton Inside A Rectangle
    By SANDY_INDIA in forum AWT / Swing
    Replies: 9
    Last Post: 07-06-2008, 09:06 AM
  5. help with rectangle class
    By darkgt in forum New To Java
    Replies: 7
    Last Post: 11-14-2007, 07:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •