Open In App

How to Create a Banner Using Applet?

In this article, we shall be building a Java Applet that shows a scrolling banner with a message that moves continually from right to left. In this article, you will learn the fundamentals of Java Applet Basics by creating a banner using Applet.

Note: java.applet package package has been deprecated in Java 9 and later versions,as applets are no longer widely used on the web , you may use JDK version 1.8.0_381 as used in this article.



Elements or Components Required For Creating Banner

As a result, in order to create a banner for your website, you’ll need a plan (HTML), a way to make it seem decent (CSS), the things you want in the banner (content), a location to write the plan (a text editor or IDE), and a means of seeing it (a web browser). It’s like designing your own unique poster just for your website.

Prerequisites:

Make sure you have the following prerequisites before we begin the tutorial:



javac -version
java -version

Program For Creating a Banner Using Applet

There are certain examples demonstrating the use of Applet in Java as shown below:

Example 1:

Here’s a simple Java code snippet for creating a banner using Applet:




// Java Program to demonstrate
// use for Applet to create Banner
import java.applet.*;
import java.awt.*;
  
public class BannerApplet
    extends Applet implements Runnable {
    String message = "Welcome to My Banner!";
    Thread t = null;
    int state;
    boolean stopFlag;
  
    // Initialize the applet.
    public void init()
    {
        setBackground(Color.DARK_GRAY);
        setForeground(Color.GREEN);
    }
  
    // Start the thread.
    public void start()
    {
        t = new Thread(this);
        stopFlag = false;
        t.start();
    }
  
    // Entry point for the thread.
    public void run()
    {
        char ch;
        while (true) {
            try {
                repaint();
                Thread.sleep(250);
                ch = message.charAt(0);
                message = message.substring(1) + ch;
                if (stopFlag)
                    break;
            }
            catch (InterruptedException e) {
            }
        }
    }
  
    // Pause the banner.
    public void stop()
    {
        stopFlag = true;
        t = null;
    }
  
    // Display the banner in the center and with a larger
    // font size.
    public void paint(Graphics g)
    {
  
        // Change the font and size as needed
        Font font = new Font("Arial", Font.BOLD, 36);
        g.setFont(font);
        FontMetrics fm = g.getFontMetrics(font);
        int x = (getSize().width - fm.stringWidth(message))
                / 2;
        int y = (getSize().height - fm.getHeight()) / 2;
        g.drawString(message, x, y);
    }
}

Output:

Explanation of the above method:

In this example, we build a Java Applet that shows a scrolling banner with a message that moves continually from right to left. The banner’s actions and appearance within the applet are specified by this code.

A scrolling banner will be displayed by the Java applet we’re making in this code, called BannerApplet. The functions of each component of the code are listed below:

Example 2:

Here’s a simple Java code snippet for creating a banner using Applet:




// Java Program to demonstrate
// use for Applet to create Banner
import java.applet.*;
import java.awt.*;
  
public class BannerApplet extends Applet implements Runnable {
    String message = "Welcome to My Banner!";
    int yCoordinate = 0;
    Thread t = null;
  
    // Initialize the applet.
    public void init() {
        setBackground(Color.DARK_GRAY);
        setForeground(Color.GREEN);
    }
  
    // Start the thread.
    public void start() {
        t = new Thread(this);
        t.start();
    }
  
    // Entry point for the thread.
    public void run() {
        while (true) {
            try {
                Thread.sleep(5);  // Adjust this value for the desired speed (lower value for faster speed)
                yCoordinate++;
                if (yCoordinate > getHeight()) {
                    yCoordinate = -20; // Reset the y-coordinate to the top of the applet
                }
                repaint();
            } catch (InterruptedException e) {
            }
        }
    }
  
    // Display the scrolling banner.
    public void paint(Graphics g) {
        g.clearRect(0, 0, getWidth(), getHeight());
        Font font = new Font("Arial", Font.BOLD, 36); // Change the font and size as needed
        g.setFont(font);
        FontMetrics fm = g.getFontMetrics(font);
        int x = (getSize().width - fm.stringWidth(message)) / 2;
        g.drawString(message, x, yCoordinate);
    }
}

Output:

Running the Applet Program

Step 1: Writing the Java Applet Code

‘BannerApplet’ is an Applet that is defined by this code. It shows a message as a banner that scrolls constantly from right to left.

Step 2: Compile and Run the Applet

Save the Java code to a ‘.java’ file, for example, ‘BannerApplet.java’. Open your terminal or command prompt and navigate to the directory containing the ‘java’ file. Compile the code using the ‘javac’ command:

javac BannerApplet.java

‘.class’ file will be produced as a result. You can use a straightforward HTML file to start the applet:




<!DOCTYPE html>
<html>
<head>
    <title>My Banner Applet</title>
</head>
<body>
    <applet code="BannerApplet.class" width="400" height="50"></applet>
</body>
</html>

Save this HTML file in the same directory as your compiled ‘.class’ file as ‘BannerApplet.html’

Step 3: Running the Applet using appletviewer

Open your terminal or command prompt and navigate to the directory containing the ‘java’ file. Run the applet using ‘appletviewer ‘ command.

appletviewer BannerApplet.html

Article Tags :