This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Determining width and height of a String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Determining width and height of a String" Watch "Determining width and height of a String" New topic
Author

Determining width and height of a String

kanaka tam
Ranch Hand

Joined: Jan 19, 2004
Posts: 42
Hello Folks,
I have written an application that manages the x and y position of a String object through two seperate JSlider objects. The Display Area for the String object is a subclass of the JPanel class. The content of the String object is updated through a JButton and JTextField objects. I was successful in writing the code up until where i could get the String from TextField and move it around using the Horizontal and vertical sliders. My problem here is as i move the sliders to it maximum value the string disappears out of the drawing area which according to my program should not disappear from the drawing area. Are there any methods that would keep the String from not disappearing the drawing area. Below is my code: It is very lengthy. My code compiles fine and executes too except for the above problem.




Thank you,
kt
[ July 24, 2005: Message edited by: kanaka tam ]
Manuel Moons
Ranch Hand

Joined: Mar 05, 2002
Posts: 229
You can determine the width an height of a string using the FontMetrics class.

An instance of FontMetrics can be acquired using the getFontMetrics method on component. You should pass the font that you are using to paint on you component (getFont). Then you can use the getStringBounds method to calculate the width and height of a string.

I do not know if this helps you out but it answers the question in your message header.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Determining width and height of a String
 
Similar Threads
using a bean to put slider on panel
Have I got my inner classes and variable declarations in the right places?
AWT and Threads
how to implement non rectangular components e.g.Circle
Prevent resizing of JPanel in BorderLayout