Using JOGL in a Java applet: Difference between revisions

From JogampWiki
Jump to navigation Jump to search
No edit summary
Line 132: Line 132:
* Put your <tt>OneTriangle.java</tt> and <tt>OneTriangleAWT.java</tt> files inside the subdirectory.
* Put your <tt>OneTriangle.java</tt> and <tt>OneTriangleAWT.java</tt> files inside the subdirectory.
* Copy all the JOGL JARs into the applet directory as described [https://jogamp.org/wiki/index.php/Using_JOGL_in_Java_Web_Start#Setting_up_the_application_directory here].
* Copy all the JOGL JARs into the applet directory as described [https://jogamp.org/wiki/index.php/Using_JOGL_in_Java_Web_Start#Setting_up_the_application_directory here].
* Copy <tt>applet-launcher.jar</tt> into the applet directory. Currently this file is not part of the JOGL distribution. I built it from source I downloaded from [https://github.com/sgothel/applet-launcher Sven's repository].


= Compiling and JARing your program =
= Compiling and JARing your program =

Revision as of 02:05, 2 August 2011

You can use JOGL in a Java applet, which lets you run a Java program embedded in a web page. This page shows an example of how to do this. The example program just draws one triangle that fills a fixed-size frame in a web page.

Base class

We'll use the same triangle-drawing base class for this example that we did in the Java Web Start example. Copy the code to a file called OneTriangle.java.

Applet class

Now we need a simple class that extends java.applet.Applet to form the top level of our program. Copy this code to a file called OneTriangleAWTApplet.java.

package name.wadewalker.jogl2tests.onetriangle;

import java.applet.*;
import java.awt.*;

import javax.media.opengl.GLAnimatorControl;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.GLProfile;
import javax.media.opengl.awt.GLCanvas;

import com.jogamp.opengl.util.FPSAnimator;

/**
 * A minimal applet that draws with JOGL in a browser window.
 *
 * @author Wade Walker
 */
@SuppressWarnings("serial")
public class OneTriangleAWTApplet extends Applet {

    private GLAnimatorControl glanimatorcontrol;

    public void init() {
        GLProfile.initSingleton( false );
        setLayout( new BorderLayout() );

        final GLCanvas glcanvas = new GLCanvas();
        glcanvas.addGLEventListener( new GLEventListener() {
            
            @Override
            public void reshape( GLAutoDrawable glautodrawable, int x, int y, int width, int height ) {
                OneTriangle.setup( glautodrawable.getGL().getGL2(), width, height );
            }
            
            @Override
            public void init( GLAutoDrawable glautodrawable ) {
            }
            
            @Override
            public void dispose( GLAutoDrawable glautodrawable ) {
            }
            
            @Override
            public void display( GLAutoDrawable glautodrawable ) {
                OneTriangle.render( glautodrawable.getGL().getGL2(), glautodrawable.getWidth(), glautodrawable.getHeight() );
            }
        });
    
        glcanvas.setSize( getSize() );
        add( glcanvas, BorderLayout.CENTER );
        glanimatorcontrol = new FPSAnimator( glcanvas, 30 );
    }

    public void start() {
        glanimatorcontrol.start();
    }
    
    public void stop() {
        glanimatorcontrol.stop();
    }
    
    public void destroy() {
    }
}

Applet web page

Finally, the applet needs a web page to display in. Note that this file sets the fixed size of the applet window. Copy this code to a file called OneTriangleApplet.html.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>OneTriangle Applet Test</title>
</head>
<body>

<P>
Test of OneTriangle as an applet.
</P>

<P>

<applet code="org.jdesktop.applet.util.JNLPAppletLauncher"
      width=600
      height=400
      archive="applet-launcher.jar,
               newt.all.jar,
               nativewindow.all.jar,
               jogl.all.jar,
               gluegen-rt.jar,
               onetriangle.jar">
   <param name="codebase_lookup" value="false">
   <param name="subapplet.classname" value="name.wadewalker.jogl2tests.onetriangle.OneTriangleAWTApplet">
   <param name="subapplet.displayname" value="OneTriangle Applet">
   <param name="noddraw.check" value="true">
   <param name="progressbar" value="true">
   <param name="jnlpNumExtensions" value="1">
   <param name="jnlpExtension1" value="JOGL.jnlp">
   <param name="jnlp_href" value="OneTriangleApplet.jnlp">
</applet>

</P>

<P>
The applet should be above this.
</P>

</body>
</html>

Setting up the applet directory

  • Create a directory to hold your applet.
  • Put your OneTriangleApplet.html file in that new directory.
  • Create a subdirectory name/wadewalker/jogl2tests/onetriangle inside your applet directory. Or if you changed the packages of the files above, create a subdirectory that matches your package names.
  • Put your OneTriangle.java and OneTriangleAWT.java files inside the subdirectory.
  • Copy all the JOGL JARs into the applet directory as described here.
  • Copy applet-launcher.jar into the applet directory. Currently this file is not part of the JOGL distribution. I built it from source I downloaded from Sven's repository.

Compiling and JARing your program

You can compile and JAR the program exactly as we did for the Java Web Start case.

Writing the JNLP files

New-style Java applets use two Java Network Launching Protocol (JNLP) files to find the JAR files that make up the applet. Create both JNLP files exactly as in the Java Web Start case.

Signing your JARs

All the JARs in your applet must be signed to work correctly in a web browser. Do this just as in the Java Web Start case.

Running your applet in a browser

To run your applet in a browser on Windows, right-click the OneTriangleApplet.html file and click "Open with > Internet Explorer|Chrome|Firefox|Safari". The result should look like this:

Applet OneTriangle test