How do I get and display an image in Applet?

To display an image in an applet we first need to obtain the image object itself. A call to applet’s getImage(URL url, String name) method help us to create an image object from the specified URL of the image.

For displaying the image on the applet’s screen we draw it in the paint() method using Graphics.drawImage() method.

package org.kodejava.example.applet;

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

public class AppletGetImage extends Applet {
    private Image logo;

    @Override
    public void init() {
        //
        // Get an Image object that can be painted on the Applet
        // screen. We need to supply the URL of the document as
        // the base location of the image and the location of the
        // image relative the the base URL. 
        //
        logo = getImage(getDocumentBase(), "/images/logo.png");
    }

    @Override
    public void paint(Graphics g) {
        g.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
        g.drawImage(logo, 10, 10, this);
    }
}

How do I get Applet’s document URL?

The code snippet below show you how to get the URL of the document (HTML, JSP, etc) where the Applet is embedded. To obtain this document URL we use the getDocumentBase() method call provided by the Applet class.

In the paint() method below we use the getDocumentBase() to create a URL as a link to an image to be displayed by our applet.

package org.kodejava.example.applet;

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

public class AppletDocumentBase extends Applet {
    private Image logo;

    @Override
    public void init() {
        //
        // Locates logo image base on the URL of the document
        // where the Applet is embedded which is returned by
        // the getDocumentBase() method call.
        //
        // eg. http://localhost:8080/images/logo.jpg
        //
        logo = getImage(getDocumentBase(), "/images/logo.png");
    }

    @Override
    public void paint(Graphics g) {
        g.setColor(Color.black);
        g.drawRect(0, 0, getWidth() - 1, getHeight() - 1);

        //
        // Draw the logo image on the Applet surface.
        //
        g.drawImage(logo, 10, 10, this);
    }
}

How do I display message in browser status bar?

In this applet example you’ll see how to display a message in browser status bar. To make the example a little bit more interesting we’ll display the current time as the message. The time will be update every on second during the life time of the applet.

package org.kodejava.example.applet;

import java.applet.Applet;
import java.awt.Graphics;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class TimeApplet extends Applet implements Runnable {
    private DateFormat formatter = null;
    private Thread t = null;

    public void init() {
        formatter = new SimpleDateFormat("hh:mm:ss");
        t = new Thread(this);
    }

    public void start() {
        t.start();
    }

    public void stop() {
        t = null;
    }

    public void paint(Graphics g) {
        Date now = Calendar.getInstance().getTime();
        //
        // Show the current time on the browser status bar
        //
        this.showStatus(formatter.format(now));
    }

    public void run() {
        int delay = 1000;
        try {
            while (t == Thread.currentThread()) {
                //
                // Repaint the applet every on second
                //
                repaint();
                Thread.sleep(delay);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

How do I change an applet background color?

By default the applet will usually have a gray background when displayed in a web browser. If you want to change it then you can call the setBackground(java.awt.Color) method and choose the color you want. Defining the background color in the init() method will change the color as soon as the applet initialized.

package org.kodejava.example.applet;

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;

public class BackgroundColorApplet extends Applet {
    public void init() {
        //
        // Here we change the default gray color background of an 
        // applet to yellow background.
        //
        setBackground(Color.YELLOW);
    }

    public void paint(Graphics g) {
        g.setColor(Color.BLACK);
        g.drawString("Applet background example", 0, 50);
    }
}

How do I read an applet parameters?

package org.kodejava.example.applet;

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;

public class AppletParameterExample extends Applet {
    private String name = "";

    public void init() {
        //
        // Here we read a parameter named name from the applet tag definition
        // in our html file.
        //
        name = getParameter("name");
    }

    public void paint(Graphics g) {
        g.setColor(Color.BLUE);
        g.drawString("Hello " + name + ", Welcome to the Applet World.", 0, 0);
    }
}

Now we have the applet code ready. To enable the web browser to execute the applet create the following html page.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <title>Parameterized Applet</title>
</head>
<body>
    <applet code="org.kodejava.example.applet.AppletParameterExample" 
            height="150" width="350">
        <param name="name" value="Mr. Bean" />
    </applet>
</body>
</html>

How do I create a Hello World Applet?

The code below demonstrate the very basic of Java applet. Applet is a small Java application that can be embedded on the web browser.

package org.kodejava.example.applet;

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

public class HelloWorldApplet extends Applet {
    public void init() {
    }

    public void start() {
    }

    public void stop() {
    }

    public void destroy() {
    }

    public void paint(Graphics g) {
        g.setColor(Color.GREEN);
        g.drawString("Hello World", 50, 100);
    }
}

To display the applet we need to create an HTML document. Here is a simple example of the document.

<html>
<head>
    <title>Hello World Applet</title>
</head>

<body>
    <applet 
        code="org.kodejava.example.applet.HelloWorldApplet" 
        height="250" 
        width="250">
    </applet>
</body>
</html>

You can now load the applet in your browser or by using the appletviewer utility.