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:
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.
Try SwingUtilities.convertPoint(source, point,destination);
This is just a scetch:
JPanel
keep track of an AffineTransform which represents the scale factor (seeAffineTransform.scale(double,double)
,paint
method of yourJPanel
: before callingsuper.paint
apply the affine transformation to yourGraphics2D
object (cast from the parameter of thepaint
method) by callingGraphics2D.setTransform(AffineTransform)
, callsuper.paint
afterwardsprocessMouseEvent
,processMouseMotionEvent
andprocessMouseWheelEvent
, apply the affine transformation to the coordinates of their mouse event parameter (AffineTransform.transform(java.awt.geom.Point2D,java.awt.geom.Point2D)
), call respectivesuper
-method afterwards.Hope this helps.