Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 1998-05-11
5
Medium Priority
?
309 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 200 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

972 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