Questions (graphic)

For Java2D/AWT, if some of these are not available with AWT but available with other libraries please mention it.

  • how to open application in full screen mode?
  • how to switch between full screen/windowed mode?
  • how to change screen resolution?
  • how to list all available resolutions?
  • how to obtain the current resolution of the user’s system?

I prefer reading source codes than tutorial/articles. So ideally a link to some example sources that does these things. But I’m not picky :smiley:

display modes

Component component = …
GraphicsConfiguration graphicsConfiguration = component.getGraphicsConfiguration();
GraphicsDevice device = graphicsConfiguration.getDevice();
DisplayMode[] modes = device.getDisplayModes();

full screen

Window frame = …
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice device = env.getDefaultScreenDevice();
device.setFullScreenWindow(frame);
device.setDisplayMode(…);

  1. To make full screen, you can use the code 65K showed. Do note that JFrame extends Frame, which extends Window so you can set a JFrame full screen.
    To make it full screen at the native resolution, don’t set a display mode.

  2. To switch back to windowed mode, do setFullScreenWindow(null).

  3. To switch resolutions, do “device.setDisplayMode(DisplayMode)”

4 and 5. You don’t have to rely on a Component to get the available display modes:


GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();

GraphicsDevice[] devices = ge.getScreenDevices(); // returns the settings for all monitors

GraphicsDevice gd = ge.getDefaultScreenDevice(); // return the default screen device

DisplayMode[] modes = gd.getDisplayModes(); // all supported display modes

DislayMode currentMode = gd.getDisplayMode(); // current display mode

GraphicsConfiguration gc = currentMode.getDefaultConfiguration(); // very useful methods, such as creating compatible BufferedImage that will allow for faster render to the screen

That’s basically it.

Be aware that switching from fullscreen out and in and sometimes cause some vague errors in the window manager, primarily of course in linux
you may have to reset the size and stuff.

do use bufferstrategy

and some commands can only be made in correct order, like you have to set setUndecorated(true) before setfullscreen(true)