19

I have an existing Java Swing application. In the middle of the application is a single JPanel which I want to be able to zoom in and out of. The JPanel has a number of JComponents on it (mostly other JPanels and JLabels).

Also mouse position will need to be adjusted appropriately as well - so mouseevents need to remain same even after the JPanel has been zoomed. As such simply changing the paint methods of each component doesn't seem plausible.

EDIT:

enter image description here

OK i kind of got it working using the MagnifierUI class with some minor edits. However the magnified panel I create has the wrong mouseevents - i.e. the panel is scaled, mouseevents are not.

subodh
  • 6,136
  • 12
  • 51
  • 73
sboy031
  • 205
  • 2
  • 5
  • JXLayer can do this. Getting hold of an example (or even JXLayer itself) is problematic now days – MadProgrammer Feb 13 '13 at 04:33
  • I should mention, I have tried the following methods 1) [Utils4Swing5](http://javadeveloperslife.wordpress.com/2009/07/18/zooming-swing/) which does not work at all. 2) I have also attempted Peit Blok's method using JXLayer and PBTransform - however Peit Blok appears to have left the planet and I can no longer find the jar necessary for PBTransform [link](http://www.java.net/node/700255) – sboy031 Feb 13 '13 at 04:34
  • 1
    *"Any suggestions?"* Leave this to user choice of screen resolution or the OS' inbuilt screen magnification utility. If the user needs it, they'll know where to find it. – Andrew Thompson Feb 13 '13 at 04:44
  • @MadProgrammer yep I have tried using JXLayer (and JLayer) but TransformUI appears to be the only UI class that can be used with JXLayer to zoom and it is strangely absent from the internet (as Peit Blok, its developer, appears to have shut up shop) – sboy031 Feb 13 '13 at 04:47
  • 5
    I've had it packaged it [here](https://www.dropbox.com/s/re1hmvypp19oqy1/JXLayer-PBJar-Demo.zip). Check out the `TestWrapped` demo in PB's Jar... – MadProgrammer Feb 13 '13 at 04:48
  • @MadProgrammer Sweet! I'll give it a try, report back later today - exactly what I was looking for! – sboy031 Feb 13 '13 at 04:51

2 Answers2

5

This is just a scetch:

  • in your JPanel keep track of an AffineTransform which represents the scale factor (see AffineTransform.scale(double,double),
  • override the paint method of your JPanel: before calling super.paint apply the affine transformation to your Graphics2D object (cast from the parameter of the paint method) by calling Graphics2D.setTransform(AffineTransform), call super.paint afterwards
  • override the methods processMouseEvent, processMouseMotionEvent and processMouseWheelEvent, apply the affine transformation to the coordinates of their mouse event parameter (AffineTransform.transform(java.awt.geom.Point2D,java.awt.geom.Point2D)), call respective super-method afterwards.

Hope this helps.

Claude
  • 1,724
  • 3
  • 17
  • 46
1

Try SwingUtilities.convertPoint(source, point,destination);

shinds
  • 756
  • 7
  • 13