home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / VCAFE.3.0A / Sample.bin / ScrollingText.java < prev    next >
Text File  |  1997-09-06  |  5KB  |  209 lines

  1. /*
  2.  *  ScrollingText.java
  3.  *
  4.  *  This file contains class ScrollingText.
  5.  */
  6.  
  7. package samples.conversion;
  8.  
  9. import java.applet.*;
  10. import java.awt.*;
  11.  
  12. public class ScrollingText extends Canvas implements Runnable
  13. {
  14.     private Thread thread;
  15.     private Image image;
  16.     private Graphics imageGC;
  17.  
  18.     public final static int MAX_RATE = 30;
  19.     public final static int MIN_RATE = 1;
  20.     public final static boolean RANDOM = true;
  21.     public final static boolean CENTER = false;
  22.  
  23.     protected Color backgroundColor = Color.black;
  24.     protected Color textColor = Color.yellow;
  25.     protected int refreshRate = 50;
  26.     protected int width;
  27.     protected int height;
  28.     protected int posX = 0;
  29.     protected int posY = 0;
  30.     protected int scrollRate = 3;
  31.     protected int textWidth;
  32.     protected int textHeight;
  33.     protected boolean random = true;
  34.     protected String text;
  35.     protected Font font;
  36.  
  37.     ScrollingText()
  38.     {
  39.         this( "SYMANTEC.   Visual Cafe. ", 320, 280 );
  40.     }
  41.  
  42.     ScrollingText( String s, int w, int h )
  43.     {
  44.         width = w;
  45.         height = h;
  46.         setBackground( backgroundColor );
  47.         setSize( width, height );
  48.         text = s;
  49.         font = new Font( "TimesRoman", Font.PLAIN, 15 );
  50.         textWidth = Toolkit.getDefaultToolkit().getFontMetrics( font ).stringWidth( s );
  51.         textHeight = Toolkit.getDefaultToolkit().getFontMetrics( font ).getHeight();
  52.         posX = width;
  53.     }
  54.  
  55.     public void addNotify()
  56.     {
  57.         super.addNotify();
  58.         image = createImage( width, height );
  59.         imageGC = image.getGraphics();
  60.     }
  61.  
  62.     public void run()
  63.     {
  64.        /*
  65.         *  This is the thread's main body.
  66.         */
  67.  
  68.         while( true )
  69.         {
  70.             repaint();
  71.             try
  72.             {
  73.                 Thread.sleep( refreshRate );
  74.             }
  75.             catch( Exception e )
  76.             {
  77.                 System.out.println( e.toString() );
  78.                 System.exit( 1 );
  79.             }
  80.             Thread.yield();
  81.         }
  82.     }
  83.  
  84.     public void start()
  85.     {
  86.        /*
  87.         *  This will start the animation.
  88.         */
  89.  
  90.         if( thread == null )
  91.             thread = new Thread( this );
  92.         thread.start();
  93.     }
  94.  
  95.     public void stop()
  96.     {
  97.        /*
  98.         *  This will stop the animation.
  99.         */
  100.  
  101.         if( thread != null )
  102.         {
  103.             thread.stop();
  104.             thread = null;
  105.         }
  106.     }
  107.  
  108.     public void update( Graphics g )
  109.     {
  110.         paint( g );
  111.     }
  112.  
  113.     public void paint( Graphics g )
  114.     {
  115.         /*
  116.          *  if posX is at starting position,
  117.          *  get the next y position either randomly
  118.          *  or set at the center.
  119.          */
  120.  
  121.         if( posX == width )
  122.             if( random )
  123.             {
  124.                  posY = (int)( (double) height * Math.random() );
  125.                  if( posY <= textHeight )
  126.                      posY += textHeight;
  127.             }
  128.             else
  129.                  posY = height / 2;
  130.  
  131.         /*
  132.          *  draw the text.
  133.          */
  134.  
  135.         if( imageGC == null )
  136.         {
  137.            /*
  138.             *  if imageGC == null
  139.             *  just use the default graphics context
  140.             *  although this may flicker.
  141.             */
  142.  
  143.             g.setColor( backgroundColor );
  144.             g.fillRect( 0, 0, width, height );
  145.             g.setColor( textColor );
  146.             g.setFont( font );
  147.             g.drawString( text, posX, posY  );
  148.         }
  149.         else
  150.         {
  151.             imageGC.setColor( backgroundColor );
  152.             imageGC.fillRect( 0, 0, width, height );
  153.             imageGC.setColor( textColor );
  154.             imageGC.setFont( font );
  155.             imageGC.drawString( text, posX, posY  );
  156.         }
  157.  
  158.         /*
  159.          *  set the next x position.
  160.          */
  161.  
  162.         posX -= scrollRate;
  163.         if( posX <= -textWidth )
  164.             posX = width;
  165.  
  166.         g.drawImage( image, 0, 0, this );
  167.     }
  168.  
  169.     public void setBackgroundColor( Color c )
  170.     {
  171.         stop();
  172.         backgroundColor = c;
  173.     }
  174.  
  175.     public void setTextColor( Color c )
  176.     {
  177.         stop();
  178.         textColor = c;
  179.     }
  180.  
  181.     public void setText( String s )
  182.     {
  183.         stop();
  184.         text = s;
  185.     }
  186.  
  187.     public void setRate( int rate )
  188.     {
  189.         if( rate < MIN_RATE )
  190.             scrollRate = MIN_RATE;
  191.         else if( rate > MAX_RATE )
  192.             scrollRate = MAX_RATE;
  193.         else
  194.             scrollRate = rate;
  195.     }
  196.  
  197.     public String getText()
  198.     {
  199.         return text;
  200.     }
  201.  
  202.     public void setPosition( boolean pos )
  203.     {
  204.         random = pos;
  205.         posX = width;
  206.         repaint();
  207.     }
  208. }
  209.