Example applet with progress bar

This page presents all the source code necessary for an example applet using a progress bar. The applet doesn't really do anything useful. It puts up a console sort of window and lets the user type in the window, but it doesn't process the input in any manner.

To keep this example as small as possible, I've removed everything from the MyConsole class that didn't seem absolutely necessary. One side effect is that you can't close the window! Inserting shutdown code is left as an exercise for the reader.

MyConsole.java
import java.awt.*;

public class MyConsole extends Frame
{
    public MyConsole()
    {
        super("My Console!");

        setLayout(null);
        addNotify();
        resize(insets().left + insets().right + 609, insets().top + insets().bottom + 326);
        ConsoleTextField=new TextArea(17,69);
        ConsoleTextField.setFont(new Font("Courier",Font.PLAIN,12));
        add(ConsoleTextField);
        ConsoleTextField.reshape(insets().left + 14,insets().top + 8,574,285);
    }

    public synchronized void show()
    {
    	move(50, 50);
    	super.show();
    }


    TextArea ConsoleTextField;
}

MyAppletRunner.java
public class MyAppletRunner
{
    public MyAppletRunner(ProgressBar unusedProgressBar)
    {
        MyConsole x = new MyConsole();
        x.show();
    }
}

ProgressBar.java
import java.awt.*;

public class ProgressBar extends Frame
{
    private              int Count;
    private              int Max;
    private static final int FrameBottom = 24;

    public ProgressBar (String Title, int TotalItems)
    {
        super(Title);

        Count = 0;
        Max   = TotalItems;

        // Allowing this to be resized causes more trouble than it is worth
        // and the goal is for this to load and launch quickly!
        setResizable(false);

        setLayout(null);
        addNotify();
        resize (insets().left + insets().right + 379,
                insets().top + insets().bottom + FrameBottom);
    }

    public synchronized void show()
    {
        move(50, 50);
        super.show();
    }

    // Update the count and then update the progress indicator.  If we have
    // updated the progress indicator once for each item, dispose of the
    // progress indicator.
    public void updateProgress ()
    {
        ++Count;

        if (Count == Max)
            dispose();
        else
            repaint();
    }


    // Paint the progress indicator.
    public void paint (Graphics g)
    {
        Dimension FrameDimension  = size();
        double    PercentComplete = (double)Count * 100.0 /(double)Max;
        int       BarPixelWidth   = (FrameDimension.width * Count)/ Max;

        // Fill the bar the appropriate percent full.
        g.setColor (Color.red);
        g.fillRect (0, 0, BarPixelWidth, FrameDimension.height);

        // Build a string showing the % completed as a numeric value.
        String s        = String.valueOf((int)PercentComplete) + " %";

        // Set the color of the text.  If we don't, it appears in the same color
        // as the rectangle making the text effectively invisible.
        g.setColor (Color.black);

        // Calculate the width of the string in pixels.  We use this to center
        // the string in the progress bar window.
        FontMetrics fm       = g.getFontMetrics(g.getFont());
        int StringPixelWidth = fm.stringWidth(s);

        g.drawString(s, (FrameDimension.width - StringPixelWidth)/2, FrameBottom);
    }

    public boolean handleEvent(Event event)
    {
        if (event.id == Event.WINDOW_DESTROY)
        {
            dispose();
            return true;
        }

        return super.handleEvent(event);
    }
}

ClassPreLoader.java
class ClassPreLoader
{
    public ClassPreLoader(ProgressBar theProgressBar, String classArray[])
    {
        int i;
        for (i = 0; i < classArray.length; ++ i)
        {
            try
            {
                Class c = Class.forName(classArray[i]);

                theProgressBar.updateProgress();
            }
            catch (Exception e)
            {
                // Do nothing.
            }
        }
    }
}

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

public class MyApplet extends Applet
{
    public void init()
    {
        // The classes needed by your applet.  They have the same
        // name as the *.class files (but without the .class).
        // Don't bother including ClassPreLoader or ProgressBar.
        String classArray[] =
        {
            "MyConsole"
        };

        super.init();
        setLayout(null);
        resize(6,6);

        ProgressBar theProgressBar  =
                new ProgressBar("My applet downloading...", classArray.length);
        theProgressBar.show();

        // Preload all the necessary classes, updating the progress bar as
        // they are loaded.
        ClassPreLoader myPreLoader = new ClassPreLoader(theProgressBar, classArray);

        // The constructor for class MyAppletRunner implements the code that
        // is traditionally in the init() method.
        MyAppletRunner myApplet    = new MyAppletRunner(theProgressBar);
    }

    public boolean handleEvent(Event event)
    {
        return super.handleEvent(event);
    }
}

MyWebPage.html
    <HTML>
    <HEAD>
    <TITLE> A simple program </TITLE>
    </HEAD>
    <BODY>

    <APPLET CODE="MyApplet.class" WIDTH=283 HEIGHT=190></APPLET>

    </BODY>

    </HTML>
Creating download progress bars for applets