在Java Swing中缩放JPanel [英] Zoom JPanel in Java Swing
问题描述
我有一个现有的 Java Swing
应用程序。在应用程序的中间是一个 JPanel
,我希望能够放大和缩小
。 JPanel
上有一些 JComponents
(主要是其他 JPanels
和 JLabels
)。
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
).
此外鼠标
位置也需要适当调整 - 所以 mouseevents
即使在 JPanel
被缩放后也需要保持相同。因此,只需更改每个组件
的 paint
方法似乎不合理。
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.
编辑:
好的我使用 MagnifierUI
class
进行一些小修改。但是我创建的放大面板有错误的 mouseevents
- 即面板是缩放的, mouseevents
不是。
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.
推荐答案
这只是一个方面:
- 在你的
JPanel
中跟踪 AffineTransform 表示比例因子(参见AffineTransform.scale(double,double)
, - 在调用
super.paint <之前覆盖
JPanel
的paint
方法: / code>将仿射变换应用于Graphics2D
对象(从paint
方法的参数强制转换)通过调用Graphics2D.setTransform(AffineTransform)
,之后调用super.paint
- 覆盖方法
processMouseEvent
,processMouseMotionEvent
和processMouseWheelEvent
,将仿射变换应用于其鼠标事件参数的坐标(AffineTransform.transform(java.awt.geom.Point2D,java.awt.geom.Point2D)
),调用相应的super
-method之后。
- in your
JPanel
keep track of an AffineTransform which represents the scale factor (seeAffineTransform.scale(double,double)
, - override the
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
afterwards - override the methods
processMouseEvent
,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.
希望这会有所帮助。
这篇关于在Java Swing中缩放JPanel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!