Java Technology  «Prev  Next»
Lesson 5 The Java AppletViewer
ObjectiveUse AppletViewer to run Java applets

Java AppletViewer

Use the AppletViewer to run Java applets without a Web browser.
The Java AppletViewer is a tool used to run applets. It works, essentially, like stripped-down Web browser. The AppletViewer bypasses the additional overhead of a Web browser and is also guaranteed to support the version of the SDK you are using.
The AppletViewer is incredibly useful because it provides a simple, efficient way to test Java applets. However, this does not mean you shouldn't also test applets in a real Web browser. You should always test applets with a real Web browser since that is what users will ultimately be using to run them. The AppletViewer does not operate directly on applets. Instead, you provide it with a Web page that contains an embedded Java applet. The AppletViewer then ignores everything but the <APPLET> HTML tag and displays a window containing the executing applet.


Embedding Java code in an AppletViewer

Here's a breakdown of embedding Java code in an AppletViewer, along with a simple example and explanation:
Understanding Applets (A Brief History) Applets were small Java programs designed to run within web browsers. They were popular in the early days of the internet but have been largely deprecated due to security concerns and the rise of more modern web technologies.
Why AppletViewer? AppletViewer is a tool included in the Java Development Kit (JDK). It allows you to test and run applets outside of a web browser environment. This is helpful for development and debugging.
Example: Simple Bouncing Ball Applet
import java.applet.Applet;
import java.awt.*;

public class BouncingBall extends Applet implements Runnable {
    int x = 50, y = 50, dx = 2, dy = 3;

    Thread animator;

    public void start() {
        animator = new Thread(this);
        animator.start();
    }

    public void stop() {
        animator = null;
    }

    public void run() {
        while (animator != null) {
            x += dx;
            y += dy;

            // Bounce off edges
            if (x < 0 || x > getWidth() - 20) dx = -dx;
            if (y < || y > getHeight() - 20) dy = -dy;

            repaint();

            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public void paint(Graphics g) {
        g.setColor(Color.red);
        g.fillOval(x, y, 20, 20);
    }
}


Explanation:
  1. Import Statements: Necessary for applet functionality and graphics.
  2. BouncingBall Class: Extends `Applet` and implements `Runnable` (for animation).
  3. Variables:
    • `x`, `y`: Ball coordinates.
    • `dx`, `dy`: Changes in x and y for movement.
    • `animator`: Thread to control animation.
  4. start() and stop(): Manage the animation thread.
  5. run(): Core animation logic:
    • Update ball position.
    • Handle bouncing off applet boundaries.
    • Repaint the applet.
    • Introduce a short delay to control animation speed.
  6. paint(): Draw the ball.

Running the Applet:
  1. Save: Save the code as `BouncingBall.java`.
  2. Compile:
       javac BouncingBall.java
       
  3. Create HTML (Optional): Create a simple HTML file like this (if you want to embed in a webpage):
    <html>
    <body>
    <applet code="BouncingBall.class" width="400" height="300"></applet>
    </body>
    </html>
    
  4. Run with AppletViewer:
       appletviewer BouncingBall.html
       
    (or just `appletviewer BouncingBall.class` if you didn't create the HTML)

Important Note: Applets are a legacy technology. Modern web development typically uses JavaScript, HTML5 Canvas, or other frameworks for interactive content.


Running an applet in the AppletViewer

Running an applet in the AppletViewer involves executing a command at the command-line and providing the appropriate Web page name.
Following is an example of running the TicTacToe applet with the AppletViewer:
appletviewer TicTacToe.html
  1. The actual name of the AppletViewer program
  2. The name of the Web page containing the TicTacToe applet, provided as the only argument to the AppletViewer.


To run the applet, you have two choices.
  1. You can use the applet viewer, a program that is included with the Java Software Development Kit from Oracle. You simply start the applet viewer, giving it the name of the HTML file that contains your applets: The applet viewer only shows the applet, not the HTML text (see An Applet in the Applet Viewer). You view applets with the applet viewer or a Java-enabled browser.
  2. You can also show the applet inside any Java enabled web browser, such as Chrome or Mozilla. An Applet in a Web Browser shows the applet running in a browser. As you can see, both the text and the applet are displayed.


Applet Viewer - Exercise

Click the Exercise link to run a Java applet using the AppletViewer.
Applet Viewer - Exercise

SEMrush Software