Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

5 Steps to Lightweight Java Game Library in NetBeans RCP

DZone's Guide to

5 Steps to Lightweight Java Game Library in NetBeans RCP

· Java Zone ·
Free Resource

Verify, standardize, and correct the Big 4 + more– name, email, phone and global addresses – try our Data Quality APIs now at Melissa Developer Portal!

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() {
                @Override
                public void run() {
                    while (true) {
                        if (isVisible()) {
                            repaint();
                        }
                        Display.sync(60);
                    }
                }
            };
            redraw.setDaemon(true);
            redraw.start();
        }
    
        @Override
        protected void paintGL() {
            GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
            GL11.glMatrixMode(GL11.GL_PROJECTION_MATRIX);
            GL11.glLoadIdentity();
            GL11.glOrtho(0, 640, 0, 480, 1, -1);
            GL11.glMatrixMode(GL11.GL_MODELVIEW_MATRIX);
            GL11.glPushMatrix();
            GL11.glTranslatef(320, 240, 0.0f);
            GL11.glRotatef(angle, 0, 0, 1.0f);
            GL11.glBegin(GL11.GL_QUADS);
            GL11.glVertex2i(-50, -50);
            GL11.glVertex2i(50, -50);
            GL11.glVertex2i(50, 50);
            GL11.glVertex2i(-50, 50);
            GL11.glEnd();
            GL11.glPopMatrix();
            angle += 1;
            try {
                swapBuffers();
            } 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() {
            setDisplayName("Demo");
            setLayout(new BorderLayout());
            try {
                Canvas can = new GameLoop();
                can.setSize(getWidth(), getHeight());
                add(can);
            } catch (Exception ex) {
            }
        }
    }

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

 

Developers! Quickly and easily gain access to the tools and information you need! Explore, test and combine our data quality APIs at Melissa Developer Portal – home to tools that save time and boost revenue. Our APIs verify, standardize, and correct the Big 4 + more – name, email, phone and global addresses – to ensure accurate delivery, prevent blacklisting and identify risks in real-time.

Topics:

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}