Solved

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

Posted on 1998-05-11
5
286 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
check java version using powershell 13 82
jsp login check 12 28
eclipse console opening separately 2 16
Java: anonymous class 4 17
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
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 …

805 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