?
Solved

How to use stringWidth(String str)

Posted on 1999-07-15
5
Medium Priority
?
266 Views
Last Modified: 2012-06-22
Hi,
I want to use the method public int stringWidth(String str), from java.awt.FontMetrics, to check how wide a String gets when I draw it to the display.

So if I have the following code:
Font f = new Font("Times New Roman", 0, 20);
setFont(f);
String str = "Hello World";
int string_width;

How can I get the width of which the string is drawn?

I have tried:
string_width = stringWidth(str); //error: Undefined name stringWidth

I also tried to make an instance of FontMetrics
FontMetrics fm = new FontMetrics(f);//error: Cannot 'new' an instance of abstract class 'FontMetrics'


0
Comment
Question by:Krafer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 2

Expert Comment

by:mwibbels
ID: 1246140
java.awt.Font f = ...
java.awt.FontMetrics fontMetrics = java.awt.Toolkit.getDefaultToolkit().getFontMetrics(f);

or (if you've got a reference to a Component):

java.awt.FontMetrics fontMetrics = c.getToolkit().getFontMetrics(f);

where c is the Component reference.

0
 
LVL 8

Expert Comment

by:diakov
ID: 1246141
FontMetrics fm = getToolkit().getFontMetrics(font);
int w = fm.stringWidth("asdasd");

Cheers,
  Nik
0
 
LVL 8

Expert Comment

by:diakov
ID: 1246142
OOPS! please reject, I didn't see that somebody else answered.

Nik

P.S. Sorry MWibbels.
0
 

Author Comment

by:Krafer
ID: 1246143
Thanks to both of you.
mwibbels please lock the question so I can grade your answer.
0
 
LVL 2

Accepted Solution

by:
mwibbels earned 320 total points
ID: 1246144
Ok, thanks.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

718 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