简体   繁体   English

在Java Swing中缩放JPanel

[英]Zoom JPanel in Java Swing

I have an existing Java Swing application. 我有一个现有的Java Swing应用程序。 In the middle of the application is a single JPanel which I want to be able to zoom in and out of. 在应用程序的中间是一个JPanel ,我希望能够zoom in and out The JPanel has a number of JComponents on it (mostly other JPanels and JLabels ). JPanel上有许多JComponents (主要是其他JPanelsJLabels )。

Also mouse position will need to be adjusted appropriately as well - so mouseevents need to remain same even after the JPanel has been zoomed. 此外mouse位置需要进行适当的调整,以及-所以mouseevents需要保持相同的偶数后JPanel已被放大。 As such simply changing the paint methods of each component doesn't seem plausible. 因此,简单地改变每个componentpaint方法似乎不合理。

EDIT: 编辑:

在此输入图像描述

OK i kind of got it working using the MagnifierUI class with some minor edits. 好吧,我有点使用MagnifierUI class进行一些小编辑。 However the magnified panel I create has the wrong mouseevents - ie the panel is scaled, mouseevents are not. 然而,放大板创建有错误的mouseevents -即面板进行缩放, mouseevents都没有。

This is just a scetch: 这只是一个问题:

  • in your JPanel keep track of an AffineTransform which represents the scale factor (see AffineTransform.scale(double,double) , 在您JPanel密切跟踪的AffineTransform代表比例因子(见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 覆盖JPanelpaint方法:在调用super.paint之前,通过调用Graphics2D.setTransform(AffineTransform)将仿射变换应用于Graphics2D对象(从paint方法的参数super.paint Graphics2D.setTransform(AffineTransform) ,然后调用super.paint
  • 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. 覆盖方法processMouseEventprocessMouseMotionEventprocessMouseWheelEvent ,将仿射变换应用于其鼠标事件参数的坐标( AffineTransform.transform(java.awt.geom.Point2D,java.awt.geom.Point2D) ),之后调用相应的super方法。

Hope this helps. 希望这可以帮助。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM