放大 Java Swing 应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2944442/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Zoom in Java Swing application
提问by Shirky
I am looking for ways to zoom in a Java Swing application. That means that I would like to resize all components in a given JPanel by a given factor as if I would take an screenshot of the UI and just applied an "Image scale" operation. The font size as well as the size of checkboxes, textboxes, cursors etc. has to be adjusted. It is possible to scale a component by applying transforms to a graphics object:
我正在寻找放大 Java Swing 应用程序的方法。这意味着我想通过给定的因素调整给定 JPanel 中所有组件的大小,就好像我要截取 UI 的屏幕截图并应用“图像缩放”操作一样。必须调整字体大小以及复选框、文本框、光标等的大小。可以通过对图形对象应用变换来缩放组件:
protected Graphics getComponentGraphics(Graphics g) {
Graphics2D g2d=(Graphics2D)g;
g2d.scale(2, 2);
return super.getComponentGraphics(g2d);
}
That works as long as you don't care about self-updating components. If you have a textbox in your application this approach ceases to work since the textbox updates itself every second to show the (blinking) cursor. And since it doesn't use the modified graphics object this time the component appears at the old location. Is there a possibility to change a components graphics object permanently? There is also a problem with the mouse click event handlers. The other possibility would be to resize all child components of the JPanel (setPreferredSize) to a new size. That doesn't work for checkboxes since the displayed picture of the checkbox doesn't change its size. I also thought of programming my own layout manager but I don't think that this will work since layout managers only change the position (and size) of objects but are not able to zoom into checkboxes (see previous paragraph). Or am I wrong with this hypothesis? Do you have any ideas how one could achieve a zoomable Swing GUI without programming custom components? I looked for rotatable user interfaces because the problem seems familiar but I also didn't find any satisfying solution to this problem.
只要您不关心自更新组件,它就可以工作。如果您的应用程序中有一个文本框,则此方法将停止工作,因为该文本框每秒更新一次以显示(闪烁)光标。并且由于它这次不使用修改后的图形对象,因此组件出现在旧位置。是否有可能永久更改组件图形对象?鼠标单击事件处理程序也存在问题。另一种可能性是将 JPanel (setPreferredSize) 的所有子组件的大小调整为新的大小。这不适用于复选框,因为复选框的显示图片不会改变其大小。我也想过编写自己的布局管理器,但我没有 不认为这会起作用,因为布局管理器只更改对象的位置(和大小),但不能放大复选框(参见上一段)。还是我对这个假设有误?您知道如何在不编写自定义组件的情况下实现可缩放的 Swing GUI 吗?我寻找可旋转的用户界面,因为这个问题似乎很熟悉,但我也没有找到任何令人满意的解决方案。
Thanks for your help, Chris
谢谢你的帮助,克里斯
采纳答案by Gnoupi
You could give a try to the JXLayer library.
你可以试试JXLayer 库。
There are several tools in it, which could help you to make a zoom. Check the examples shown here. I would recommend you to read more about the TransformUI, from this library. From the example, it seems like it could help solving your problem.
里面有几个工具,可以帮助你进行缩放。检查此处显示的示例。我建议您从这个库中阅读有关 TransformUI 的更多信息。从这个例子来看,它似乎可以帮助解决您的问题。
回答by trashgod
Scaling the view is easy enough; transforming mouse coordinates is only slightly more difficult. Here's an elementary example. I'd keep JComponents out, although it might make sense to develop an analogous ScaledComponentthat knows about the geometry. That's where @Gnoupi's suggestion of using a library comes in.
缩放视图很容易;转换鼠标坐标只是稍微困难一些。这是一个基本的例子。我会JComponent避开 s,尽管开发一个ScaledComponent了解几何的类似物可能是有意义的。这就是@Gnoupi 建议使用库的地方。
回答by DjRocks
hey you can try this if you want to zoom a image like any other image viewer the use a JPanel draw an image using drawImage() method now create a button and when you click the button increase the size of the panel on the frame it appears as if the image is being viewed in Zoom
嘿,如果您想像任何其他图像查看器一样缩放图像,可以尝试此操作,使用 JPanel 使用 drawImage() 方法绘制图像现在创建一个按钮,当您单击该按钮时,会增加框架上面板的大小,它会出现就像在缩放中查看图像一样
回答by cellepo
You might find Piccolo2D.java API useful: http://code.google.com/p/piccolo2d/
您可能会发现 Piccolo2D.java API 很有用:http: //code.google.com/p/piccolo2d/
It is very simple.
这很简单。
It touts in particular its smooth zooming. You essentially make a "canvas" that can contain various elements, and can then zoom by just holding right-click and panning the mouse back and forth.
它特别吹捧它的平滑缩放。您基本上制作了一个可以包含各种元素的“画布”,然后只需按住右键单击并来回平移鼠标即可进行缩放。
I worked on a team that used it to create this: http://sourceforge.net/apps/mediawiki/guitar/index.php?title=WebGuitar#EFG.2FGUI_Visualizer
我在一个使用它来创建这个的团队工作:http: //sourceforge.net/apps/mediawiki/guitar/index.php?title=WebGuitar#EFG.2FGUI_Visualizer
The nodes you see there are clickable links themselves.
您看到的节点本身就是可点击的链接。

