Starting an Applet

Started by Super_Llama, June 08, 2010, 13:51:04

Previous topic - Next topic

Super_Llama

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? Looking at the page source for other lwjgl applets (minecraft), it looks like they use a loader. Do I need to do this, and how?

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) {
          }
        }
    }
 
}

kappa

In order to use LWJGL as an applet you need to use LWJGL's appletloader (also what Minecraft uses :) ).

The LWJGL Appletloader is included with the LWJGL download and found in lwjgl_util_applet.jar.

What the appletloader does is start itself as an applet, downloads the natives files and jars for you while showing a nice loading screen, then extracts the natives and added them to the jvm. It then simply passes control over to your lwjgl applet.

There is no java code needed to use the lwjgl appletloader, it is controlled entirely through html. You should download the lwjgl applet package from the download page for an example. (you can use this as a template if you like).

As for your lwjgl applet, you should be able to create it in netbeans an example template can be found here

if you need extra help you can also visit lwjgl irc channel where i'm sure ppl can help you in real time :) its found at #lwjgl on freenode.net use an irc client or the webchat

Super_Llama

So I need to copy the lwjgl jars into my dist folder, then use lwjgl_util_applet.jar as the applet instead of testgame, and use param tags to tell it which junk to load? I hope I've got it right -- I'll try it and get back to you. I can't wait to get a programmable box capable of OpenGL in my browser, because once that's out of the way, the rest is pretty much the same stuff as any other project, though much more dependent on classes and threads. Java may not be my favorite language, but the game I'm trying to make is going to be a 2D side-scrolling browser-based online RTS/Shooter mix with heavy use of pixel shaders, so LWJGL seemed like the best path to take. Once I've got the applet working, I'm going to see if I can get a pixel-shaded object on the screen. If there are any peculiarities I need to know about that process, I'd be grateful to be informed.

P.S: It'd help if I knew which params are necessary in the loader-- otherwise I'm just going to be reading them all from minecraft's page source :-[

kappa

Quote from: Super_Llama on June 08, 2010, 14:38:06
P.S: It'd help if I knew which params are necessary in the loader-- otherwise I'm just going to be reading them all from minecraft's page source :-[

As mentioned in my previous post download the lwjgl applet package from the download page (direct link)
See the source code of the html file in that package, it explains which parameters you need and which are optional.

Also see the Gears Applet linked above, it should give you a basic example what you need to adapt your application into in order to get it up running quickly.

I'd recommend that you avoid the lzma and pack200 stuff initially and just use jars for everything and get your applet to work. The lzma and pack200 step can be tricky to get right but well worth the extra hassle later on.

Super_Llama

Ah, okay. Downloading now, and thanks for the fast replies.

That package is just what I needed - a sample applet that isn't just a bunch of "put this in your code" tutorials. Trying to plug in my jar, but I can't figure out what file it is. I think it's res.jar.lzma, but I don't really understand what an lzma is.

*10 seconds later*

Nevermind, I just replaced "res.jar.lzma" in the code with "testgame.jar" and I've got the spinning square on my screen now ;D

Now that I have a programmable box, I'm going to look into using GLSL. Thanks for all the help!!!

Matthias

In case you later want to create .pak.lzma files I suggest this Ant task as it can do the pak200 file creation and lzma compression at the same time.
Example usage can be found in the TWL examples build.xml