You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何获取显示器分辨率并设置到DesktopLauncher?伪全屏代码报错求助

Hey there, let's tackle your two questions one by one:

1. How to Get Display Resolution and Set It to DesktopLauncher's config.width/height

Since you're working with a LibGDX DesktopLauncher, there are two reliable ways to grab the monitor's native resolution and apply it to your configuration:

Method 1: Use LibGDX's Built-in DisplayMode

This is the most straightforward approach for LibGDX projects:

public class DesktopLauncher {
    public static void main (String[] arg) {
        // Initialize the config object first
        LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
        
        // Get the primary monitor's display mode
        DisplayMode displayMode = LwjglApplicationConfiguration.getDisplayMode();
        
        // Set config dimensions to match the monitor
        config.width = displayMode.width;
        config.height = displayMode.height;
        
        // Launch your game with the updated config
        new LwjglApplication(new YourGameClass(), config);
    }
}

Method 2: Use AWT Toolkit (For Multi-Monitor Flexibility)

If you need to target a specific monitor or want a more cross-desktop compatible way, use AWT's Toolkit:

import java.awt.Toolkit;

public class DesktopLauncher {
    public static void main (String[] arg) {
        LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
        
        // Fetch screen resolution via AWT
        java.awt.Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        config.width = (int) screenSize.getWidth();
        config.height = (int) screenSize.getHeight();
        
        new LwjglApplication(new YourGameClass(), config);
    }
}

Note: LibGDX's desktop project includes AWT dependencies by default, so this should work out of the box.

2. Troubleshooting the NullPointerException for Terraria-Style Borderless Fullscreen

That NullPointerException at line 13 of your DesktopLauncher means you're trying to access a method/property on an object that hasn't been initialized (it's null). Let's break down the likely causes and fixes:

First, Let's Confirm the Correct Borderless Fullscreen Setup

Terraria's "pseudo-fullscreen" is just a borderless window that fills the entire screen. Here's the proper way to implement it in LibGDX:

public class DesktopLauncher {
    public static void main (String[] arg) {
        LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
        
        // Get screen resolution (with null check to avoid NPE!)
        DisplayMode displayMode = LwjglApplicationConfiguration.getDisplayMode();
        if (displayMode != null) {
            config.width = displayMode.width;
            config.height = displayMode.height;
        } else {
            // Fallback to a default resolution if display mode can't be fetched
            config.width = 1920;
            config.height = 1080;
        }
        
        // Enable borderless window (this is the key for pseudo-fullscreen)
        config.borderless = true;
        // Optional: Prevent window resizing
        config.resizable = false;
        
        // Make sure your game instance is properly initialized!
        new LwjglApplication(new MMGame(), config);
    }
}

Likely Causes of the NullPointerException

  1. Uninitialized config Object
    If line 13 is something like config.width = ... but you forgot to create the config with new LwjglApplicationConfiguration(), that'll throw an NPE. Double-check that you've initialized config before using it.

  2. Null DisplayMode
    In rare cases, LwjglApplicationConfiguration.getDisplayMode() might return null (e.g., headless environments, though that's unlikely for a desktop game). Adding a null check like in the example above will prevent this.

  3. Null Game Instance
    If line 13 is the new LwjglApplication(...) call, make sure you're passing a valid instance of your game class (not null). For example, new LwjglApplication(null, config) would trigger an NPE.

Debugging Step

To pinpoint exactly what's null, add print statements to your main method:

public static void main (String[] arg) {
    System.out.println("Launcher starting...");
    LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
    System.out.println("Config initialized: " + (config != null));
    
    DisplayMode displayMode = LwjglApplicationConfiguration.getDisplayMode();
    System.out.println("DisplayMode: " + displayMode);
    
    if (displayMode != null) {
        System.out.println("Display width: " + displayMode.width);
    }
    
    // Rest of your code...
}

Run the game again, and the console output will tell you which object is null.

内容的提问来源于stack exchange,提问作者leonidsah

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 08:09:18