How to Create a Banner Using Applet?
Last Updated :
24 Oct, 2023
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
Similar Reads
How to Draw a Car using Java Applet?
An Applet is a Java program that runs in a web browser. An Applet is a Java class that extends the java.applet.Applet class, which means that to create any Applet, you need to import this class and extend it in your Applet class. The applet does not have a main() method. Applets are designed to embe
2 min read
How to Read a File using Applet?
In this article, we will learn how to read the content of the file using Java and display those contents using Applet. Approach UsedThe user should type the name of the file in the input field that asks for the filename in the output applet window. If the file is already present, it will display the
2 min read
How to Write to a File Using Applet?
In this article, we will learn how to write a file using Applet and grant access to write files using Applet. ApproachThe applet output window contains three fields. Those are the file name field, Text area field, and save button field. File name field: It asks the user to enter the file name to wri
3 min read
How to Create a Voting Application in Android?
In general, voting means comparing two or more entities on the basis of certain conditions. In this article, we will create a simple voting application that uses two different programming languages namely Java and Python, and ask the user to vote for their preferred language. A sample GIF is given b
3 min read
How to Display Image using Applet?
In this article, we will be using the Applet to display the image in proper layout. Here, we will display the image along with the text in the Applet Viewer. Approach for Displaying Images Using AppletWe need to import the essential packages like Applet and AWT for customization.When the packages ge
4 min read
How to Play Sound using Applet?
In this article, we will be using the Applet window to play the sound or music. Here, we will have buttons like Play, Pause, and Restart. Using these buttons we will manage the sound in the Applet window. Approach for Playing Sound Using AppletWe need to import the essential packages like Applet and
4 min read
Create Banners using React and Tailwind CSS
We will build a responsive banner component using React and Tailwind CSS. Tailwind CSS allows us to create highly customizable and modern UI components with minimal effort. The banner will include a heading and subheading and a call to action button styled for responsiveness and accessibility.Prereq
3 min read
How to Create an Event Listener in Applet?
In this article, we will be creating the event Listener in the Applet window. Here, we will use the ActionListener on Button and MouseListener when the event has occurred through mouse activities. Approach for Implementing Event Listener AppletWe need to import the essential packages like Applet and
5 min read
Java Program to Goto a Link Using Applet
In this article, we shall be animating the applet window to show the goto link in Java Applet. A Goto link is basically, any particular link that redirects us from one page to another. Approach to Implement Goto a Link using AppletCreate an applet with the two buttons.Add ActionListener to the butto
2 min read
How to create a two tone banner in bootstrap ?
Bootstrap is a CSS framework used to design webpages. It is the most widely used CSS framework all over due to its simplicity and ease of implementation. Bootstrap is used along with HTML and JavaScript to make the webpages interactive. Banners are section in the webpage that call for extra attentio
3 min read