Java Swing:清除 JPanel 中与 JLayeredPane 中的其他 JPanel 重叠的自定义绘制
我有一个包含三个 JPanel 的 JLayeredPane,其中两个重叠,我正在向其绘制形状。重叠的两个 JPanel 之一需要清除绘制到其上的每个形状,而不影响绘制到其下方的 JPanel 的形状从屏幕上消失。目前我正在使用这样的东西:
Graphics g = pane2.getGraphics();
g.clearRect (0, 0, 1000, 1000);
但这不仅会清除绘制到pane2的所有内容,还会清除其下方的pane1。所以我的问题是:有没有什么方法可以清除绘制到一个 JPanel 的所有内容,而不影响绘制到其下的 JPanel 的任何内容?
I've got a JLayeredPane containing three JPanels, two of which overlap, that I'm painting shapes to. One of the two JPanels that overlap needs to have every shape drawn to it cleared without affecting the shapes drawn to the JPanel under it disappear from the screen. Currently I'm using something like this:
Graphics g = pane2.getGraphics();
g.clearRect (0, 0, 1000, 1000);
But this not only clears everything painted to pane2 but also pane1, which is under it. So my question is: Is there any way to clear everything painted to one JPanel without affecting anything painted to a JPanel under it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
确保您的面板是透明的。我认为你需要这样的代码:
或者你应该能够使用以下代码来强制重新绘制所有面板:
Make sure your panels are non-opaque. I would think you need code like:
Or you should be able to use the following to force a repaint of all the panels:
我认为你可以用这种方式清除它,然后按照标准方式绘制它。类似于:
您可能还需要重新绘制底部
JPanel
。如果您无法重新绘制底部
JPanel
- 例如,如果您在任何地方都没有形状列表 - 那么我怀疑可能无法在底部恢复JPanel
。I think you can clear it that way then just paint it the standard way. Something like:
You might also need to repaint the bottom
JPanel
.If you cannot repaint the bottom
JPanel
--if, for example, you do not have a list of the shapes anywhere--then I suspect that it may not be possible to recover on the bottomJPanel
.我认为你应该使用剪辑来设置不应该被替换的区域。
在面板 2 中检测哪个区域不应被损坏并创建绳索矩形。然后创建一个剪辑区域。减去面积的矩形。请参阅 Area 类来减去形状。
I think you should use clip to set regions which should not be replaced.
In the panel 2 detecty which area should not be damaged and create roper rectangle(s). Then create a clip area. Rectangle with subtracted area. See Area class to subtract shape.