LWJGL Window

By the end of this tutorial you will have learned how to create, update, and destroy a display compatible with the Lightweight Java Game Library.

Everything is static

Unlike in other Java windowing libraries, the LWJGL handles the window statically. Instead of creating an instance of the hypothetical Display class, static methods are invoked.

Importing classes

Note that Display refers to org.lwjgl.opengl.Display and not the other Display class. Also DisplayMode refers to org.lwjgl.opengl.DisplayMode. The methods that are used in this tutorials are listed below.

Display.setDisplayMode(DisplayMode);
// Sets the display mode (width,height) of the window.
Display.setTitle(String);
// Sets the window title to the specified String.
Display.create();
// Creates a display with the specified display mode and title.
Display.isCloseRequested();
// Returns if the user attempted to close the display.
Display.update();
// Updates the contents of the display and checks for input.
Display.sync(int);
// Caps the frame-rate of the display to the specified amount of frames-per-second.
Display.destroy();
// Frees all the resources allocated for the display and destroys it.

Creating the LWJGL window

Since the method “Display.create()” throws an “LWJGLException”, the try-catch clause will resemble the following.

try {
	Display.setDisplayMode(new DisplayMode(640, 480));
	Display.setTitle("Episode 2 - A fresh display!");
	Display.create();
} catch (LWJGLException e) {
	e.printStackTrace();
	Display.destroy();
	System.exit(1);
}

Updating the window in the game loop

The game loop is a continuous loop in which input handling, game logic, and resource management is done. The loop will be broken if the user requests the display to be closed.

while (!Display.isCloseRequested()) {
	// While no attempt to close the display is made..
	// Put render code here.
	// Put input handling code here.
	Display.update();
	// Refresh the display and poll input.
	Display.sync(60);
	// Wait until 16.67 milliseconds have passed. (Maintain 60 frames-per-second)
}

Destroying the LWJGL window

After the game loop has been broken, the resources have to be cleaned up. In this case of this program, the only thing that needs to be cleaned up in the display itself.

Display.destroy();

For some inexplicable reason the window does not immediately close when the user clicks on the close button – it lingers for several seconds. Fixing this consists of placing the following line after “Display.destroy();”.

System.exit(0);

This quits the Java Virtual Machine. Keep in mind that any code written after this will not be executed.

Entire source code

import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.LWJGLException;
 
public class YourFirstDisplay {
	public static void main(String[] args) {
		try {
			Display.setDisplayMode(new DisplayMode(640, 480));
			Display.setTitle("A fresh display!");
			Display.create();
		} catch (LWJGLException e) {
			e.printStackTrace();
			Display.destroy();
			System.exit(1);
		}
		while(!Display.isCloseRequested()) {
			Display.update();
			Display.sync(60);
		}
		Display.destroy();
		System.exit(0);
	}
}

Conclusion

That concludes this tutorial. If you have any questions please leave a comment below.

17 thoughts on “LWJGL Window

  1. leviN

    On my Pc the program is running as it should but if I try to run it on my Laptop I get a Exception which says:”PixelFormat is not accellerated”
    I already updated my graphic drivers but it still throws this Exception!
    Please Help!!!

    Reply
  2. Elvis

    Hi,

    I am having a huge problem trying to create a game graphics engine from scratch without using any known libraries like jME or LWJGL, or J3D, etc. The reason why, is because I am a college student, and in Spring 2017, my professor will be teaching an exclusive Video Game Programming course for which I want to do a 3D video game.

    I don’t know whether I am doing things wrong or right, but all my attempts at Backface removal, and collision detection, etc, you know the important things that make a game realistic, tend to come up wrong, or so I think, since I have not been really successful at it.

    My professor taught the Video Game Programming course this past Spring 2015, for which I sat on the course the whole semester, and learned the basics. But he does not teach a complete 3D perspective of how to do a video game in 3D. The only things he showed us in 3D is, D*x/z and D*y/z and how draw a Cube.

    You would probably say, my professor is not very good at 3D video game programming…And you are right. He is only expecting 2D, but I don’t want to do 2D. I want to learn 3D.

    I found LWJGL library, and I found your videos on youtube, in which you mention that if one would want to be coached to see the video description. So I found your website, and now, I am trying to beg you to help me.

    I don’t need to do a video game library from scratch the way the professor is expecting everyone to do in the class, but I would like to learn how to use LWJGL so I can build my video game from it.

    So I am asking can you help me with this, Please?

    You can contact me directly at the email I posted.

    Thank you.

    Reply
  3. Pingback: render/draw or input first? | Question and Answer

  4. Pingback: render/draw or input first? | DL-UAT

  5. Pingback: EXCEPTION_ACCESS_VIOLATION when running any LWJGL code

  6. Anonymous

    Can you tell me what is a try – catch clause because I dont know what it is I only know basic java example if statements arithmetic loops (not all of them) arrays user input using scanner. Im learning it at school

    Reply
    1. David

      Try tries to perform an operation. If for some reason the operation “throws” an error, the catch statement will “catch” it.

      Reply
    1. Anonymous

      I have a similar issue. I’m on a mac and the display just flashes and seems to show the rest of the screen broken up.

      Reply

Leave a Reply to ellykadenyo Cancel reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>