Setting Up LWJGL with NetBeans

From LWJGL
(Difference between revisions)
Jump to: navigation, search
(Added some more.)
(Done, I think.)
Line 1: Line 1:
 
== Introduction ==
 
== Introduction ==
  
This tutorial will show you how to setup [http://www.lwjgl.org/ LWJGL] with the [http://www.netbeans.org/ NetBeans] [http://en.wikipedia.org/wiki/Integrated_development_environment IDE].<br/>
+
This tutorial will explain how to setup [http://www.lwjgl.org/ LWJGL] with the [http://www.netbeans.org/ NetBeans] [http://en.wikipedia.org/wiki/Integrated_development_environment IDE].<br/>
 
'''Requirements''':  You should have NetBeans (and Java) installed and should be familiar with creating a new Java project in NetBeans and extracting Zip files.<br/>
 
'''Requirements''':  You should have NetBeans (and Java) installed and should be familiar with creating a new Java project in NetBeans and extracting Zip files.<br/>
'''Note''':  ''X.X'' anywhere denotes the version number of LWJGL that you are downloading and setting up.<br/>
+
'''Note''':  ''X.X'' anywhere denotes the version number of LWJGL that you are specifically downloading and setting up.<br/>
 
[[#top|Top]]
 
[[#top|Top]]
  
Line 16: Line 16:
 
#* (Recommended.)  Download '''lwjgl-docs-X.X.zip''' if you want to be able to see LWJGL's [http://www.lwjgl.org/javadoc/ Javadoc] within NetBeans.
 
#* (Recommended.)  Download '''lwjgl-docs-X.X.zip''' if you want to be able to see LWJGL's [http://www.lwjgl.org/javadoc/ Javadoc] within NetBeans.
 
#* (Recommended.)  Download '''lwjgl-source-X.X.zip''' if you want to be able to navigate to LWJGL's source code within NetBeans.
 
#* (Recommended.)  Download '''lwjgl-source-X.X.zip''' if you want to be able to navigate to LWJGL's source code within NetBeans.
#* (Optional.)  Download '''lwjgl_applet-X.X.zip''' if you want to be able to create [http://en.wikipedia.org/wiki/Java_applet Java Applets] with LWJGL.  This exceeds the scope of this tutorial; for more information, please go to the [http://lwjgl.org/wiki/index.php?title=Main_Page#Applet LWJGL Applet tutorials].
+
#* (Optional.)  Download '''lwjgl_applet-X.X.zip''' if you want to be able to create [http://en.wikipedia.org/wiki/Java_applet Java Applets] with LWJGL.  This exceeds the scope of this tutorial; for more information, please go to the [[Main_Page#Applet|LWJGL Applet Tutorials]].
 
# Extract '''lwjgl-X.X.zip''' and '''lwjgl-source-X.X.zip'''.
 
# Extract '''lwjgl-X.X.zip''' and '''lwjgl-source-X.X.zip'''.
 
#* '''lwjgl-docs-X.X.zip''' does '''not''' need to be extracted.
 
#* '''lwjgl-docs-X.X.zip''' does '''not''' need to be extracted.
Line 34: Line 34:
 
# 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.
 
# 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.
 
#* [[File:NetBeans4.png]]<br/><br/>
 
#* [[File:NetBeans4.png]]<br/><br/>
# 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'''.  In most Operating Systems, you can select multiple files by holding down the '''Ctrl''' key while you click so that you can just do this in one step.
+
# 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.
 
#* [[File:NetBeans5.png]]<br/><br/>
 
#* [[File:NetBeans5.png]]<br/><br/>
 
# The rest of these steps are '''optional'''.
 
# The rest of these steps are '''optional'''.
Line 45: Line 45:
  
 
== Setting Up a Project to Use LWJGL in NetBeans ==
 
== Setting Up a Project to Use LWJGL in NetBeans ==
 +
 +
# Create a new Java Application Project in NetBeans from '''File|New Project...''' in the main menu.
 +
# Add the created Library from [[#Setting Up LWJGL in NetBeans]] to your project.  You can do this 3 different ways:
 +
#* (Shown in image below).  Right click your project's '''Libraries''' node and then select '''Add Library...'''.
 +
#* Right click your project's node and then select '''Properties'''.  Then select the '''Libraries''' category.
 +
#* Go to '''File|Project Properties''' in the main menu and then select the '''Libraries''' category.
 +
#* [[File:NetBeans7.png]]<br/><br/>
 +
# Your project should now look like this:
 +
#* [[File:NetBeans8.png]]<br/><br/>
 +
# Go to your project's properties.  You can do this 2 ways:
 +
#* (Shown in image below).  Right click your project's node and then select '''Properties'''.
 +
#* Go to '''File|Project Properties''' in the main menu.
 +
#* [[File:NetBeans9.png]]<br/><br/>
 +
# 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.
 +
#* '''Note''':  This only means that you can run the program inside of NetBeans.  To run it outside of NetBeans (i.e., by double clicking on the executable JAR in your '''dist''' folder), you will need to copy all of the Native Libraries into your '''dist''' folder where your executable JAR is located.  For more information on distribution, please go to the [[General_FAQ|General FAQ]].
 +
#* 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='''<lwjgl-X.X path>'''/native/'''<linux|macosx|solaris|windows>'''
 +
#** '''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"
 +
#* [[File:NetBeans10.png]]
 +
[[#top|Top]]
 +
 +
 +
 +
== Testing the Full Setup ==
 +
 +
# Add the following import to a class in your project:<br/><pre class="brush:java">import org.lwjgl.LWJGLException;</pre>
 +
# 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:<br/><pre class="brush:java">//compiled code</pre><pre class="brush:java">throw new RuntimeException("Compiled Code");</pre>
 +
# 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.
 +
# Finally, try compiling, building, and running this simple test code:
 +
<pre class="brush:java">
 +
import static org.lwjgl.opengl.GL11.*;
 +
import static org.lwjgl.util.glu.GLU.*;
 +
 +
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 <a href="http://lwjgl.org/">LWJGL Home Page</a>
 +
*/
 +
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;
 +
    }
 +
  }
 +
}
 +
 +
</pre>
 +
[[#top|Top]]<br/>
 +
[[Main_Page|Back to Main Wiki Page]]

Revision as of 01:01, 9 December 2010

Contents

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.

  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.
    • lwjgl-X.X.zip is the only one required.
    • (Recommended.) Download lwjgl-docs-X.X.zip if you want to be able to see LWJGL's Javadoc within NetBeans.
    • (Recommended.) Download lwjgl-source-X.X.zip if you want to be able to navigate to LWJGL's source code within NetBeans.
    • (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.
  3. Extract lwjgl-X.X.zip and lwjgl-source-X.X.zip.
    • lwjgl-docs-X.X.zip does not need to be extracted.

Top


Setting Up LWJGL in NetBeans

  1. Open up NetBeans.
  2. Go to Tools|Libraries in the main menu.
    • NetBeans1.png

  3. Click on the New Library button.
    • NetBeans2.png

  4. Type in LWJGL or LWJGL-X.X to denote it from earlier/later versions or any other name that you want for the Library Name.
    • NetBeans3.png

  5. 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.
    • NetBeans4.png

  6. 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.
    • NetBeans5.png

  7. The rest of these steps are optional.
  8. 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.
    • NetBeans6.png

  9. 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.*

Top


Setting Up a Project to Use LWJGL in NetBeans

  1. Create a new Java Application Project in NetBeans from File|New Project... in the main menu.
  2. Add the created Library from #Setting Up LWJGL in NetBeans to your project. You can do this 3 different ways:
    • (Shown in image below). Right click your project's Libraries node and then select Add Library....
    • Right click your project's node and then select Properties. Then select the Libraries category.
    • Go to File|Project Properties in the main menu and then select the Libraries category.
    • NetBeans7.png

  3. Your project should now look like this:
    • NetBeans8.png

  4. Go to your project's properties. You can do this 2 ways:
    • (Shown in image below). Right click your project's node and then select Properties.
    • Go to File|Project Properties in the main menu.
    • NetBeans9.png

  5. 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.
    • Note: This only means that you can run the program inside of NetBeans. To run it outside of NetBeans (i.e., by double clicking on the executable JAR in your dist folder), you will need to copy all of the Native Libraries into your dist folder where your executable JAR is located. For more information on distribution, please go to the General FAQ.
    • 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=<lwjgl-X.X path>/native/<linux|macosx|solaris|windows>
      • 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"
    • NetBeans10.png

Top


Testing the Full Setup

  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
    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 static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.util.glu.GLU.*;

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 <a href="http://lwjgl.org/">LWJGL Home Page</a>
 */
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

Personal tools
Namespaces

Variants
Actions
Navigation
Tools