Results 1 to 4 of 4
- 11-19-2009, 12:46 PM #1
Member
- Join Date
- Nov 2009
- Posts
- 5
- Rep Power
- 0
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.
also, in the pdf it says thisJava 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); } }
and to me that last part doesnt make sense. to me it should read something likeThe 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.
im not sure if these two issues are directly connected, but help on either one would be appreciated :)If you want to center a label, you need to shift its origin by half of these distances in each direction relative to...
- 11-19-2009, 02:44 PM #2
Member
- Join Date
- Nov 2009
- Posts
- 5
- Rep Power
- 0
I tried replacing
withJava Code:topLabel.getAscent()/2
and now the label looks centered. is this the proper method?Java Code:(topLabel.getAscent()-topLabel.getDescent())/2
- 11-19-2009, 03:41 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
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
- 11-19-2009, 05:22 PM #4
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
-
Centering inside a JFrame
By kahaj in forum AWT / SwingReplies: 9Last Post: 09-23-2009, 07:23 PM -
problem in displaying text inside the item label - JFREECHART
By chittora in forum Java 2DReplies: 9Last Post: 07-21-2009, 02:41 AM -
Centering text of output
By dch414 in forum New To JavaReplies: 2Last Post: 10-02-2008, 10:08 PM -
How To Add A Jbutton Inside A Rectangle
By SANDY_INDIA in forum AWT / SwingReplies: 9Last Post: 07-06-2008, 08:06 AM -
help with rectangle class
By darkgt in forum New To JavaReplies: 7Last Post: 11-14-2007, 06:19 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks