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.

Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn