Applets and Applications

Programs that Function as Applets and as Applications

The following example shows how you might write a Java® program, so that it can function either as an applet or as an application. The program can run as an applet because it inherits JApplet and it can run as an application because it has a main routine. The code creates the UI components within a JPanel and then sets this panel to be the content pane for a JApplet or a JFrame. When the program runs as an applet, the JApplet class is used as the top level container. When the program runs as an application, we create a JFrame and use this as the top level container.

MyApp.java

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;

// This class can be run either as an Applet or as an Application.
 public class MyApp extends JApplet {
    // The RootPaneContainer interface is implemented by JApplet and by JFrame.
    // It specifies methods like setContentPane() and getContentPane().  The
    // content pane is of type java.awt.Container or one of its subclasses.
    RootPaneContainer mRPC;

    // This contructor is used when we run as an applet.
    public MyApp() {
        mRPC = this;
    }

    // This contructor is used when we run as an application.
    public MyApp(JFrame frame) {
        mRPC = frame;
    }

    // The init method is the place to put the code to initialize the applet.  The code to set up the
    // user interface usually goes here.  We avoid putting applet initialization code in applet constructors
    // because an applet is not guaranteed to have a full environment until the init method is called.
    public void init() {
        // We will put all our components in a JPanel and then set this panel
        // as the content pane for the applet or application.
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());

        JSlider slider = new JSlider(0,50,0);
        panel.add(slider, BorderLayout.SOUTH);
        final DrawingArea drawingArea = new DrawingArea();
        panel.add(drawingArea, BorderLayout.CENTER);

        slider.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
                JSlider source = (JSlider)e.getSource();
                if (!source.getValueIsAdjusting()) {
                    int offset = (int)source.getValue();
                    drawingArea.setOffset(offset);
                    drawingArea.repaint();
                }
            }
        });

        mRPC.setContentPane(panel);
    }

    // The start method is the place to start the execution of the applet.
    // For example, this is where you would tell an animation to start running.
    public void start() {
    }

    // The stop method is the place to stop the execution of the applet.
    // This is where you would tell an animation to stop running.
    public void stop() {
    }
 
    // The destroy method is where you would do any final cleanup that needs to be done.  The
    // destroy method is rarely required, since most of the cleanup can usually be done in stop().
    public void destroy() {
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        final MyApp app = new MyApp(frame);

        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                app.stop();
                app.destroy();
                System.exit(0);
            }
        });

        app.init();

        frame.setSize(400, 400);
        frame.setVisible(true);

        app.start();
    }
}
 

// A user interface component, which is to be added to the applet.
class DrawingArea extends JPanel {
    private int mOffset;

    public DrawingArea() {
        setBackground(Color.white);
    }

    public void setOffset(int offset) {
        mOffset = offset;
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setFont(new Font("Helvetica", Font.PLAIN, 24));
        g.setColor(Color.green);
        g.drawString("An Applet or an Application?", 10+mOffset, 50);
        g.drawString("That is the question.", 10+mOffset, 100);
    }
}

mypage.html

<HTML>

<APPLET CODE=MyApp.class WIDTH=400 HEIGHT=400>
</APPLET>