屏幕上的浮动矩形

发布于 2024-07-24 01:22:39 字数 560 浏览 4 评论 0原文

我知道如何在 JPanel 上绘制矩形,但如何在屏幕上绘制矩形以使矩形看起来是浮动的? 更具体地说,是一个未填充的矩形。 我的想法是使用一个透明的JFrame,上面画了一个矩形; 然而,这使得 JFrame 中的所有内容都是透明的。

我的解决方案 所以我认为有很多方法可以解决这个问题,有些方法比其他方法更复杂,有些方法比其他方法更实用。

我尝试了启动画面。 问题是运行时需要传递虚拟机参数“-splash”。 我创建了一个清单文件来自动执行此操作/将参数放入 eclipse 中; 但代码依赖于 .gif 文件,我无法轻松更改矩形的大小/位置。 通过屏幕截图伪造时也会出现类似的问题。 不过,建议很好,我学到了一些很酷的东西。

那么,回到我所做的事情。 我使用了 JFrame,获取内容窗格并将背景设置为红色(无论您想要什么颜色),然后将框架设置为未装饰,从而删除窗口的标题栏和边框。 这创建了一个浮动矩形,我可以轻松更改(frame.setSize,.setLocation)的大小和位置。 我尚未将其设为非填充矩形,我尝试了内部框架和分层窗格,但没有成功。

I know how to draw a rectangle onto a JPanel, but how can I paint a rectangle to the screen so that the rectangle appears to be floating? More specifically, a non-filled rectangle. My thought is to use a transparent JFrame with a rectangle drawn on it; however, this makes all of the content in the JFrame transparent.

My Solution
So I think there are many ways of going about this, some more complex than others, some more practical than others.

I tried the splash screen. The problem with that is you need to pass VM parameters "-splash " when you run. I created a manifest file to automate this/put the parameters into eclipse; but then the code is dependent on the .gif file and I can't change the size/position of the rectangle easily. Similar problems occur while faking it via screen screenshot. Good suggestions though, I learned some pretty cool stuff.

So, back to what I did. I used a JFrame, got the content pane and set the background to red (what ever color you want), then set the frame undecorated which removes the titlebar and border of the window. This created a floating rectangle which I could easily change the size and location of (frame.setSize, .setLocation). I have yet to make this a non filled rectangle, I tried internal frames and layeredpanes, but no success.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

淡淡の花香 2024-07-31 01:22:39

JFrame 是一个重量级组件,它们在很长一段时间内都是不透明的。 然而,从Java 6u10开始,类中有一个用于透明窗口的非官方API com.sun.awt.AWTUtilities,它很可能会在 Java 7 中成为正式版本。在早期版本中,模拟这种事情的唯一方法是 通过使用 java.awt.Robot 拍摄的屏幕截图伪造它

JFrame is a heavyweight component, and those were always opaque for the longest time. However, since Java 6u10, there is an inofficial API for transparent windows in the class com.sun.awt.AWTUtilities, which will most likely become official in Java 7. In earlier versions, the only way to simulate this kind of thing was to fake it via screenshots taken with java.awt.Robot

听不够的曲调 2024-07-31 01:22:39

您可能必须使窗口的某些部分透明,而实际绘制的矩形则不然。 我怀疑是否有一个与平台无关的解决方案,因此您需要为此求助于 JNI。 根据您需要执行的操作,使窗口对于点击不可见也可能会很好,这也需要其他技巧。

You would probably have to have parts of the window transparent while the actual drawn rectangle is not. I doubt there is a platform-agnostic solution for this so you would need to resort to JNI for this. Depending on what you need to do it might also be nice to make the window invisible for clicks which would need other tricks as well.

皇甫轩 2024-07-31 01:22:39

https://github.com/twall/jna/

该项目提供了一个库并包含以下示例半透明的时钟和信息气球甚至超越了您想要做的事情。 这些演示在 Windows 上运行良好,但我无法透露它们的跨平台程度。

https://github.com/twall/jna/

That project offers a library and has examples of a clock and info balloons that are semi-transparent and transcend even what you're trying to do. The demos work well on Windows but I couldn't speak to how cross platform they are.

橘虞初梦 2024-07-31 01:22:39

您可能需要查看 JLayeredPane。 此页面上的演示部分展示了您想要的内容,但是在绘制矩形时,您需要将油漆设置为透明:

AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f);
g2d.setComposite(ac);
g2d.drawImage(image, x, y, this);

You might want to look at JLayeredPane. The demo on this page shows partially what you want, however when painting your rectangle you'll need to set your paint to transparent:

AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f);
g2d.setComposite(ac);
g2d.drawImage(image, x, y, this);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文