/* 
   This applet scrolls the message 
       "Hello World (for absolutely the last time)!" 
   across the screen over and over.
*/

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ScrollingHelloWorld extends JApplet 
                          implements ActionListener 
{

   Timer timer;  // The timer that drives the animation.  This
                 // is created and started in the applet's
                 // start() method, and is stopped in the
                 // applet's stop() method.
                                      
   String message = "Hello World (for absolutely the last time)!";  
           // The message to be scrolled across the screen.
  
   int messagePosition = -1;   
          // Current position of the left end of the message, given as the
          //   distance in pixels from the left edge of the applet.
          //   In each frame, this is incremented by one character width
          //   until the message has scrolled completely off the applet.
          //   Then it is reset to zero.  The value of -1 indicates that
          //   the scrolling has not yet begun.
   
   int messageHeight;  // Data about size of message.
   int messageWidth;
   
   int charWidth;  // The width of one character.

   public void init() 
   {
         // Make a JPanel to use as the drawing surface of the
         // applet, and set its colors and font.  The font used
         // is a large "Monospaced" font, meaning all the characters
         // in the font are the same with.  The FontMetric data
         // about the message string is also computed here.
      JPanel display = new JPanel() 
      {
            public void paintComponent(Graphics g) 
            {
                 // Displays the message string, starting
                 // messagePosition pixels from the right edge.
               super.paintComponent(g);
               g.drawString(message, getWidth() - messagePosition, 
                                     getHeight()/2 + messageHeight/2);
            }
      };
      setContentPane(display);
      display.setBackground(Color.white);
      display.setForeground(Color.red);
      Font messageFont = new Font("Monospaced", Font.BOLD, 30);
      display.setFont(messageFont);
      FontMetrics fm = getFontMetrics(messageFont);
      messageWidth = fm.stringWidth(message);
      messageHeight = fm.getAscent();
      charWidth = fm.charWidth('H');
   }
   

   public void start() 
   {
         // Called when the applet is being started or restarted.
         // Create a new timer, or restart the existing timer.
      if (timer == null) 
      {
         timer = new Timer(300, this);
         timer.start();
      }
      else 
      {
         timer.restart();
      }
   }
   

   public void stop() 
   {
         // Called when the applet is about to be stopped.
         // Stop the timer.
      timer.stop();
   }
   
   
   public void actionPerformed(ActionEvent evt) 
   {
          // Compute and display the next frame of the animation.  
          // This is called in response to events from the timer.
          // Move the message position one character to the left.
          // If the message has moved entirely off the applet,
          // put it back in its starting position.
       messagePosition += charWidth;
       if (getSize().width - messagePosition + messageWidth < 0) 
       {
            // message has moved off left edge
          messagePosition = 0;
       }
       repaint();
   }       
            
   
} // end class ScrollingHelloWorld

(Applet "ScrollingHelloWorld" would be displayed here
if Java were available.)

Back to Lesson 32 Examples

Back to Java Main Page