• 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

scaling a font when resizing by dragging

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am using g.drawString() in paintComponent() to display a text string on a JPanel. I am resizing the JPanel by dragging it on a canvas (another JPanel). I want the text string to resize as well. Is there a way to do this so that the displayed string stays within the bounds of the JPanel. The text string has to increase when the dragged JPanel increases and decrease when the dragged JPanel decreases in size. In other words, the bounds of the text string has to change more or less in proportion to the bounds of the dragged JPanel.

Thanks for your help.
 
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by jack swallow:
I am using g.drawString() in paintComponent() to display a text string on a JPanel. I am resizing the JPanel by dragging it on a canvas (another JPanel). I want the text string to resize as well.



There are several ways to do this. Three come to mind:

1. call g.setFont() to set a larger or smaller font (could use g.getFontMetrics().charsWidth() to test font size)

2. call ((Graphics2D)g).scale()

3. replace the JPanel with a JSVGCanvas

The first two may require code to detect resizing. The last one depends on an external library.
[ July 13, 2007: Message edited by: Brian Cole ]
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Brian Cole
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mr. Wood's implementation uses approach #1.

It could be modified to use approach #2 like this:


[edit: I'm not sure you really need the setRenderingHint() stuff.]
[ July 13, 2007: Message edited by: Brian Cole ]
reply
    Bookmark Topic Watch Topic
  • New Topic