Skip to content
Advertisement

How to set JFrame in fullscreen using users native resolution?

I am trying to create a little game for a project in university. Since I need to update the screen permanently I am looking for a good render loop implementation. It should be full screen. I found this on gamedev.net – Java Games: Active Rendering:

JavaScript

I executed the code and there are two things I am wondering about:

My display resolution is 1920×1080. The code is using 640×480. Therefore the rendered rectangles look like a 640×480 picture upscaled to 1920×1080 meaning you have huge pixels and the code doesn’t take avantage of the higher posible resolution of the display. How can I adapt the code to actually use the native display resolution of the user?

Second, usually what you see when you take a look on games code is a rendering loop living in its own thread to not block the main thread. However thats not the case here. Why? I assume because the JFrame already lives in its own thread created by swing?

Thanks for the help.

Advertisement

Answer

There are lots of issues with this code.

First of all, when you are checking (polling) a flag variable in the main thread, that will be updated by a key listener, which will be called in the event dispatch thread, the minimum you have to do, is to declare that variable volatile.

Then, there is no point in using JFrame when you don’t use the Swing framework at all. Further, it’s nonsensical to request double buffering from the AWT and then, use a BufferedImage for another buffering atop the already buffered operation.

Using the native resolution is as easy as removing the setDisplayMode(…) call. After turning the window to full screen, you can simply use getWidth() and getHeight() on it to get the actual dimensions for the operations (it’s not needed for the buffered image, as that was obsolete anyway).

JavaScript

I made some other small improvements.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement