FileDocCategorySizeDatePackage
Scroller.javaAPI DocExample2711Mon Oct 16 19:44:02 BST 2000None

Scroller.java

// File:    Scroller.java (.good)
// T Balls
// Feb 1998
// Applet to scroll text on to a screen area

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

public class Scroller extends Applet implements Runnable
{  public void init()
   {  // get data from HTML
      String s = getParameter( "FontSize" );
      int points;
      if( s == null )
      {  points = 10;
      }
      else
      {  points = Integer.parseInt( s.trim() );
      }

      s = getParameter( "Delay" );
      if( s == null )
      {  delay = 50;
      }
      else
      {  delay = Integer.parseInt( s.trim() );
      }
      
      s = getParameter( "Lines" );
      int lines;
      if( s == null )
      {  lines = 0;
      }
      else
      {  lines = Integer.parseInt( s.trim() );
      }
      if( lines > 0 )
      {  text = new String[ lines ];
         int line;
         for( line = 0; line < lines; line += 1 )
         {  String name = "Line" + line;
            text[line] = getParameter( name );
         }
      }
      setLayout( new BorderLayout() );
      display.setFont( new Font( "Serif", Font.PLAIN, points ) );
      add( display ); // Center is default (1.1/1.0.2)
      
      // Thread bits
      t = new Thread( this );
      t.start();
      suspended = true;;
   }

   private volatile boolean suspended;
   private TextArea display = new TextArea( "_", 5, 0, TextArea.SCROLLBARS_NONE );
   private String[] text = null;
   private Thread t;
   private int delay = 50;
   
   public void start()
   {  synchronized(this)
      {  suspended = false;
         notify();
      }
   }

   public void stop()
   {  suspended = true;
   }

   public void destroy()
   {  Thread toGo = t;
      t = null;
      toGo.interrupt();
   }

   public void run()
   {  Thread current = Thread.currentThread();
      while( t == current )
      {  try
         {  while( suspended )
            {  synchronized(this)
               {  wait();
               }
            }
            int line;
            for( line=0; line < text.length; line += 1 )
            {  for( int ch = 0; ch < text[line].length(); ch += 1 )
               {  outputChar( text[line].charAt( ch ) );
               }
               outputChar( '\n' );
            }
            for( line = 0; line < 10; line += 1 )
            {  outputChar( '\n' );
            }
            display.setText( "_" );
         }
         catch( InterruptedException ioe )
         {}
      }
   }

   private void outputChar( char ch ) throws InterruptedException
   {  display.insert( "" + ch, display.getText().length()-1 );
      t.sleep( delay );
   }
}