Java Program to Create a Banner using Applet

This is a Java Program to Create a Banner Using Applet

Problem Description

We have to write a program in Java such that it creates a moving banner where the banner moves at intervals of 1 second.

Expected Input and Output

For creating a banner, we can have the following set of input and output.

To View the Banner :

When the program is executed,
it is expected that a banner is created,
and the banner movies at intervals of 1 second.
Problem Solution

1. Create a thread.
2. Delay the each execution of program by 1 seconds.
3. For each execution of thread, shift the first character of the banner text to the last position.
4. Repaint the applet.

Program/Source Code

Here is source code of the Java Program to create a banner. The program is successfully compiled and tested using javac compiler on Fedora 30. The program output is also shown below.

  1. /*Java Program to Create a Banner using Applet*/
  2. import java.applet.*;
  3. import java.awt.*;
  4. public class Banner extends Applet implements Runnable
  5. {
  6.     String text = " Sample Banner ";
  7.     Thread t;
  8.     //Initialize the applet
  9.     public void init()
  10.     {
  11.         setBackground(Color.white);
  12.     }
  13.     //Function to start the thread
  14.     public void start()
  15.     {
  16. 	t = new Thread(this);
  17. 	t.start();
  18.     }
  19.     //Function to execute the thread
  20.     public void run()
  21.     {
  22.         while(true)
  23. 	{
  24. 	    try
  25. 	    {
  26. 	        repaint();
  27. 		//Delay each thread by 1000ms or 1 seconds
  28.                 Thread.sleep(1000);
  29. 		//Shift the first character of banner text to the last postion
  30.                 text = text.substring(1) + text.charAt(0);
  31. 	    }
  32. 	    catch(Exception e)
  33. 	    {
  34. 	    }
  35. 	}
  36.     }
  37.     //Function to draw text
  38.     public void paint(Graphics g)
  39.     {
  40. 	g.setFont(new Font("TimesRoman",Font.BOLD,15));
  41. 	g.drawString(text,200,200);
  42.     }
  43. }
  44. /*
  45. <applet code = Banner.class width=500 height=500>
  46. </applet>
  47. */

To compile and run the program use the following commands :

Program Explanation

1. Create an object of the Thread class and start it. To start a thread it is required to implements the Runnable interface.
2. Delay the thread by using Thread.sleep(int) with time mentioned in milliseconds.
3. Shift the first character to the last to creating a moving banner.
4. Repaint the applet.

Runtime Test Cases

Here’s the run time test cases for creating a banner for different input cases.


Test case 1 – To View the Banner.

Test case 2 – To View the Banner.

Test case 3 – To View the Banner.


Sanfoundry Global Education & Learning Series – Java Programs.

If you find any mistake above, kindly email to [email protected]

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.