Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Chicken or Egg: Drawing Java String's

Chicken or Egg: Drawing Java String's
Thread Tools
Ghoser777
Professional Poster
Join Date: Dec 2000
Location: Chicago, Illinois
Status: Offline
Reply With Quote
Jan 10, 2004, 12:09 AM
 
I'm working on project in Java where I want to draw Strings. So say I have a String object and a Font that I want to draw it in. No problem - in my paint(Graphics) method I'll just set the font and call drawString().

Unfortunately, I also want to resize my container to fit the String appropriately. To get the required width and height to draw the String in, I need a FontRenderContext, which I can get from a Graphics object. But I can't get my Graphics object until I'm actually drawing. But if I resize my container while redrawing, won't paint get called again? This seems frustrating, as there is an easier way to do this with Cocoa (I need an applet though).

Thanks,
Matt Fahrenbacher
     
beamso
Fresh-Faced Recruit
Join Date: Nov 2001
Location: Melboune, Australia
Status: Offline
Reply With Quote
Jan 10, 2004, 07:28 AM
 
Originally posted by Ghoser777:
I'm working on project in Java where I want to draw Strings. So say I have a String object and a Font that I want to draw it in. No problem - in my paint(Graphics) method I'll just set the font and call drawString().

Unfortunately, I also want to resize my container to fit the String appropriately. To get the required width and height to draw the String in, I need a FontRenderContext, which I can get from a Graphics object. But I can't get my Graphics object until I'm actually drawing. But if I resize my container while redrawing, won't paint get called again? This seems frustrating, as there is an easier way to do this with Cocoa (I need an applet though).

Thanks,
Matt Fahrenbacher
Hi. The java.awt.Component class has a getGraphics() method, so you can get the graphics even if you aren't in the paint method. You can also get the Font for the component, using the getFont() method, and then use this to get the font metrics for this class using the getFontMetrics() method. The font metrics class will should give you a similar figure to that of the FontRenderContext class. (I can't confirm that last bit because I've never used FontRenderContext, but I have used the FontMetrics class)
     
Ghoser777  (op)
Professional Poster
Join Date: Dec 2000
Location: Chicago, Illinois
Status: Offline
Reply With Quote
Jan 10, 2004, 02:17 PM
 
Ah ha! Didn't realize it had that method. That fixes everything.

Thanks,
Matt Fahrenbacher
     
absmiths
Mac Elite
Join Date: Sep 2000
Location: Edmond, OK USA
Status: Offline
Reply With Quote
Jan 15, 2004, 05:29 PM
 
Originally posted by Ghoser777:
Ah ha! Didn't realize it had that method. That fixes everything.

Thanks,
Matt Fahrenbacher
I don't generally like to mess with the Graphics object outside of paint() or update(), so I usually just use Toolkit.getFontMetrics() to do it. It is deprecated now, but I think that is just in multiple monitor environments.
     
   
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 08:03 PM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,