Setting Up LWJGL with NetBeans

Introduction
This tutorial will explain how to setup LWJGL with the NetBeans IDE. Requirements: You should have NetBeans (and Java) installed and should be familiar with creating a new Java project in NetBeans and extracting Zip files. Note: X.X anywhere denotes the version number of LWJGL that you are specifically downloading and setting up. Top

Downloading and Setting Up LWJGL
Here are the first steps that are required for any IDE. Top
 * 1) Create a dedicated folder for LWJGL and remember its location.  This folder will be the permanent residence of LWJGL on your system.  Optionally, name it "lwjgl-X.X" to denote it from earlier/later versions.
 * 2) Download LWJGL from here into the folder.
 * 3) * lwjgl-X.X.zip is the only one required.
 * 4) * (Recommended.) Download lwjgl-docs-X.X.zip if you want to be able to see LWJGL's Javadoc within NetBeans.
 * 5) * (Recommended.) Download lwjgl-source-X.X.zip if you want to be able to navigate to LWJGL's source code within NetBeans.
 * 6) * (Optional.) Download lwjgl_applet-X.X.zip if you want to be able to create Java Applets with LWJGL.  This exceeds the scope of this tutorial; for more information, please go to the LWJGL Applet Tutorials.
 * 7) Extract lwjgl-X.X.zip and lwjgl-source-X.X.zip.
 * 8) * lwjgl-docs-X.X.zip does not need to be extracted.

Setting Up LWJGL in NetBeans
Top
 * 1) Open up NetBeans.
 * 2) Go to Tools|Libraries in the main menu.
 * 3) * [[File:NetBeans1.png]]
 * 4) Click on the New Library button.
 * 5) * [[File:NetBeans2.png]]
 * 6) Type in LWJGL or LWJGL-X.X to distinguish it from earlier/later versions or any other name that you want for the Library Name.
 * 7) * [[File:NetBeans3.png]]
 * 8) Always make sure that the correct Library is selected; you don't want to accidentally modify another Library.  Now select the Classpath tab for your newly created Library and then click the Add JAR/Folder... button.
 * 9) * [[File:NetBeans4.png]]
 * 10) Go to the folder where you extracted lwjgl-X.X.zip and then go into the jar folder.  The only JARs that you really need are lwjgl.jar, lwjgl_util.jar, and jinput.jar.  You can select multiple files by holding down the Ctrl key while you click so that you can do this in just one step.
 * 11) * [[File:NetBeans5.png]]
 * 12) The rest of these steps are optional.
 * 13) Select the Sources tab and then click the Add JAR/Folder... button.  Next, go to the folder where you extracted lwjgl-source-X.X.zip and then go into the src folder.  Select the generated and java folders.
 * 14) * Note: Advanced users.  If you want to only select a Zip file for whatever reason, use the Ant build script here.
 * 15) * [[File:NetBeans6.png]]
 * 16) Select the Javadoc tab and then click the Add JAR/Folder... button.  Finally, select wherever you put lwjgl-docs-X.X.zip, and we are done.  Phew.  *Wipes sweat from forehead.*

Setting Up a Project to Use LWJGL in NetBeans
Top
 * 1) Create a new Java Application Project in NetBeans from File|New Project... in the main menu.
 * 2) Add the created Library from  to your project.  You can do this 3 different ways:
 * 3) * (Shown in image below). Right click your project's Libraries node and then select Add Library....
 * 4) * Right click your project's node and then select Properties. Then select the Libraries category.
 * 5) * Go to File|Project Properties in the main menu and then select the Libraries category.
 * 6) * [[File:NetBeans7.png]]
 * 7) Your project should now look like this:
 * 8) * [[File:NetBeans8.png]]
 * 9) Go to your project's properties.  You can do this 2 different ways:
 * 10) * (Shown in image below). Right click your project's node and then select Properties.
 * 11) * Go to File|Project Properties in the main menu.
 * 12) * [[File:NetBeans9.png]]
 * 13) Finally, you need to tell NetBeans where the Native Libraries (i.e, DLL, JNILIB, DYLIB, SO files) are for your system so that the natives are linked when running.
 * 14) * Select the Run category and then type the following into the VM Options, replacing what is in bold to suit your system:
 * -Djava.library.path=/native/
 * 1) ** Note: If your path has any spaces in it, you'll need to surround it in quotes like this:
 * -Djava.library.path="My Spacey Folder/lwjgl-X.X/native/windows"
 * 1) ** Note: This only means that you can run the program inside of NetBeans.  To run it outside of NetBeans, run this command:
 * java -Djava.library.path=  -jar  .jar
 * For more information on distribution, please go to the General FAQ.
 * 1) * [[File:NetBeans10.png]]

Testing the Full Setup
 import static org.lwjgl.opengl.GL11.*; import static org.lwjgl.util.glu.GLU.*;
 * 1) Add the following import to a class in your project: import org.lwjgl.LWJGLException;
 * 2) Right click on the import and then select Navigate|Go to Source.  Make sure that the actual code is there and that nothing looks like the following: //compiled code&#10;throw new RuntimeException("Compiled Code");
 * 3) Right click on the import again and then select Show Javadoc.  This should open up a new window/tab through your web browser with documentation explaining the code.
 * 4) Finally, try compiling, building, and running this simple test code:

import java.io.IOException; import java.util.logging.FileHandler; import java.util.logging.Level; import java.util.logging.Logger; import org.lwjgl.LWJGLException; import org.lwjgl.input.Keyboard; import org.lwjgl.input.Mouse; import org.lwjgl.opengl.Display; import org.lwjgl.opengl.DisplayMode;

/** * @author jediTofu * @see LWJGL Home Page */ public class Main { public static final int DISPLAY_HEIGHT = 480; public static final int DISPLAY_WIDTH = 640; public static final Logger LOGGER = Logger.getLogger(Main.class.getName);

private int squareSize; private int squareX; private int squareY; private int squareZ;

static { try { LOGGER.addHandler(new FileHandler("errors.log",true)); }   catch(IOException ex) { LOGGER.log(Level.WARNING,ex.toString,ex); } }

public static void main(String[] args) { Main main = null; try { System.out.println("Keys:"); System.out.println("down - Shrink"); System.out.println("up   - Grow"); System.out.println("left - Rotate left"); System.out.println("right - Rotate right"); System.out.println("esc  - Exit"); main = new Main; main.create; main.run; }   catch(Exception ex) { LOGGER.log(Level.SEVERE,ex.toString,ex); }   finally { if(main != null) { main.destroy; }   }  }

public Main { squareSize = 100; squareX = 0; squareY = 0; squareZ = 0; }

public void create throws LWJGLException { //Display Display.setDisplayMode(new DisplayMode(DISPLAY_WIDTH,DISPLAY_HEIGHT)); Display.setFullscreen(false); Display.setTitle("Hello LWJGL World!"); Display.create;

//Keyboard Keyboard.create;

//Mouse Mouse.setGrabbed(false); Mouse.create;

//OpenGL initGL; resizeGL; }

public void destroy { //Methods already check if created before destroying. Mouse.destroy; Keyboard.destroy; Display.destroy; }

public void initGL { //2D Initialization glClearColor(0.0f,0.0f,0.0f,0.0f); glDisable(GL_DEPTH_TEST); glDisable(GL_LIGHTING); }

public void processKeyboard { //Square's Size if(Keyboard.isKeyDown(Keyboard.KEY_DOWN)) { --squareSize; }   if(Keyboard.isKeyDown(Keyboard.KEY_UP)) { ++squareSize; }

//Square's Z   if(Keyboard.isKeyDown(Keyboard.KEY_LEFT)) { ++squareZ; }   if(Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) { --squareZ; } }

public void processMouse { squareX = Mouse.getX; squareY = Mouse.getY; }

public void render { glClear(GL_COLOR_BUFFER_BIT); glLoadIdentity;

//Draw a basic square glTranslatef(squareX,squareY,0.0f); glRotatef(squareZ,0.0f,0.0f,1.0f); glTranslatef(-(squareSize >> 1),-(squareSize >> 1),0.0f); glColor3f(0.0f,0.5f,0.5f); glBegin(GL_QUADS); glTexCoord2f(0.0f,0.0f); glVertex2f(0.0f,0.0f); glTexCoord2f(1.0f,0.0f); glVertex2f(squareSize,0.0f); glTexCoord2f(1.0f,1.0f); glVertex2f(squareSize,squareSize); glTexCoord2f(0.0f,1.0f); glVertex2f(0.0f,squareSize); glEnd; }

public void resizeGL { //2D Scene glViewport(0,0,DISPLAY_WIDTH,DISPLAY_HEIGHT);

glMatrixMode(GL_PROJECTION); glLoadIdentity; gluOrtho2D(0.0f,DISPLAY_WIDTH,0.0f,DISPLAY_HEIGHT); glPushMatrix;

glMatrixMode(GL_MODELVIEW); glLoadIdentity; glPushMatrix; }

public void run { while(!Display.isCloseRequested && !Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) { if(Display.isVisible) { processKeyboard; processMouse; update; render; }     else { if(Display.isDirty) { render; }       try { Thread.sleep(100); }       catch(InterruptedException ex) { }     }      Display.update; Display.sync(60); } }

public void update { if(squareSize < 5) { squareSize = 5; }   else if(squareSize >= DISPLAY_HEIGHT) { squareSize = DISPLAY_HEIGHT; } } }

Top Back to Main Wiki Page