brandonborkholder / glg2d

Graphics2D wrapper for JOGL
77 stars 31 forks source link

Does not render for transparent backgrounds #35

Open Jire opened 8 years ago

Jire commented 8 years ago

GLG2D, and seemingly every JOGL/LWJGL do not render on transparent backgrounds (backgrounds with a color alpha of less than 1F). It renders absolutely nothing, even on 0.999F, but as soon as the alpha is 1F it works as intended.

The reason this is so important is to make "overlays" which draw directly atop the desktop, which is quite common in game mods / cheats.

Here's my demo:

import org.jogamp.glg2d.GLG2DCanvas;

import javax.swing.*;
import java.awt.*;

public final class OverlayDemo extends JWindow {

    private static final Dimension SCREEN_SIZE = Toolkit.getDefaultToolkit().getScreenSize();

    private final JPanel frame = new JPanel() {
        @Override
        protected void paintComponent(Graphics g) {
            g.setColor(Color.GREEN);
            g.fillRect(100, 100, 200, 200);
        }
    };

    private OverlayDemo() {
        setSize(SCREEN_SIZE);
        setBackground(new Color(0, 0, 0, 0 /* alpha */));

        frame.setSize(SCREEN_SIZE);
        frame.setDoubleBuffered(true);

        setContentPane(new GLG2DCanvas(frame));
        setVisible(true);

        new Thread(() -> {
            while (!Thread.interrupted()) {
                repaint();
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    return;
                }
            }
        }).start();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(OverlayDemo::new);
    }

}