Solved

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

Posted on 1998-05-11
5
291 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

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

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

740 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