Zoom JPanel in Java Swing

2019-03-18 01:40发布

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.

2条回答
叛逆
2楼-- · 2019-03-18 02:16

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

查看更多
贼婆χ
3楼-- · 2019-03-18 02:17

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.

查看更多
登录 后发表回答