Zoom in Java Swing application

Posted by Shirky on Stack Overflow See other posts from Stack Overflow or by Shirky
Published on 2010-05-31T15:20:29Z Indexed on 2010/05/31 15:23 UTC
Read the original article Hit count: 142

Filed under:
|
|
|
|

Hi there,

I am looking for ways to zoom in a Java Swing application. That means that I would like to resize all components in a given JPanel by a given factor as if I would take an screenshot of the UI and just applied an "Image scale" operation. The font size as well as the size of checkboxes, textboxes, cursors etc. has to be adjusted. It is possible to scale a component by applying transforms to a graphics object:

 protected Graphics getComponentGraphics(Graphics g) {
  Graphics2D g2d=(Graphics2D)g;

  g2d.scale(2, 2);

  return super.getComponentGraphics(g2d);
 }

That works as long as you don't care about self-updating components. If you have a textbox in your application this approach ceases to work since the textbox updates itself every second to show the (blinking) cursor. And since it doesn't use the modified graphics object this time the component appears at the old location. Is there a possibility to change a components graphics object permanently? There is also a problem with the mouse click event handlers. The other possibility would be to resize all child components of the JPanel (setPreferredSize) to a new size. That doesn't work for checkboxes since the displayed picture of the checkbox doesn't change its size. I also thought of programming my own layout manager but I don't think that this will work since layout managers only change the position (and size) of objects but are not able to zoom into checkboxes (see previous paragraph). Or am I wrong with this hypothesis? Do you have any ideas how one could achieve a zoomable Swing GUI without programming custom components? I looked for rotatable user interfaces because the problem seems familiar but I also didn't find any satisfying solution to this problem.

Thanks for your help, Chris

© Stack Overflow or respective owner

Related posts about java

Related posts about gui