Open In App

How to Create a Banner Using Applet?

Last Updated : 24 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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

  • HTML File: Consider the HTML file as the layout of your website. It instructs the placement of your banner and every other element on the page on your computer.
  • CSS Styles: Consider CSS styles as a magic wand that may be used to alter the appearance of elements on your website. You can use it to choose fonts, colors, and the size of objects.
  • Banner Content: This is the information that you wish to appear in your banner. Words, images, or anything else you like are acceptable. It’s comparable to the posters you hang on your wall.
  • Text Editor or Integrated Development Environment (IDE): The instructions for your web page are written in a text editor or IDE. Similar to writing a letter or a narrative, but with instructions for the machine instead of yourself.
  • Web Browser: This is the application you use to browse websites. Using it, you may check how your banner appears on your website.
  • Knowledge of HTML and CSS: Understanding HTML and CSS is required to develop a website and create a visually stunning banner, just like with learning a new language.

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:

  • Java Development Kit(JDK) : Make sure your system is running JDK version 1.8.0_381 or earlier. It is available for download from the Oracle website.To check which version is there in your system use the given cmd commands :

javac -version
java -version

  • Integrated Development Environment (IDE) or text editor: To write and edit Java code, you’ll need a text editor or IDE.
  • HTML File: You’ll need an HTML file in order to embed the Java Applet within a web page.

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




// 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:

  • init(): The applet is initialized using this function. It determines the starting colors of our banner, which are green for the text and dark gray for the background, respectively.
  • start(): This method is called as soon as the applet is started . It initializes a new thread that executes the run() method.
  • run(): This section of the code is where all the magic happens. It repeatedly repaints the applet inside of a loop to give the impression that the banner is moving. To produce the scrolling effect, it also shifts the message’s characters.
  • stop(): Calling this function will pause the scrolling banner and end the scrolling thread.
  • paint(): The scrolling message is shown on the applet through the paint() method. It displays the message on the applet at a certain location , and as the message changes, it appears to scroll from right to left.

Example 2:

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

Java




// 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:

HTML




<!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


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads