Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: centering a label inside a rectangle

  1. #1
    Junior Member
    Join Date
    Nov 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default centering a label inside a rectangle

    hey,

    I'm trying to complete q3 from this pdf.

    http://www.stanford.edu/class/cs106a...signment-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.

    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
    Junior Member
    Join Date
    Nov 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: centering a label inside a rectangle

    I tried replacing

    topLabel.getAscent()/2
    with
    (topLabel.getAscent()-topLabel.getDescent())/2

    and now the label looks centered. is this the proper method?

Similar Threads

  1. count components inside a JPanel
    By dewboy3d in forum AWT / Java Swing
    Replies: 1
    Last Post: August 2nd, 2009, 03:17 PM
  2. Creating and displaying a JPanel inside another JPanel
    By JayDuck in forum AWT / Java Swing
    Replies: 1
    Last Post: April 7th, 2009, 08:02 AM
  3. Dropping to graphic element dragged from JList
    By tua1 in forum AWT / Java Swing
    Replies: 1
    Last Post: November 29th, 2008, 07:22 AM