Setting Up LWJGL with NetBeans

From LWJGL
(Difference between revisions)
Jump to: navigation, search
m
m (Ant build script link.)
 
Line 41: Line 41:
 
# The rest of these steps are '''optional'''.
 
# The rest of these steps are '''optional'''.
 
# 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.
 
# 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.
 +
#* '''Note''':  Advanced users.  If you want to only select a Zip file for whatever reason, use the [http://en.wikipedia.org/wiki/Apache_Ant Ant] build script [http://lwjgl.org/forum/index.php/topic,3706.msg20603.html#msg20603 here].
 
#* [[File:NetBeans6.png]]<br/><br/>
 
#* [[File:NetBeans6.png]]<br/><br/>
 
# 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.*
 
# 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.*

Latest revision as of 05:14, 28 January 2011

Contents


[edit] 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


[edit] 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 into the folder.
    • 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


[edit] 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 distinguish 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.
    • Note: Advanced users. If you want to only select a Zip file for whatever reason, use the Ant build script here.
    • 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


[edit] 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 different 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.
    • 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"
      • 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=<path> -jar <name>.jar
        For more information on distribution, please go to the General FAQ.
    • NetBeans10.png

Top


[edit] 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