Difference between revisions of "Applets"

Jump to: navigation, search
Line 1: Line 1:
=== Applets ===
Using jPCT in an applet is not a whole lot different from using it in an application.  There are only a couple of additional steps you must follow.
Using jPCT in an applet is not a whole lot different from using it in an application.  There are only a couple of additional steps you must follow.

Revision as of 19:20, 7 April 2009


Using jPCT in an applet is not a whole lot different from using it in an application. There are only a couple of additional steps you must follow.

The first thing you must do is set up a basic outline for your applet. It will normally be written around some sort of "game loop" in which you update the state of your world and tell the applet to repaint itself.

Here is a simple Hello World example using software-rendering mode :

import java.awt.Graphics;
import javax.swing.JApplet;

import com.threed.jpct.FrameBuffer;
import com.threed.jpct.Object3D;
import com.threed.jpct.Primitives;
import com.threed.jpct.World;

public class HelloWorld extends JApplet implements Runnable
    private Object3D box;

    private FrameBuffer buffer = null;
    private World world = null;

    private boolean alive = true;
    private boolean initialized = false;

    // Initialize all components of the applet
    public void init()
        world = new World();  // create a new world
        World.setDefaultThread( Thread.currentThread() );

        // Add some light to the scene
        world.setAmbientLight(0, 255, 0);

        // create a new buffer to draw on:
        buffer = new FrameBuffer( getWidth(), getHeight(),
                                  FrameBuffer.SAMPLINGMODE_NORMAL );

        // Create the box:
        box = Primitives.getBox(13f, 2f);

        // add the box our world:
        world.addObject( box );

        // set the camera's position:
        world.getCamera().setPosition( 50, -50, -5 );

        // Look at the box:
        world.getCamera().lookAt( box.getTransformedCenter() );

        // Finished initializing.  Now it is safe to paint.
        initialized = true;

        // Start the main "Game Loop":
        new Thread( this ).start();

    // Main Game Loop:
    public void run()
        while( alive )
            // Have the box rotate:
            box.rotateY( 0.01f );

            // Tell the applet to repaint itself:

            // Sleep for a bit:
                Thread.sleep( 10 );
            catch( Exception e )

    // Draw the scene:
    public void paint( Graphics g )
        // Make sure jPCT is finished initializing:
        if( !initialized )

        buffer.clear();   // erase the previous frame

        // render the world onto the buffer:
        world.renderScene( buffer );
        world.draw( buffer );

        buffer.display( g, 0, 0 );

    // End the main game loop:
    public void destroy()
    	alive = false;

To load your applet, you will of course need an HTML file. The following would load the above "Hello World" example:

		<title>Hello World</title>
		<applet code="HelloWorld"

To use hardware-rendering instead, you would first need to make a couple of minor changes to the code:

import java.awt.BorderLayout;
import java.awt.Canvas;
    // Global variable:
    Canvas myCanvas;
        // Inside init() (REPLACE FrameBuffer.SAMPLINGMODE_NORMAL)
        buffer = new FrameBuffer( getWidth(), getHeight(),
                                  FrameBuffer.SAMPLINGMODE_HARDWARE_ONLY );
        myCanvas = buffer.enableGLCanvasRenderer();
        add( myCanvas, BorderLayout.CENTER);
        myCanvas.setVisible( true );
            // Inside paint() (REPLACE buffer.display( g, 0, 0 ))

The HTML for loading your applet changes significantly when using hardware-rendering. You must load it with the LWJGL Applet Loader, which you can download from: http://www.lwjgl.org

Instructions for using the LWJGL Applet Loader:

You must place the following files somewhere:


Plus the two loader image files (in this example I am using paulscodelogo.png and paulscodeprogress.gif)

Next you need to create an html file to load the applet. The following will load the above "Hello World" example:

		<title>Hello World</title>
        <applet code="org.lwjgl.util.applet.AppletLoader"
            archive="lwjgl_util_applet.jar, lzma.jar" codebase="." width="640" height="480">
                <param name="al_title" value="HelloWorld">
                <param name="al_main" value="HelloWorld">
                <param name="al_logo" value="paulscodelogo.png">
                <param name="al_progressbar" value="paulscodeprogress.gif">
                <param name="al_jars"
                <param name="al_windows" value="windows_natives.jar.lzma">
                <param name="al_linux" value="linux_natives.jar.lzma">
                <param name="al_mac" value="macosx_natives.jar.lzma">
                <param name="al_solaris" value="solaris_natives.jar.lzma">
                <param name="al_version" value="1.0">

Just substitute your own files and class names.

Create a text file named ".htaccess". If you are running Windows, it will complain, because that is not a valid filename, according to Bill. You'll have to name it something like ".htaccess.txt", then rename it later after uploading it.

Edit the file you created, with notepad or other text editor. Add the line:

AddType application/x-lzma .lzma

Save your file, and FTP it to wherever lwjgl_util_applet.jar and lzma.jar are located. If necessary, rename your file ".htaccess" (remove the ".txt" extension) Make sure you are allowed to use .htaccess before doing this! Some of the things that .htaccess is able to do can compromise a server configuration. For this reason, some web hosting companies may require them to be set up by an admin, so don't get in trouble. You should CHMOD your .htaccess file to 644 (RW-R--R--) to prevent people from messing with it.

A more advanced example demonstrating loading 3ds files, parent/child relationships, rotating with the mouse, and switching between software and hardware rendering modes on the fly, can be found Here ( Source Code )

--Paulscode 22:44, 6 April 2009 (UTC)