This is a Java Program to Create a Banner Using Applet
We have to write a program in Java such that it creates a moving banner where the banner moves at intervals of 1 second.
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.
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.
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.
/*Java Program to Create a Banner using Applet*/
import java.applet.*;
import java.awt.*;
public class Banner extends Applet implements Runnable
{
String text = " Sample Banner ";
Thread t;
//Initialize the applet
public void init()
{
setBackground(Color.white);
}
//Function to start the thread
public void start()
{
t = new Thread(this);
t.start();
}
//Function to execute the thread
public void run()
{
while(true)
{
try
{
repaint();
//Delay each thread by 1000ms or 1 seconds
Thread.sleep(1000);
//Shift the first character of banner text to the last postion
text = text.substring(1) + text.charAt(0);
}
catch(Exception e)
{
}
}
}
//Function to draw text
public void paint(Graphics g)
{
g.setFont(new Font("TimesRoman",Font.BOLD,15));
g.drawString(text,200,200);
}
}
/*
<applet code = Banner.class width=500 height=500>
</applet>
*/
To compile and run the program use the following commands :
>>>javac Banner.java >>>appletviewer Banner.java
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.
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.
- Apply for Java Internship
- Practice Information Technology MCQs
- Check Programming Books
- Check Java Books
- Practice Programming MCQs