
/* 
   This applet displays 25 copies of a message.  The color and 
   position of each message is selected at random.  The font
   of each message is randomly chosed from among five possible
   fonts.  The messages are displayed on a black background.
   When the user clicks on the applet, new random fonts, colors,
   and positions are chosen.
   
   Note:  The positions, colors, and fonts used for the strings 
   are stored in an array so that the applet can be properly
   redrawn when necessary.
*/


import java.awt.*;
import java.awt.event.*;
import java.applet.*;


class StringData {
      // An object of this type holds the postion, color, and font
      // of one copy of the string.  (Probably, this should be a
      // static nested class inside class RandomStringsWithArray.)
   int x, y;
   Color color;
   Font font;
}


public class RandomStringsWithArray extends Applet implements MouseListener{

   final static int MESSAGE_COUNT = 25;  // The number of copies of the message.

   String message;  // The message to be displayed.  This can be set in
                    // an applet param with name "message".  If no
                    // value is provided in the applet tag, then 
                    // the string "Java!" is used as the default.
   
   Font[] fonts;  // An array containing the five fonts.
                  //   (To be initialized in the constructor.)
                  
   StringData[] data;  // An array containing the font, color, and
                       //   and position of each copy of the message.
                       //   The StringData class is defined later in this file.
   

   public void init() {
         // Initialize the applet.  Get the message from an applet parameter,
         // if it's there.  Create the five fonts.  Create the string data
         // array and fill it with random data.  Set the applet to listen
         // for mouse clicks on itself.
   
      message = getParameter("message");
      if (message == null)
         message = "Java!";
         
      fonts = new Font[5];
      fonts[0] = new Font("Serif", Font.BOLD, 14);
      fonts[1] = new Font("SansSerif", Font.BOLD + Font.ITALIC, 24);
      fonts[2] = new Font("Monospaced", Font.PLAIN, 20);
      fonts[3] = new Font("Dialog", Font.PLAIN, 30);
      fonts[4] = new Font("Serif", Font.ITALIC, 36);
      
      data = new StringData[MESSAGE_COUNT];
      for (int i = 0; i < MESSAGE_COUNT; i++)
         data[i] = new StringData();
      makeStringData();
      
      setBackground(Color.black);
      addMouseListener(this);
      
   } // end init()
   
   
   void makeStringData() {
         // Fill the array, data, with random position, font, and color values.

      int width = getSize().width;       // Get the applet's width and height.
      int height = getSize().height;
   
      for (int i = 0; i < MESSAGE_COUNT; i++) {
            // Set random values in data[i] for the postion, color, and font.

         data[i].x = -50 + (int)(Math.random()*(width+40));
         data[i].y = (int)(Math.random()*(height+20));
         
         data[i].color = Color.getHSBColor( (float)Math.random(), 1.0F, 1.0F );
         
         int fontNum = (int)(Math.random()*fonts.length);  // A random index for
                                                           //  the fonts array.
         data[i].font = fonts[fontNum];

      }
      
   }  // end makeStringData()
   
   
   
   public void paint(Graphics g) {
        // Draw copies of the strings, using the position, color and
        // font data for each copy, which is stored in the array, data.
   
      for (int i = 0; i < MESSAGE_COUNT; i++) {
          g.setColor( data[i].color );
          g.setFont( data[i].font );
          g.drawString( message, data[i].x, data[i].y );
      }
      
   }  // end paint()
   
   
   public void mousePressed(MouseEvent evt) {
          // When user presses the mouse, create a new set of random
          // data for the strings, and repaint the applet.
      makeStringData();
      repaint();
   }
   
   public void mouseEntered(MouseEvent evt) { }    // These empty routines
   public void mouseExited(MouseEvent evt) { }     //    are required by the
   public void mouseClicked(MouseEvent evt) { }    //    MouseListener interface.
   public void mouseReleased(MouseEvent evt) { }
   

}  // end class RandomStringsWithArray

