External Publication
Visit Post

Resizing canvas in a Swing application

jMonkeyEngine Hub March 14, 2026
Source

Never mind guys. Claude finally fixed it.

He added this fix to the main Swing app:



    public static void main(String[] args) {

        // Fix HiDPI gap in JME3/LWJGL2 canvas: Java 9+ declares the process
        // as DPI-aware, so the GL framebuffer is at physical pixel resolution
        // but Canvas.getWidth()/getHeight() report logical pixels.  This causes
        // glViewport to cover only (1/scale) of the framebuffer, leaving gaps
        // on the right and bottom edges.  Disabling Java's DPI scaling makes
        // logical == physical so the viewport fills the entire framebuffer.
        System.setProperty("sun.java2d.uiScale", "1");

        // Compensate by letting FlatLaf scale the Swing UI independently.
        // Toolkit.getScreenResolution() returns the OS DPI (e.g. 120 for 125%)
        // via the native GetDeviceCaps API, unaffected by sun.java2d.uiScale.
        try {
            int dpi = Toolkit.getDefaultToolkit().getScreenResolution();
            float scale = dpi / 96f;
            if (scale > 1f) {
                System.setProperty("flatlaf.uiScale", String.valueOf(scale));
            }
        } catch (Exception ignored) {
        }
    ```

Discussion in the ATmosphere

Loading comments...