I am working on a game engine.
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Main {
public static void main(String[] args) {
System.setProperty("sun.java2d.opengl", "true");
SwingUtilities.invokeLater(() -> {
Dimension size = new Dimension(800, 600);
// Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("MRE");
frame.setUndecorated(true);
frame.setSize(size);
frame.setVisible(true);
JPanel panel = new JPanel();
panel.setSize(size);
panel.setBackground(Color.BLUE);
frame.add(panel);
});
}
}
When you run this code you should see an undecorated frame with a blue JPanel.
However if you switch the size variable with the commented one the whole screen turns black.
When I disabled sun.java2d.opengl
it worked, but I want to draw all graphics using the GPU and not the CPU.
Debugging it, I noticed the issue is only when the size is the screen size (1280, 1024), The dimensions 1280, 1025 work fine
java.awt.Dimension[width=1280,height=1024]
I would like to know if the issue is for me only or it is a common issue \$\endgroup\$setVisible(true)
on the JFrame? Doing this means that the GUI has already been rendered when the panel is added. Better to callpack()
andsetVisible(true)
on the JFrame after all components have been added. \$\endgroup\$