Solved

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

Posted on 1998-05-11
5
277 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
matchUp  challenge 9 71
ArrayLists in ArrayLists/HashMaps? 4 38
wordappend challenge 8 82
github account with ecipse 1 15
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…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

763 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

12 Experts available now in Live!

Get 1:1 Help Now