在面板内放大和缩小
我有一个面板,其中一些 2D 对象正在移动。我已根据需要重写了paintComponent()。现在我希望能够放大和缩小该区域。放大时,会出现滚动条,通过滚动条可以查看整个字段。放大和缩小时,2D 对象的尺寸应相应增大或减小。哪个 Swing 组件或者组件组合将有助于实现这一目标?
I have a Panel in which some 2D objects are moving about. I have overridden paintComponent() as necessary. Now I want to be able to zoom in and zoom out that area. When zooming in, scrollbars will appear by which one can view the entire field by scrolling. While zooming in and out, the 2D objects should increase or decrease in size accordingly. Which Swing component or rather combination of components will help to achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最简单的方法是修改面板并引入双精度来指示缩放级别。该双精度值表示您的比例,其中 1 为正常比例,更高为放大比例。您可以在
paintComponent
中将该双精度值与Graphics2D
一起使用。例如:
对于滚动,将面板放入 JScrollPane 中,并将其与也使用缩放比例的 getPreferredSize 结合起来。 JScrollPane 使用您放入其中的组件的首选大小。如果首选大小超过其自身大小,它将显示滚动条。
如果您更改面板的首选大小,以便缩放它返回的宽度和高度,您应该没问题。基本上你可以返回类似的东西:
Easiest way is to modify your panel and introduce a double indicating your zoom level. This double would indicate your scale, where 1 is normal and higher is zoomed in. You can use that double together with
Graphics2D
in yourpaintComponent
.Such as:
For the scrolling, put your panel in a JScrollPane and combine that with a getPreferredSize that also uses your zoom scale. JScrollPane uses the preferred size of the component you put in it. It will show scrollbars if the preferred size exceeds its own size.
If you change the preferred size of your panel so that the width and height it returns is scaled you should be fine. Basically you can just return something like:
我知道这个问题已经很老了,但我想我可以发布我的解决方案,以防它对将来的某人有用。
因此,我创建了一个扩展 JPanel 的类,该类实现了 MouseWheelListener,以便检测用户何时滚动鼠标。我的类还监听拖动,以便在用户单击和拖动时移动内容。
代码说明
首先,在构造函数中,您必须将其设置为 MouseWheelListener
对于放大和缩小,我使用了布尔型
zoomer
(指示用户何时滚动鼠标)和两个双精度zoomFactor
(保留对象大小相乘的当前系数)和prevZoomFactor
(用于先前的缩放系数)。我还重写了 JPanel 的
paint()
方法,其中(在绘制任何内容之前)当用户缩放 (zoomer
=true) 时,我按缩放图形>缩放因子
。代码:最后,我重写 MouseWheelListener 的
mouseWheelMoved
方法,在该方法中,我增加zoomFactor
(如果用户向上滚动)或减小zoomFactor
>(如果用户向下滚动)。代码:工作示例
如果您还想使用拖动功能并希望根据鼠标的位置进行缩放,则可以使用下面的类,该类在构造函数中获取 BufferedImage 作为参数,以便在屏幕上显示某些内容。
我还在 GitHub 上上传了一个名为 Zoomable-Java-Panel 其中有一个我上面展示的功能示例,您可以测试并查看如何将其实现到项目中。
I know this question is old, but I thought I could post my solution in case it could be useful for someone in the future.
So, I created a class that extends JPanel which implements the MouseWheelListener in order to detect when the user rolls the mouse. My class also listens for dragging in order to move the contents when the user clicks and drags.
Code Explanation
First, in the constructor you must set this as the MouseWheelListener
For the zoom in and out I used a boolean
zoomer
(to indicate when the user rolls with the mouse) and two doubleszoomFactor
(to keep the current factor by which the objects' sizes are multiplied) andprevZoomFactor
(for the previous zoom factor).I also override the
paint()
method of the JPanel, in which (before drawing anything) when the user zooms (zoomer
=true) I scale the graphics by thezoomFactor
. Code:Finally, I override the
mouseWheelMoved
method of the MouseWheelListener, in which I increase thezoomFactor
(if the user rolls up) or decrease thezoomFactor
(if the user rolls down). Code:Working Example
If you also want to use the drag function and want to zoom according to the position of the mouse, you can use the class below, which gets a BufferedImage as a parameter in the constructor in order to display something on screen.
I have also uploaded a project on GitHub called Zoomable-Java-Panel in which there is a functional example of what I showed above, which you can test and see how it can be implemented into a project.