13

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

Shirky
  • 347
  • 2
  • 3
  • 14
  • scaling already rendered components containing things like anti-aliased fonts will give shitty results, especially if those fonts are using RGB-decimation/sub-pixel anti-aliasing. – NoozNooz42 May 31 '10 at 15:41

5 Answers5

9

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
  • 4,715
  • 5
  • 34
  • 50
  • 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 May 31 '10 at 16:47
  • @Shirky - there is no emergency in accepting, especially if someone comes with something better ;) – Gnoupi Jun 01 '10 at 13:49
  • Time to say goodbye ;-) . I'm closing the question, your answer was exactly what I was looking for. Thanks. – Shirky Jun 01 '10 at 19:37
  • 1
    The library and the example sites are down. Can you explain how this library solved the problem ? – Robert Jun 19 '17 at 13:25
  • 1
    @Robert someone uploaded the utility classes and example project on github: https://github.com/edolganov/knowledge-collector/tree/master/ver2/trunk/labs/zoom-with-jxpanel-src. Also, there seem to be a link to a dropbox archive as well (as referred to in the comments under this other question: https://stackoverflow.com/questions/14846402/zoom-jpanel-in-java-swing). Hope that helps. – Gnoupi Jun 22 '17 at 09:12
  • 1
    Also, the JXLayer project mostly got integrated in Java 7 as JLayer. So the examples might be compatible with that. – Gnoupi Jun 22 '17 at 09:14
7

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
  • 203,806
  • 29
  • 246
  • 1,045
  • 1
    Thanks for your answer. I was looking for a general-purpose approach. Adjusting single components (the article you linked to) is another problem. – Shirky Jun 01 '10 at 19:36
2

You might find Piccolo2D.java API useful: http://code.google.com/p/piccolo2d/

It is very simple.

It touts in particular its smooth zooming. You essentially make a "canvas" that can contain various elements, and can then zoom by just holding right-click and panning the mouse back and forth.

I worked on a team that used it to create this: http://sourceforge.net/apps/mediawiki/guitar/index.php?title=WebGuitar#EFG.2FGUI_Visualizer

The nodes you see there are clickable links themselves.

cellepo
  • 4,001
  • 2
  • 38
  • 57
2

hey you can try this if you want to zoom a image like any other image viewer the use a JPanel draw an image using drawImage() method now create a button and when you click the button increase the size of the panel on the frame it appears as if the image is being viewed in Zoom

DjRocks
  • 21
  • 1
0

Since Java 9, there are VM arguments (actually meant to be used for high dpi scaling) that can render a application with a higher scaling factor:

java -Dsun.java2d.uiScale=2.0 -jar MyApplication.jar

Or:

java -Dsun.java2d.win.uiScaleX=2.0 -Dsun.java2d.win.uiScaleY=2.0 -jar MyApplication.jar
wknauf
  • 160
  • 1
  • 7