如何获取显示器分辨率并设置到DesktopLauncher?伪全屏代码报错求助
Hey there, let's tackle your two questions one by one:
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.
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
Uninitialized
configObject
If line 13 is something likeconfig.width = ...but you forgot to create the config withnew LwjglApplicationConfiguration(), that'll throw an NPE. Double-check that you've initialized config before using it.Null
DisplayMode
In rare cases,LwjglApplicationConfiguration.getDisplayMode()might returnnull(e.g., headless environments, though that's unlikely for a desktop game). Adding a null check like in the example above will prevent this.Null Game Instance
If line 13 is thenew LwjglApplication(...)call, make sure you're passing a valid instance of your game class (notnull). 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

