Java-Gaming.org Java4K winners: [ by our judges | by the community ]         
Featured games (67)
games approved by the League of Dukes
Games in Showcase (∞)
games submitted by our members



News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  Print  
  Pixel length of string  (Read 311 times)
0 Members and 1 Guest are viewing this topic.
Offline JESTERRRRRR

Jr. Member
**

Posts: 63
Medals: 1



« on: 2012-01-27 00:42:40 »

Hey,

I am programming a multiplayer game with a lobby system. In the lobby I draw a picture that looks like a chatbox, and over it I draw a StringBuffer, using .append(char) to add letters as they are typed.

My problem is that if I type 'WWWWWWWWWWWWWWWWW' it may reach the edge of the box at say 15 chars, so I'd want it to be stopped. If I type 'iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii' it'll be more like 25 chars, so I can't limit it by length(). Seems a pretty simple problem, any ideas?

EDIT: Nevermind, literally just found it: SwingUtilities.computeStringWidth( fontMetrics, stringToComputeTheWidth ); 
Offline Damocles

Sr. Member
**

Posts: 264
Medals: 14



« Reply #1 on: 2012-01-27 00:58:16 »

Have a look at this site

http://www.javadocexamples.com/java/awt/FontMetrics/stringWidth%28String%20str%29.html

you can use something like

FontMetrics fm = rendComp.getFontMetrics(rendComp.getFont());

int stringWide=fm.stringWidth("WAWAWIii");
int charWide=fm.charWidth('O');

Offline ra4king

JGO Kernel
*****

Posts: 3160
Medals: 196


I'm the King!


« Reply #2 on: 2012-01-28 13:34:24 »

Note rendComp has to be of type java.awt.Graphics2D Smiley

Pages: [1]
  Print  
 
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.104 seconds with 20 queries.