简体   繁体   English

放大 Java Swing 申请

[英]Zoom in Java Swing application

I am looking for ways to zoom in a Java Swing application.我正在寻找放大 Java Swing 应用程序的方法。 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.这意味着我想按给定因素调整给定 JPanel 中所有组件的大小,就好像我会截取 UI 的屏幕截图并仅应用“图像缩放”操作一样。 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:可以通过对图形 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.如果您的应用程序中有文本框,则此方法将停止工作,因为文本框每秒更新一次以显示(闪烁的)cursor。并且由于这次它不使用修改后的图形 object,因此该组件出现在旧位置。 Is there a possibility to change a components graphics object permanently?是否有可能永久更改组件图形 object? 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.另一种可能性是将 JPanel (setPreferredSize) 的所有子组件调整为新大小。 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).我还考虑过编写自己的布局管理器,但我认为这行不通,因为布局管理器仅更改对象的 position(和大小),但无法放大复选框(请参阅上一段)。 Or am I wrong with this hypothesis?还是我对这个假设有误? Do you have any ideas how one could achieve a zoomable Swing GUI without programming custom components?您是否知道如何在不编写自定义组件的情况下实现可缩放的 Swing GUI? 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谢谢你的帮助,克里斯

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. 我建议你从这个库中阅读有关TransformUI的更多信息。 From the example, it seems like it could help solving your problem. 从示例中,它似乎可以帮助解决您的问题。

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 analogous ScaledComponent that knows about the geometry. 我会保留JComponent ,尽管开发一个知道几何的类似ScaledComponent可能是有意义的。 That's where @Gnoupi's suggestion of using a library comes in. 这就是@ Gnoupi建议使用图书馆的地方。

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. 您在那里看到的节点本身就是可点击的链接。

嘿你可以尝试这个如果你想像任何其他图像查看器一样缩放图像使用JPanel绘制图像使用drawImage()方法现在创建一个按钮,当你单击按钮增加框架上面板的大小它出现就像在缩放中查看图像一样

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:自 Java 9 以来,有 VM arguments(实际上意味着用于高 dpi 缩放)可以呈现具有更高缩放因子的应用程序:

java -Dsun.java2d.uiScale=2.0 -jar MyApplication.jar

Or:或者:

java -Dsun.java2d.win.uiScaleX=2.0 -Dsun.java2d.win.uiScaleY=2.0 -jar MyApplication.jar

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM