Java Swing:清除 JPanel 中与 JLayeredPane 中的其他 JPanel 重叠的自定义绘制

发布于 2024-11-01 09:44:08 字数 334 浏览 0 评论 0原文

我有一个包含三个 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 技术交流群。

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

发布评论

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

评论(3

时光倒影 2024-11-08 09:44:08

确保您的面板是透明的。我认为你需要这样的代码:

Graphics g = pane2.getGraphics();      
g.clearRect (0, 0, 1000, 1000); 
pane2.repaint(0, 0, 1000, 1000);

或者你应该能够使用以下代码来强制重新绘制所有面板:

layeredPane.repaint();

Make sure your panels are non-opaque. I would think you need code like:

Graphics g = pane2.getGraphics();      
g.clearRect (0, 0, 1000, 1000); 
pane2.repaint(0, 0, 1000, 1000);

Or you should be able to use the following to force a repaint of all the panels:

layeredPane.repaint();
时光礼记 2024-11-08 09:44:08

我认为你可以用这种方式清除它,然后按照标准方式绘制它。类似于:

Graphics g = pane2.getGraphics(); 
g.clearRect (0, 0, 1000, 1000);
super.paintComponent(g);

您可能还需要重新绘制底部 JPanel

如果您无法重新绘制底部 JPanel - 例如,如果您在任何地方都没有形状列表 - 那么我怀疑可能无法在底部恢复 JPanel

I think you can clear it that way then just paint it the standard way. Something like:

Graphics g = pane2.getGraphics(); 
g.clearRect (0, 0, 1000, 1000);
super.paintComponent(g);

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 bottom JPanel.

甜柠檬 2024-11-08 09:44:08

我认为你应该使用剪辑来设置不应该被替换的区域。
在面板 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文