Difference between revisions of "Applets"

From JPCT
Jump to: navigation, search
(Applets)
 
(3 intermediate revisions by 3 users not shown)
Line 3: Line 3:
 
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.
  
The first thing you must do is set up a basic outline for your [http://www.term-paper-research.com/custom-research-paper.html research papers] 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.
+
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 :
 
Here is a simple Hello World example using software-rendering mode :
Line 194: Line 194:
  
 
Just substitute your own files and class names.
 
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:
 
<pre>AddType application/x-lzma .lzma</pre>
 
 
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.
 
  
 
----
 
----
Line 210: Line 201:
  
 
--[[User:Paulscode|Paulscode]] 22:44, 6 April 2009 (UTC)
 
--[[User:Paulscode|Paulscode]] 22:44, 6 April 2009 (UTC)
 +
 +
[[Category:jPCT]]
 +
[[Category:Applets]]

Latest revision as of 00:36, 2 February 2013

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.

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;

    @Override
    // 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);
        box.build();

        // 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:
    @Override
    public void run()
    {
        while( alive )
        {
            // Have the box rotate:
            box.rotateY( 0.01f );

            // Tell the applet to repaint itself:
            this.repaint();

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

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

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

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

        buffer.display( g, 0, 0 );
    }

    // End the main game loop:
    @Override
    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:

<html>
	<head>
		<title>Hello World</title>
	</head>
	<body>
		<applet code="HelloWorld"
		      width=640
		      height=480
		      archive="HelloWorld.jar,
			       jpct.jar">
		</applet>
	</body>
</html>

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 ))
            buffer.displayGLOnly();
            myCanvas.repaint();

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:

jpct.jar
lzma.jar
lwjgl_util_applet.jar
lwjgl.jar.pack.lzma
lwjgl_util.jar.pack.lzma
jinput.jar.pack.lzma
res.jar.lzma
windows_natives.jar.lzma
linux_natives.jar.lzma
macosx_natives.jar.lzma
solaris_natives.jar.lzma

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:

<html>
	<head>
		<title>Hello World</title>
	</head>
    <body>
        <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"
                    value="HelloWorld.jar,
                           jpct.jar,
                           lwjgl.jar.pack.lzma,
                           jinput.jar.pack.lzma,
                           lwjgl_util.jar.pack.lzma,
                           res.jar.lzma">
                <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">
        </applet>
    </body>
</html>

Just substitute your own files and class names.


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)