Solved

java.awt.Graphics.drawString() question...

Posted on 1998-05-11
5
281 Views
Last Modified: 2010-04-16
I have a panel which I draw a 3D rect on to give it the effect of a sunken border.  I also want to draw a String in the center of this rectangle.  I always want the String to be centered.  How can I determine the x and y position so that I can always draw the String in the center?
0
Comment
Question by:bjg
  • 3
  • 2
5 Comments
 
LVL 16

Expert Comment

by:imladris
ID: 1220914
Does the question centre around the "3Dness" of the rectangle or around the centreing of the string?

Or to put it another way, if the  rectangle were "flat" would you have the same question or not?

0
 

Author Comment

by:bjg
ID: 1220915
Yes, if the rectangle were flat, it would still be the same question.  I want to know how to center the String of text inside the rectangle...
0
 
LVL 16

Accepted Solution

by:
imladris earned 50 total points
ID: 1220916
The width of the string can be found with the stringWidth method of the FontMetrics class. A FontMetric can be constructed with the Font object.

Font f=new Font("Helvetica",Font.PLAIN,12);
FontMetrics fm=new FontMetrics(f);

The stringWidth method's signature is:

public int stringWidth(String str);

It returns the width the string you're interested in in pixels.
So horizontal centreing is a matter of (assuming the rectangle is variable r)

xoff=r.x+(r.width-fm.stringWidth(s))/2;

vertical centring is a bit more of a judgement call due to descenders ascenders etc.  I have found an offset of:

yoff=fm.getAscent()+(r.height-fm.getHeight())/2

to work well.

So the drawString would then be:

g.drawString(s,r.x+xoff,r.y+yoff);


0
 

Author Comment

by:bjg
ID: 1220917
I don't have an actual Rectangle object (r).  I just draw3DRect() on my panel, then I want to drawString() inside the 3DRect, and centered also.  So what do I do about the Rectangle object that you have in your sample?
0
 
LVL 16

Expert Comment

by:imladris
ID: 1220918
The rectangle would be equivalent to the values passed to the draw3DRect (you pass it x,y,width and height). Just substitute those for r.x, r.width etc.

Also, getting the FontMetrics is a bit different than I indicated. Sorry, about that. To get the FontMetrics object you need a Graphics object (which you would have in the paint method). With that you can specify:

FontMetrics fm=g.getFontMetrics(f);

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now