aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes scaling a font when resizing by dragging Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "scaling a font when resizing by dragging" Watch "scaling a font when resizing by dragging" New topic
Author

scaling a font when resizing by dragging

Rob Byrd
Greenhorn

Joined: Jul 13, 2007
Posts: 12
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

Joined: Sep 20, 2005
Posts: 862
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 ]

bitguru blog
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: scaling a font when resizing by dragging