views:

303

answers:

2

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

+1  A: 

You could give a try to the JXLayer library.

There are several tools in it, which could help you to make a zoom. Check the examples shown here. I would recommend you to read more about the TransformUI, from this library. From the example, it seems like it could help solving your problem.

Gnoupi
Thanks for your response! This seems a very good way to do it.I'm leaving the question open for another few hours in case there are other people who are able to contribute to this topic but are scared of questions that are marked as "answered" :-)
Shirky
@Shirky - there is no emergency in accepting, especially if someone comes with something better ;)
Gnoupi
Time to say goodbye ;-) . I'm closing the question, your answer was exactly what I was looking for. Thanks.
Shirky
+1  A: 

Scaling the view is easy enough; transforming mouse coordinates is only slightly more difficult. Here's an elementary example. I'd keep JComponents out, although it might make sense to develop an analogous ScaledComponent that knows about the geometry. That's where @Gnoupi's suggestion of using a library comes in.

trashgod
Thanks for your answer. I was looking for a general-purpose approach. Adjusting single components (the article you linked to) is another problem.
Shirky