在Java Swing中缩放JPanel [英] Zoom JPanel in Java Swing

查看:1508
本文介绍了在Java Swing中缩放JPanel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个现有的 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 (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.

希望这会有所帮助。

这篇关于在Java Swing中缩放JPanel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆