如何使java JPanel和graphics2d透明?
嗯,标题是很不言自明的。我想使用 java 构建两个面板,一层一层地放置在彼此之上。我希望顶层包含一个 JPanel,其中包含一个 Graphics2d 对象。我希望 JPanel 和graphics2d 都有透明背景(我仍然希望graphics2d 绘制的内容可见)。有谁知道如何做到这一点?
Well the title is quite self explanatory. I want to build two panels one on-top of each other in layers using java. I want the top layer to contain a JPanel which will contain a graphics2d object. I'd like both the JPanel and graphics2d to have transparent background (I still want the content drawn by the graphics2d visible). Does anyone have an idea how it can be done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 JPanel 上调用 setOpaque(false) - 这不会绘制 JPanel 的背景。
取决于您要重写以获取 Graphics2D 的方法(JPanel 不包含像组件一样的 Graphics2D 对象 - Graphics2D 对象用于绘制 JPanel) - 如果它是paintComponent(),您应该 阅读 JComponent 的 JavaDocs - 并首先调用 super.paintComponent(g) 以便遵守不透明度 - 然后进行其余的绘制。
工作示例:
Call setOpaque(false) on the JPanel - that will not paint the JPanel's background.
Depending on what method you're overriding to get at the Graphics2D (JPanel's don't contain a Graphics2D object like a component - a Graphics2D object is used to paint the JPanel) - if it's paintComponent() you should read the JavaDocs for JComponent - and call super.paintComponent(g) first so that opacity is honored - and then do the rest of your painting.
Working example: