Using JOGL in a Java applet

From JogampWiki
Revision as of 01:31, 2 August 2011 by Wwalker (talk | contribs) (Created page with 'You can use JOGL in a [http://download.oracle.com/javase/6/docs/technotes/guides/javaws/ Java applet, which lets you run Java programs embedded in a web page. This page shows an …')
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

You can use JOGL in a [http://download.oracle.com/javase/6/docs/technotes/guides/javaws/ Java applet, which lets you run Java programs 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() {
    }
}