This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

scaling a font when resizing by dragging

 
Rob Byrd
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • 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.
 
Brian Cole
Author
Ranch Hand
Posts: 908
1
  • Mark post as helpful
  • send pies
  • 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 ]
 
Craig Wood
Ranch Hand
Pie
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Brian Cole
Author
Ranch Hand
Posts: 908
1
  • Mark post as helpful
  • send pies
  • 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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic