从JPanel中删除透明的JPanel



我想用另一个面板替换一个面板(所有面板都有透明部分)
代码如下:

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class PhotoFrame extends JFrame {
    public class PhotosDisplayPanel extends JPanel {
        private static final long serialVersionUID = 1L;
        private String[] imgsLink = null;
        private long delay = 1000;
        private long period = 1000;
        private int curIdx = 0;
        private PhotoDisplayPanel currentPhoto = null;
        public PhotosDisplayPanel(String[] imgsLink) {
            super();
            this.imgsLink = imgsLink;
            setBackground(new Color(0, 0, 0, 0));
            setLayout(new GridLayout(1, 1));
            currentPhoto = new PhotoDisplayPanel(imgsLink[curIdx]);
            add(currentPhoto);
        }
        public void start() {
            if (imgsLink == null) {
                return;
            }
            curIdx = -1;
            Timer timer = new Timer();
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    curIdx++;
                    if (curIdx >= imgsLink.length) {
                        curIdx = 0;
                    }
                    displayNextImage();
                }
            }, delay, period);
        }
        protected void displayNextImage() {
            if (currentPhoto != null) {
                remove(currentPhoto);
            }
            revalidate();
            repaint();
            currentPhoto = new PhotoDisplayPanel(imgsLink[curIdx]);
            add(currentPhoto);
            revalidate();
            repaint();
        }
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
        }
    }
    public class PhotoDisplayPanel extends JPanel {
        private static final long serialVersionUID = 1L;
        private String imgLink;
        public PhotoDisplayPanel(String imgLink) {
            super();
            this.imgLink = imgLink;
        }
        @Override
        protected void paintComponent(Graphics g) {
            if (imgLink != null && !imgLink.equals("")) {
                System.out.println("Draw image");
                // get dimension of the panel
                int pWidth = getWidth();
                int pHeight = getHeight();
                g.setColor(new Color(255, 0, 0, 50));
                g.fillRect(0, 0, pWidth, pHeight);
                Image img = new ImageIcon(imgLink).getImage();
                // Calculate positions and dimensions
                int imgWidth = img.getWidth(this);
                int imgHeight = img.getHeight(this);
                int iwidth = 0;
                int iheight = 0;
                if (imgWidth / imgHeight > pWidth / pHeight) {
                    iwidth = pWidth;
                    iheight = imgHeight * pWidth / imgWidth;
                } else {
                    iheight = pHeight;
                    iwidth = imgWidth * pHeight / imgHeight;
                }
                int ix = (pWidth - iwidth) / 2;
                int iy = (pHeight - iheight) / 2;
                // Fill the picture to the panel
                g.drawImage(img, ix, iy, iwidth, iheight, this);
            } else {
                super.paintComponent(g);
            }
        }
    }
    private static final long serialVersionUID = 1L;
    private static final int defaultWidth = 650;
    private static final int defaultHeight = 400;
    private int width = defaultWidth;
    private int height = defaultHeight;
    private PhotosDisplayPanel photoPanel; // is view panel which display
    // input and output
    private String[] imgsLink;
    public PhotoFrame() {
        initialize();
        imgsLink = new String[] { "background.png", "screenShot.jpg" };
        createControls();
    }
    private void createControls() {
        setLayout(new GridLayout(0, 1));
        photoPanel = new PhotosDisplayPanel(imgsLink);
        add(photoPanel);
        photoPanel.start();
    }
    private void initialize() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(width, height);
        setTitle("PhotosFrame");
        setAlwaysOnTop(false);
        setBackground(Color.CYAN);
        setLayout(new GridLayout(1, 1));
        setContentPane(new JLabel(new ImageIcon("blue_sunset.jpg")));
    }
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                new PhotoFrame().setVisible(true);
            }
        });
    }
}

其中currentPhoto为透明背景的面板。然而,旧面板的图像并没有完全清洁,我可以看到新面板下面的旧面板。有没有办法全面清除旧面板的图像?感谢

然而,旧面板的图像没有完全清洁,

查看Backgrounds With Transparency,了解可能的原因和一些解决方案。

基本上,当使用透明背景时,Swing不知道是否需要重新绘制背景,因此需要强制重新绘制。

最新更新