wood burning stoves*
The moose likes Swing / AWT / SWT and the fly likes Centering text on object drawn with Graphics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Centering text on object drawn with Graphics" Watch "Centering text on object drawn with Graphics" New topic
Author

Centering text on object drawn with Graphics

Michael Boehm
Ranch Hand

Joined: Jun 02, 2010
Posts: 51
Ok, Graphics is not such an easy class :S

I have a a derived JPanel inside a JFrame. In the Panel I have a circle drawn using drawOval in Graphics which takes the x- and y-coords for the upper left point in the box bounding the oval and with and height. I need to write a single character in the center of the drawn circle. Is there any methods which can assist in doing this precisely or do I have to "hack around" ?
Martin Vanyavchich
Ranch Hand

Joined: Sep 16, 2008
Posts: 241
hack around


SCJP 6, OCMJD 6, OCPJWSD 6
I no good English.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

Create an Icon that draws the circle, set the Icon and text to a JLabel with appropriate horizontal and vbertical text position and alignments. No hacking at all. Something like (uncompiled, untested, imports omitted)JLabel = new JLabel(text, new CircleIcon(diameter), JLabel.CENTER);
label.setHorizontalTextPosition(JLabel.CENTER);
label.setVerticalTextPosition(JLabel.CENTER);[/code]Apologies for any and all typos.


luck, db
There are no new questions, but there may be new answers.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19654
    
  18

Class FontMetrics will return the width of Strings when painted using its matching Font.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Michael Boehm
Ranch Hand

Joined: Jun 02, 2010
Posts: 51
Thanks for the hints !
This
Rob Prime wrote:Class FontMetrics will return the width of Strings when painted using its matching Font.

lead me to the getStringBounds method in FontMetrics. Very useful for this particular problem, now I just have to do a bit of elementary school trig
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38025
    
  22
Moving to our GUIs forum, where we usually discuss such questions.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> now I just have to do a bit of elementary school trig

a circle is just a square with rounded corners,
so it shouldn't be that hard to get height/2, width/2
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
For my money, Darryl's solution is the laziest, and I'm all about effective but lazy programming.
 
 
subject: Centering text on object drawn with Graphics
 
Similar Threads
how do i find points along the circumferance of a circle
color detection with mouse
hi please help with conversion from awt to swing
awt question
Is this possible to do?