Ticker Widget/Applet

Although there are hundreds of ticker widgets available over the net, better than these, I wrote this widget particularly so that I could integrate this with my Site-Guide. Also, this ticker is mainly intended for using as a status bar, but what the heck, I can also use it in an applet anywhere. And the best part is I am also giving it free with source-code! The behaviour of this ticker is different compared to others because if this ticker can show the full text, it will not scroll.
 
 

Disclaimer:
You can download this free for use in your personal pages for non-commercial purposes. This source code and the .class files are given on an as-is basis and I assume no responsiblity of anything. You can change the source code and use it if you want, but however, make sure my name remains there :-)

Download TICKER.ZIP!

To deploy this in your site, download the zip file and extract the .class files. Then in your HTML code, put a tag like the on below. Replace "Hello! This is ...." with whatever text you want ! Simple!!

Source Code for TickerApplet

HTML code :
<applet code=TickerTestApplet.class width=300 height=30>
<PARAM NAME=TEXT VALUE="Hello! This is a test for Nagendra's Ticker Widget...">
</applet>

File TickerTestApplet.java
/********************************************************************/
//  File    : TickerTestApplet.java
//  Author  : Nagendra R Setty
//            nagendra@home.com
//  Date    : 01/21/97
//  A simple Ticker Applet which uses my Ticker class.
//     The text to be displayed will be specified in the applet's
// HTML tag, parameter name = TEXT 
/********************************************************************/

import java.awt.*;
import java.applet.*;
import Ticker;

public class TickerTestApplet extends Applet {
   Ticker m_ticker;

   public void init () {
      setLayout (new BorderLayout());
      // This applet uses a parameter in the HTML
      // tag called TEXT. get the parameter and
      // set it as the text to the Ticker.
      String text = getParameter ("TEXT");
      m_ticker = new Ticker (text);
      add (m_ticker);
      add ("Center", m_ticker);
   }

   public void start (){
      m_ticker.start ();
   }

   public void stop (){
      m_ticker.stop ();
   }
};

/********************************************************************/
// end of TickerTestApplet.java
/********************************************************************/
 

File Ticker.java
/********************************************************************/
// file     : Ticker.java
// Author   : Nagendra R Setty
//            nagendra@home.com
// Date     : 01/21/97
//    Ticker widget which keeps scrolling a given line of text.
/********************************************************************/

import java.awt.*;
import java.lang.Runnable;

public class Ticker extends Panel implements Runnable{
   Thread    m_thread;
   Rectangle m_rectControl;
   String    m_text;
   int       m_delay;
   int       m_xpos;
   int       m_ypos;
   int       m_textWidth;
   boolean   m_textChanged;
   Color     m_color;

   public Ticker () {
      m_delay = 25;
      m_text = "Nagendra's Ticker Widget. This is a very simple ticker widget";
      m_ypos = 18;
      m_textChanged = true;
      m_color = Color.orange;
   }
   public Ticker (String s) {
      m_delay = 25;
      m_text = s;
      m_ypos = 18;
      m_textChanged = true;
      m_color = Color.orange;
   }

   // Use this mehtod to change color.
   public void setColor (Color clr) {
      m_color = clr;
   }
   // Use this mehtod to change the text.
   public void setText (String s) {
      m_text = s;
      if (m_thread != null) {
         m_thread.stop ();
         m_thread = null;
      }
      m_textChanged = true;
      repaint ();
   }

   public void paint (Graphics g) {
      m_rectControl = bounds ();
 
      g.setColor (Color.black);
      g.fillRect (m_rectControl.x, m_rectControl.y,
      m_rectControl.width, m_rectControl.height);

      g.setColor (m_color);
      Font f = new Font ("Helvetica", Font.PLAIN, 12);
      g.setFont (f);
      FontMetrics fm = g.getFontMetrics ();
      m_textWidth = fm.stringWidth (m_text);
      // Can the text fit int view ??
      if (m_textWidth <= (m_rectControl.width - 6)){
         // Can fit text: Paint and kill the
         // thread if it is running
         g.drawString (m_text, 3, m_ypos);
         if (m_thread != null) {
            m_thread.stop ();
            m_thread = null;
         }
         m_textChanged = false;
      }
      else {
         // No Text can't fit. If this paint was due to
         // a setText operation, then reset the text position
         if (m_textChanged) {
            m_xpos = m_rectControl.width - 3;
            m_textChanged = false;
         }
         // draw the text in the position specified by
         // m_xpos variable
         g.drawString (m_text, m_xpos, m_ypos);
         // Start the thread if it is not started yet
         if (m_thread == null) {
            m_thread = new Thread (this);
            m_thread.start ();
         }
      }
   }

   // If you're using this in an applet, call the start
   // and stop methods from the Applet's start and stop
   // methos. This will ensure that the threa is killed
   // when your browser is minimized and restarts when
   // you activate it again.
   public void stop (){
      if (m_thread != null) {
         m_thread.stop ();
         m_thread = null;
      }
   }
   public void start () {
      repaint ();
   }

   public void resize (int w, int h) {
      super.resize (w, h);
      repaint ();
   }
   public void resize (Dimension d) {
      super.resize (d);
      repaint ();
   }

   // the thread handler.
   public synchronized void run () {
      while (true) {
         Graphics g = getGraphics ();
         try {
            // Draw the text where you drew last time, (position
            // remembered by 'm_xpos'.
            Font f = new Font ("Helvetica", Font.PLAIN, 12);
            g.setFont (f);
            g.setColor (Color.black);
            g.drawString (m_text, m_xpos, m_ypos);

            // Here, check if the text has completely
            // scrolled through. if it has, reset position.
            // otherwise, move to the left by 3 pixels.
            if (m_xpos < (10 - m_textWidth))
               m_xpos = m_rectControl.width - 3;
            else
               m_xpos -= 3;

            g.setColor (m_color);
            g.drawString (m_text, m_xpos, m_ypos);
         }
         catch (NullPointerException n) {}

         try {
            m_thread.sleep (m_delay);
         }
         catch (InterruptedException e) { }
      }
   }

   // cleanup work! If this was an applet, this could've been
   // done in the Applet's stop () method. However, this is
   // a widget and so this has to clean up itself.
   protected void finalize () {
      if (m_thread != null)
         m_thread.stop ();
      }
};

/********************************************************************/
// end of Ticker.java
/********************************************************************/
 

e-mail me: nagendra@home.com
home