Java Swing:从与JLayeredPane中的其他JPanel重叠的JPanel清除自定义绘图 [英] Java Swing: Clearing custom painting from a JPanel overlayed with other JPanels in a JLayeredPane

查看:659
本文介绍了Java Swing:从与JLayeredPane中的其他JPanel重叠的JPanel清除自定义绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JLayeredPane包含三个JPanel,其中两个重叠,我正在绘制形状。重叠的两个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);

但是这不仅清除了绘制到pane2的所有内容,还清除了下面的pane1。所以我的问题是:有没有办法清除绘制到一个JPanel的所有内容而不影响绘制到JPanel下的任何内容?

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?

推荐答案

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

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);

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

You might also need to repaint the bottom JPanel.

如果您无法重新绘制底部 JPanel - 例如,如果您没有列表在任何地方的形状 - 然后我怀疑它可能无法在底部 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.

这篇关于Java Swing:从与JLayeredPane中的其他JPanel重叠的JPanel清除自定义绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆