Banner Applet cum Application

Example on Moving Banner and Applet cum Application that works applet as an application also

Banner is a string that moves either horizontally or vertically across the applet window. In the following program, the string "S N Rao" is moved horizontally. This is done both as applet and application ( Applet cum Application ).

Before going with this program, go through the multithreading topic as some concepts are required here. sleep() method of Thread class makes a running thread to become inactive for the specified time passed, as parameter, in milliseconds. Here, for every iteration, the thread becomes inactive for 110 milliseconds. To get the size of the applet window, we used getSize().width and getSize().height methods. clearRect() method of Graphics class clears the earlier image existing on the applet window. For every 110 milliseconds the old image is removed and the new one is drawn (or to say, old image is overridden with new one); this is how animation is implemented.

import java.awt.*;
import java.applet.*;
public class Move extends Applet implements Runnable
{
    String str = "Wishes from way2java.com";
    Thread t1 = null;
     public void init() 
     {
        t1 = new Thread(this);
        t1.start();
     }
     public void run () 
     {
         char ch1;
         while(true)                     // no condition checking
         {
             try 
             {
                repaint();               // it calls paint() implicitly
                Thread.sleep(110);  //  interval of 110 milliseconds
                ch1 = str.charAt(0);
                str = str.substring(1, str.length());
                str += ch1;
             } 
             catch(InterruptedException e) {}
        }
    }
    public void paint(Graphics g) 
    {
        g.drawString(str, 40, 100);
        showStatus("I am a banner programmer.");// display on status bar
    }
}

File Name: GoAhead.html


	


Applet cum ApplicationScreenshot of MovingBanner.html

ch1 = str.charAt(0);
str = str.substring(1, str.length());

charAt() method of String class returns the character available at the index position passed as parameter. With every iteration, the character changes as index number changes. The String class substring() method returns few characters of a string available in between the integer values passed as parameter.

Now Converting Application to Applet ( of Applet cum Application )

An application can be converted to an applet or vice versa. It is required to import two packages java.applet and java.awt for Applet and Graphics classes and if required, for event handling, java.awt.event package also.

Following are the simple steps to remember while migrating from an application to applet.

  1. Import java.applet and java.awt packages.
  2. Include init() method in place of constructor
  3. No main() method
  4. Write paint() method to draw something on the applet window
  5. Create one HTML file to give the address of applet and window size

2 thoughts on “Banner Applet cum Application”

  1. Well, i created a banner and a choose menu and also radio buttons in one program but the banner and choose menu are overlapping each other… how do i change the position of the moving banner?

Leave a Comment

Your email address will not be published.