Program to display rolling banner using repaint( ) method of an Applet

This is a very interesting applet program in which we are displaying a rolling banner like marquee tag in HTML. The banner message is passed in applet's <PARAM> tag.


PROGRAM
import java.awt.*;
import java.applet.*;

/*
<applet code="ParamBanner" width=300 height=50>
<param name="message" value="Java makes the Web move!">
</applet>
*/

public class ParamBanner extends Applet implements Runnable {
 String msg;
 Thread t = null;
 int state;
 boolean stopFlag;
 // Set colors and initialize thread.
 public void init() {
  setBackground(Color.cyan);
  setForeground(Color.red);
 }
 // Start thread
 public void start() {
  msg = getParameter("message");
  if(msg == null) msg = "Message not found.";
   msg = " " + msg;
  t = new Thread(this);
  stopFlag = false;
  t.start();
 }
 // Entry point for the thread that runs the banner.
 public void run() {
  char ch;
  // Display banner
  for( ; ; ) {
   try {
    repaint();
    Thread.sleep(250);
    ch = msg.charAt(0);
    msg = msg.substring(1, msg.length());
    msg += ch;
    if(stopFlag)
     break;
   } catch(InterruptedException e) {}
  }
 }
 // Pause the banner.
 public void stop() {
  stopFlag = true;
  t = null;
 }
 // Display the banner.
 public void paint(Graphics g) {
  g.drawString(msg, 50, 30);
 }
}

OUTPUT

C:\>javac ParamBanner.java
C:\>appletviewer ParamBanner.java



Comments

Popular posts from this blog

Program to define a class 'employee' with data members as empid, name and salary. Accept data for 5 objects using Array of objects and print it.

Define a class Student with four data members such as name, roll no.,sub1, and sub2. Define appropriate methods to initialize and display the values of data members. Also calculate total marks and percentage scored by student.

Program to draw Smiley in Applet