Search code examples
javaswingresizeflickerjwindow

Flickering While Resizing Transparent JWindow


I'm trying to make a transparent window with Java Swing, and I have created the window. Problem is when I resize the window, it flickers. I tried changing the background to an opaque color instead. That fixed the problem, But I want the window to be transparent. I have also tried

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

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

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

But with no avail. I've tried JWindow.setBounds instead of JWindow.setSize, but that also had no effect.

Here is the code I use to produce the 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();
    }
}

How can I prevent flickering when the JWindow is being resized?

Any help is appreciated.


Solution

  • As suggested by @camickr I should not resize the window in the render method.

    So to stop the flickering I remove

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

    from the paintComponent(Graphics g) method. And add

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

    to the constructor GlassWindow().