Over a million developers have joined DZone.

5 Steps to Lightweight Java Game Library in NetBeans RCP

· Java Zone

Discover how AppDynamics steps in to upgrade your performance game and prevent your enterprise from these top 10 Java performance problems, brought to you in partnership with AppDynamics.

Let's get started with LWJGL in a window in a NetBeans Platform application. In the end, we'll have this simple canvas that I found somewhere on-line, in a TopComponent (in real life, the white shape is moving):

  1. Start by setting things up as described here, i.e., in a standard Java SE application in NetBeans IDE. Use those instructions as a simple environment where you can try out things outside the NetBeans Platform.

  2. Create a new NetBeans Platform application.

  3. Create a new module, call it "LWJGLCore", or anything else you like. In the Properties dialog of the module, go to Libraries and then "Wrapped JARs". Now wrap the JARs mentioned in the link in step 1 into your module. Switch to the Files window of the module in NetBeans IDE and the folder "release/modules/ext" should contain the three modules you wrapped. Create a new folder, "lib", and put the native JARs into it, i.e., look below at the screenshot and do not continue with the next step until your module has the folder and file structure that you see below.

  4. Now create this new class in your module. Note that I have no idea whether the code below is correct or optimal, it's simply here to show a simple class you can use to check whether your set up is working:
    public class GameLoop extends AWTGLCanvas {
        float angle = 0;
        public GameLoop() throws LWJGLException {
            Thread redraw = new Thread() {
                public void run() {
                    while (true) {
                        if (isVisible()) {
        protected void paintGL() {
            GL11.glOrtho(0, 640, 0, 480, 1, -1);
            GL11.glTranslatef(320, 240, 0.0f);
            GL11.glRotatef(angle, 0, 0, 1.0f);
            GL11.glVertex2i(-50, -50);
            GL11.glVertex2i(50, -50);
            GL11.glVertex2i(50, 50);
            GL11.glVertex2i(-50, 50);
            angle += 1;
            try {
            } catch (Exception e) {

  5. Add another class that defines the TopComponent and adds the canvas defined above to it:
    import java.awt.BorderLayout;
    import java.awt.Canvas;
    import org.openide.windows.TopComponent;
    @TopComponent.Description(preferredID = "LWJGLWindow")
    @TopComponent.Registration(mode = "editor", openAtStartup = true)
    public class LWJGLWindow extends TopComponent {
        public LWJGLWindow() {
            setLayout(new BorderLayout());
            try {
                Canvas can = new GameLoop();
                can.setSize(getWidth(), getHeight());
            } catch (Exception ex) {

Run the application and you'll see the same as shown in the screenshot with which this article started.


The Java Zone is brought to you in partnership with AppDynamics. AppDynamics helps you gain the fundamentals behind application performance, and implement best practices so you can proactively analyze and act on performance problems as they arise, and more specifically with your Java applications. Start a Free Trial.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}