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