调整透明 JWindow 大小时闪烁

Flickering While Resizing Transparent JWindow

我正在尝试使用 Java Swing 制作一个透明的 window,并且我已经创建了 window。问题是当我调整 window 大小时,它会闪烁。我尝试将背景更改为不透明的颜色。这解决了问题,但我希望 window 是透明的。我也试过

Toolkit.getDefaultToolkit().setDynamicLayout(true);,

Toolkit.getDefaultToolkit().getDesktopProperty("awt.dynamicLayoutSupported");,

System.setProperty("sun.awt.noerasebackground", "true");,

但无济于事。我试过 JWindow.setBounds 而不是 JWindow.setSize,但也没有效果。

这是我用来生成 window

的代码
import java.awt.Color;
import java.awt.Graphics;
import java.awt.MouseInfo;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JPanel;
import javax.swing.JWindow;

public class GlassWindow extends JPanel {
    // RIGHT CLICK TO CLOSE
    private static final long serialVersionUID = 1L;
    private JWindow jwindow = new JWindow();
    private boolean programCalledRender, shouldClose;
    private int edges = 8;
    private Color background = new Color(255, 100, 0, 100);
    
    public GlassWindow() {
        int width = 1000, height = 750;
        this.setOpaque(false);
        this.setSize(width, height);
        jwindow.setSize(width, height);
        jwindow.setBackground(new Color(0, 0, 0, 0));
        jwindow.setContentPane(this);
        jwindow.setLocationRelativeTo(null);
        jwindow.setVisible(true);
        jwindow.addMouseListener(new MouseListener() {
            public void mouseClicked(MouseEvent e) {
                if(e.getButton() == MouseEvent.BUTTON3) System.exit(0);
            }
            public void mouseEntered(MouseEvent e) {}
            public void mouseExited(MouseEvent e) {}
            public void mousePressed(MouseEvent e) {}
            public void mouseReleased(MouseEvent e) {}
        });
        new Thread(() -> run()).start();
    }
    public void render() {
        programCalledRender = true;
        this.repaint();
    }
    private void run() {
        int setTPS = 20, setFPS = 60;
        long lastTime = System.nanoTime();
        double delta = 0, frameDelta = 0;
        while(true) {
            if(shouldClose) break;
            long now = System.nanoTime();
            delta += (now-lastTime)/(1000000000/(double)setTPS);
            frameDelta += (now-lastTime)/(1000000000/(double)setFPS);
            lastTime = now;
            while(delta > 0) {
                tick();
                delta--;
            }
            while(frameDelta > 0) {
                render();
                frameDelta--;
            }
        }
    }
    private void tick() {
        
    }
    @Override
    public void paintComponent(Graphics g) {
        if(!programCalledRender) return;
        super.paintComponent(g);
        int newWidth = MouseInfo.getPointerInfo().getLocation().x+20-jwindow.getX();
        if(newWidth != jwindow.getWidth()) {
            jwindow.setSize(newWidth, jwindow.getHeight());
        }
        if(background != null) {
            g.setColor(background);
            g.fillRect(edges, edges, jwindow.getWidth()-edges*2, jwindow.getHeight()-edges*2);
        }
        g.setColor(new Color(0, 0, 0, 100));
        for(int i = 0; i <= edges; i++) {
            g.drawRect(i, i, jwindow.getWidth()-i*2, jwindow.getHeight()-i*2);
        }
    }
    
    public static void main(String[] args) {
        new GlassWindow();
    }
}

如何在调整 JWindow 大小时防止闪烁?

感谢任何帮助。

按照@camickr 的建议,我应该 调整render 方法中window 的大小。

所以为了停止闪烁我删除了

      int newWidth = MouseInfo.getPointerInfo().getLocation().x+20-jwindow.getX();
        if(newWidth != jwindow.getWidth()) {
            jwindow.setSize(newWidth, jwindow.getHeight());
        }

来自 paintComponent(Graphics g) 方法。并添加

        jwindow.addMouseMotionListener(new MouseMotionListener() {
            public void mouseDragged(MouseEvent e) {}
            public void mouseMoved(MouseEvent e) {
                int newWidth = MouseInfo.getPointerInfo().getLocation().x+20-jwindow.getX();
                if(newWidth != jwindow.getWidth()) {
                    jwindow.setSize(newWidth, jwindow.getHeight());
                }
            }
        });

构造函数GlassWindow().