Java Swing: Transparent PNG permanently captures original background

前端 未结 2 1252
没有蜡笔的小新
没有蜡笔的小新 2020-12-12 05:35

I have the following code:

import javax.swing.JWindow;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import java.awt.Graphics;
import java.aw         


        
2条回答
  •  离开以前
    2020-12-12 06:12

    The problem is, you window is actually transparent. Java still thinks that the Window opaque and therefore won't update the graphics in such away as to show what's actually behind.

    Creating a transparent window is relatively simple in Java since Java 1.6.10 (I think)

    The following is a very simple example, using a semi transparent paint effect that will allow what ever falls below the window to continue to be painted correctly.

    import com.sun.awt.AWTUtilities;
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.EventQueue;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Shape;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.geom.RoundRectangle2D;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class TransaprentBlur {
    
        public static void main(String[] args) {
            new TransaprentBlur();
        }
    
        public TransaprentBlur() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    }
    
                    JFrame frame = new JFrame("Testing");
                    frame.setUndecorated(true);
                    frame.setBackground(new Color(0, 0, 0, 0));
    //                Java 6...
    //                AWTUtilities.setWindowOpaque(frame, true);
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(new TestPane());
                    frame.setSize(400, 400);
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class TestPane extends JPanel {
    
            public TestPane() {
                setOpaque(false);
    
                addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        if (e.getClickCount() == 2) {
                            System.exit(0);
                        }
                    }
                });
            }
    
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Shape shape = new RoundRectangle2D.Float(0, 0, getWidth() - 1, getHeight() - 1, 20, 20);
                Graphics2D g2d = (Graphics2D) g.create();
                g2d.setColor(new Color(225, 225, 225, 128));
                g2d.fill(shape);
                g2d.setColor(Color.GRAY);
                g2d.draw(shape);
                g2d.dispose();
            }
    
        }
    
    }
    

    Update with image example

    Screen shoots showing windows been moved behind the window...

    enter image description hereenter image description here

    Basically, all you need to do, is place you image rendering code with in the paintComponent method of TestPane

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import javax.imageio.ImageIO;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class TransaprentBlur {
    
        public static void main(String[] args) {
            new TransaprentBlur();
        }
    
        public TransaprentBlur() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    }
    
                    JFrame frame = new JFrame("Testing");
                    frame.setUndecorated(true);
                    frame.setBackground(new Color(0, 0, 0, 0));
    //                Java 6...
    //                AWTUtilities.setWindowOpaque(frame, true);
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(new TestPane());
                    frame.setSize(400, 400);
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class TestPane extends JPanel {
    
            private BufferedImage image;
    
            public TestPane() {
    
                try {
                    image = ImageIO.read(getClass().getResource("/5UNGbsr.png"));
                } catch (IOException ex) {
                }
                setOpaque(false);
    
                addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        if (e.getClickCount() == 2) {
                            System.exit(0);
                        }
                    }
                });
            }
    
            @Override
            public Dimension getPreferredSize() {
                return image == null ? super.getPreferredSize() : new Dimension(image.getWidth(), image.getHeight());
            }
    
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                if (image != null) {
                    Graphics2D g2d = (Graphics2D) g.create();
                    int x = (getWidth() - image.getWidth()) / 2;
                    int y = (getHeight() - image.getHeight()) / 2;
                    g2d.drawImage(image, x, y, this);
                    g2d.dispose();
                }
            }
    
        }
    
    }
    

提交回复
热议问题