• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Determining width and height of a String

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic