放大 Java Swing 应用程序
我正在寻找放大 Java Swing 应用程序的方法。这意味着我想按给定因子调整给定 JPanel 中所有组件的大小,就好像我要截取 UI 的屏幕截图并仅应用“图像缩放”操作一样。字体大小以及复选框、文本框、光标等的大小都需要调整。 可以通过对图形对象应用变换来缩放组件:
protected Graphics getComponentGraphics(Graphics g) {
Graphics2D g2d=(Graphics2D)g;
g2d.scale(2, 2);
return super.getComponentGraphics(g2d);
}
只要您不关心自我更新组件,这种方法就可以工作。如果您的应用程序中有一个文本框,则此方法将不起作用,因为文本框每秒都会更新自身以显示(闪烁的)光标。由于这次它不使用修改后的图形对象,因此该组件出现在旧位置。是否有可能永久更改组件图形对象?鼠标单击事件处理程序也存在问题。 另一种可能性是将 JPanel 的所有子组件的大小 (setPreferredSize) 调整为新的大小。这不适用于复选框,因为复选框的显示图片不会改变其大小。 我还考虑过编写自己的布局管理器,但我认为这行不通,因为布局管理器只能更改对象的位置(和大小),但无法放大复选框(请参阅上一段)。或者我的这个假设是错误的? 您是否知道如何在不编写自定义组件的情况下实现可缩放的 Swing GUI?我寻找可旋转的用户界面,因为这个问题看起来很熟悉,但我也没有找到任何令人满意的解决方案来解决这个问题。
感谢您的帮助, 克里斯
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:
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.
Thanks for your help,
Chris
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以尝试一下 JXLayer 库。
其中有几个工具可以帮助您进行缩放。查看此处显示的示例。我建议您从这个库中阅读有关 TransformUI 的更多信息。从这个例子来看,它似乎可以帮助解决你的问题。
You could give a try to the JXLayer library.
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.
缩放视图很容易;转换鼠标坐标只是稍微困难一些。这是一个基本示例。我会保留
JComponent
,尽管开发一个类似的了解几何形状的ScaledComponent
可能是有意义的。这就是 @Gnoupi 关于使用库的建议的用武之地。Scaling the view is easy enough; transforming mouse coordinates is only slightly more difficult. Here's an elementary example. I'd keep
JComponent
s out, although it might make sense to develop an analogousScaledComponent
that knows about the geometry. That's where @Gnoupi's suggestion of using a library comes in.嘿,如果您想像任何其他图像查看器一样缩放图像,您可以尝试使用 JPanel 使用 drawImage() 方法绘制图像现在创建一个按钮,当您单击该按钮时,会增加框架上出现的面板的大小就像在缩放中查看图像一样
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
您可能会发现 Piccolo2D.java API 很有用:http://code.google.com/p/piccolo2d/< /a>
非常简单。
它特别吹捧其平滑的缩放功能。您本质上是制作一个可以包含各种元素的“画布”,然后只需按住右键单击并来回平移鼠标即可进行缩放。
我在一个团队中使用它来创建这个: http ://sourceforge.net/apps/mediawiki/guitar/index.php?title=WebGuitar#EFG.2FGUI_Visualizer
您看到的节点本身就是可点击的链接。
You might find Piccolo2D.java API useful: 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
The nodes you see there are clickable links themselves.
从 Java 9 开始,有 VM 参数(实际上用于高 dpi 缩放)可以以更高的缩放因子呈现应用程序:
或者:
Since Java 9, there are VM arguments (actually meant to be used for high dpi scaling) that can render a application with a higher scaling factor:
Or: