Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 5th, 2005, 5:42 PM   #1
Writlaus
Hobbyist Programmer
 
Writlaus's Avatar
 
Join Date: Nov 2005
Posts: 149
Rep Power: 3 Writlaus is on a distinguished road
drawString size?

I am creating my own button class in an applet, and I'm planning to use the g.drawString() method, but I need to know the size of the text when it's drawn to know how big to draw the border around it. Are there any methods that return the size a given string would be if it were to be drawn?
Writlaus is offline   Reply With Quote
Old Dec 5th, 2005, 5:55 PM   #2
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
I know that the FontMetrics class can help find the size of characters -- don't know if that helps you at all.
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old Dec 5th, 2005, 5:55 PM   #3
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 856
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
If you are extending JButton, you can use the setFont(Font f) method, for example:
setFont(new Font("Helvetica", Font.PLAIN, 24))
titaniumdecoy is offline   Reply With Quote
Old Dec 5th, 2005, 9:23 PM   #4
groovicus
Programmer
 
Join Date: Nov 2004
Posts: 84
Rep Power: 4 groovicus is on a distinguished road
Ditto on the Font Metrics. This might help you a little bit:
http://www.ictp.trieste.it/~manuals/...awingText.html
__________________
HijackThis Team-SFDC
groovicus is offline   Reply With Quote
Old Dec 5th, 2005, 10:30 PM   #5
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 856
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
I think buttons automatically resize themselves depending on the length of their text, so if you don't specify a size you may be able to use the setBorder(Border b) method and save yourself the trouble.
titaniumdecoy is offline   Reply With Quote
Old Dec 5th, 2005, 11:46 PM   #6
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,038
Rep Power: 5 lectricpharaoh will become famous soon enough
Quote:
Originally Posted by titaniumdecoy
I think buttons automatically resize themselves depending on the length of their text, so if you don't specify a size you may be able to use the setBorder(Border b) method and save yourself the trouble.
I think what the OP means is he wants to do what is referred to in Windows as 'owner draw'. He's not setting the text through the setText() method; he's overriding the paintComponent() method, and doing custom painting of the control.
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is offline   Reply With Quote
Old Dec 6th, 2005, 6:35 PM   #7
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 856
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
If what you want is a button that is simply a color background with a border and text, all you need to do is add the code:

setBackground(Color.white);
setBorder(BorderFactory.createLineBorder (Color.black, 1));
titaniumdecoy is offline   Reply With Quote
Old Dec 7th, 2005, 3:10 PM   #8
Writlaus
Hobbyist Programmer
 
Writlaus's Avatar
 
Join Date: Nov 2005
Posts: 149
Rep Power: 3 Writlaus is on a distinguished road
I'm using the fontMetrics thing right now, and it's working great =) thanks.
Writlaus is offline   Reply With Quote
Old Dec 7th, 2005, 5:59 PM   #9
Writlaus
Hobbyist Programmer
 
Writlaus's Avatar
 
Join Date: Nov 2005
Posts: 149
Rep Power: 3 Writlaus is on a distinguished road
I had a related question, though... Is there a way to underline certain letters in string when drawing it?
Writlaus is offline   Reply With Quote
Old Dec 7th, 2005, 6:12 PM   #10
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
Can't you just make a new Font that automatically underlines?
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting 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
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:04 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC