5 Steps to Lightweight Java Game Library in NetBeans RCP
Join the DZone community and get the full member experience.
Join For FreeLet'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):
- 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.
- Create a new NetBeans Platform application.
- 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.
- 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) { } } }
- 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.
NetBeans
Rich client platform
Java (programming language)
Library
Opinions expressed by DZone contributors are their own.
Trending
-
Build a Simple Chat Server With gRPC in .Net Core
-
Building A Log Analytics Solution 10 Times More Cost-Effective Than Elasticsearch
-
Playwright JavaScript Tutorial: A Complete Guide
-
The SPACE Framework for Developer Productivity
Comments