3 Replies Latest reply: Jun 8, 2010 9:15 AM by gimbal2 RSS

    Starting with LWJGL

    843853
      Hey everyone, I'm working on a Java game as a personal project, hoping to use LWJGL. I've been programming for 8 years (as a hobby), so I can at least say I understand the basics :P. Unfortunately, I've never really made anything in Java before. I've used Netbeans as my C++ IDE of choice because of the excellent code completion tools, and I understand the concepts involved in rendering and OpenGL too. My problem is that I don't very well understand the process involved in beginning a LWJGL applet. The coding itself isn't hard, but I'm not sure about the html or the setup of the jars and whatnot.

      I have lwjgl extracted to c:\lwjgl\ and included as Compile jars in netbeans. I have a class (Main) that extends Applet, and a canvas extending AWTGLCanvas implementing paintGL and a constructor that creates a refreshing thread. The rendering/setup code is just a temporary copy-paste from a tutorial until I understand it well enough to rewrite it on my own, but unfortunately it isn't working. My jar is in the same folder as the html file, and the html file contains the following tag:
      <applet code="testgame.Main" archive="testgame.jar" width="640" height="480"></applet>
      When I open it in firefox, I get no errors, but the applet won't clear. I probably did something wrong with the thread code, but perhaps there is more to embedding an applet than I thought?

      I googled it and apparently there aren't many tutorials for LWJGL applets, so I decided to ask here. Thanks :)

      Here's my main file:
      package testgame;
      
      import java.applet.Applet;
      import java.awt.BorderLayout;
      import java.awt.Canvas;
      import org.lwjgl.LWJGLException;
      import org.lwjgl.opengl.AWTGLCanvas;
      import org.lwjgl.opengl.Display;
      import org.lwjgl.opengl.GL11;
      
      /**
       *
       * @author Owner
       */
      public class Main extends Applet {
      
          /**
           * @param args the command line arguments
           */
          @Override public void init() {
              setLayout(new BorderLayout());
      
              try {
                  Canvas can = new MainCanvas();
                  can.setSize(getWidth(),getHeight());
                  add(can);
              } catch (Exception ex) {
                  ex.printStackTrace();
              }
          }
          public class MainCanvas extends AWTGLCanvas {
              float angle = 0;
              public MainCanvas() throws LWJGLException {
                  Thread redraw = new Thread() {
                      @Override public void run() {
                          while (true) {
                              if (isVisible()) {
                                  repaint();
                              }
                              Display.sync(60);
                          }
                      }
                  };
                  redraw.setDaemon(true);
                  redraw.start();
              }
              @Override public 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) {
                }
              }
          }
      
      }