TestAwtPanels: lwjgl2 vs lwjgl3 + Swing/AWT

Hi @SwiftWolf ,
I think I found the source of the confusion around the test class results. It seems the behavior depends on whether we’re using LWJGL 2 or 3:

  • LWJGL 2:
    • The Canvas isn’t centered in the Frame, regardless of using a BorderLayout.CENTER Panel or manual resizing.
    • Mouse behavior: Click/release with flyCam.setDragToRotate(true) doesn’t re-center the cursor during camera rotation.
  • LWJGL 3:
    • We need to set settings.setGammaCorrection(false) to avoid program crashes.
    • The Canvas is centered by default (no need for BorderLayout.CENTER Panel).
    • Mouse behavior: Click/release with flyCam.setDragToRotate(true) re-centers the cursor for camera rotation.

This difference in behavior explains why some configurations work in one and not the other.

Note: The mouse problem is caused by the different behavior of the libraries when FlyByCamera enables/disables cursor visibility. This behavior is currently being discussed in an open issue here.


1. Here is code and screenshots of the test with lwjgl2

(run with jme3.7.0-stable, Windows 11):

public class Test_SafeCanvas extends SimpleApplication {

    public static void main(String[] args) {
        AppSettings settings = new AppSettings(true);
        settings.setResolution(640, 480);

        final Test_SafeCanvas app = new Test_SafeCanvas();
        app.setPauseOnLostFocus(false);
        app.setSettings(settings);
        app.createCanvas();
        app.startCanvas(true);

        JmeCanvasContext context = (JmeCanvasContext) app.getContext();
        Canvas canvas = context.getCanvas();
        canvas.setPreferredSize(new Dimension(settings.getWidth(), settings.getHeight()));

        try {
            Thread.sleep(3000);
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }

        JFrame frame = new JFrame("Test - LWJGL2");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                app.stop();
            }
        });
        
        JPanel container = new JPanel();
        container.setLayout(new BorderLayout());
        container.add(canvas, BorderLayout.CENTER);

        frame.getContentPane().add(container);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    @Override
    public void simpleInitApp() {
        flyCam.setDragToRotate(true);

        Box b = new Box(1, 1, 1);
        Geometry geom = new Geometry("Box", b);
        Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        mat.setTexture("ColorMap", assetManager.loadTexture("Interface/Logo/Monkey.jpg"));
        geom.setMaterial(mat);
        rootNode.attachChild(geom);
    }

}


2. Here is code and screenshots of the test with lwjgl3

(run with jme3.7.0-stable, Windows 11):

public class Test_SafeCanvas extends SimpleApplication {

    public static void main(String[] args) {
        AppSettings settings = new AppSettings(true);
        settings.setGammaCorrection(false); /* for lwjgl3 */
        settings.setResolution(640, 480);

        final Test_SafeCanvas app = new Test_SafeCanvas();
        app.setPauseOnLostFocus(false);
        app.setSettings(settings);
        app.createCanvas();
        app.startCanvas(true);

        JmeCanvasContext context = (JmeCanvasContext) app.getContext();
        Canvas canvas = context.getCanvas();
        canvas.setPreferredSize(new Dimension(settings.getWidth(), settings.getHeight()));

        try {
            Thread.sleep(3000);
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }

        JFrame frame = new JFrame("Test - LWJGL3");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                app.stop();
            }
        });
        
        // This panel is not necessary-to center the Canvas
//        JPanel container = new JPanel();
//        container.setLayout(new BorderLayout());
//        container.add(canvas, BorderLayout.CENTER);

        frame.getContentPane().add(canvas);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    @Override
    public void simpleInitApp() {
        flyCam.setDragToRotate(true);
        flyCam.setMoveSpeed(10f);
        viewPort.setBackgroundColor(ColorRGBA.DarkGray);

        Box b = new Box(1, 1, 1);
        Geometry geom = new Geometry("Box", b);
        Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        mat.setTexture("ColorMap", assetManager.loadTexture("Interface/Logo/Monkey.jpg"));
        geom.setMaterial(mat);
        rootNode.attachChild(geom);
    }

}

Edit:

It remains to clarify my second doubt. Any suggestions?

3 Likes