Geertjan is a DZone Zone Leader and has posted 468 posts at DZone. You can read more from them at their website. View Full User Profile

5 Steps to Lightweight Java Game Library in NetBeans RCP

02.13.2011
| 5810 views |
  • submit to reddit

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.

 

Published at DZone with permission of its author, Geertjan Wielenga.

Comments

Andrea Maietta replied on Wed, 2011/02/16 - 6:47am

A small reminder: the @TopComponent.Description and @TopComponent.Registration interfaces can only be found from the specification version 3.7 of the Window System API module; the specification version is only 3.3 in NetBeans 6.9.1 and 3.5 in NetBeans 7.0 Beta, so you'll have to download and install a nightly build and everything will go smoothly.

Thanks again, Geertjan!

Matt Coleman replied on Fri, 2012/11/30 - 2:42am in response to: Andrea Maietta

thank for this heads up Andrew..i appreciate it buffalo search engine optimization 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.