
/* 
   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 javax.swing.*;


public class RandomStringsWithArray extends JApplet {


   static class StringData {
         // An object of this type holds the position, color, and font
         // of one copy of the string.
      int x, y;
      Color color;
      Font font;
   }
    

   class Display extends JPanel {
         // An object belonging to this class is used as the drawing
         // surface of the applet, to display the messages.
      public void paintComponent(Graphics g) {
           // Draw copies of the strings, using the position, color and
           // font data for each copy, which is stored in the array, data.
         super.paintComponent(g);
         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 nested class Display 


   final static int MESSAGE_COUNT = 25;  // 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 up a MouseListener to
         // listen for clicks on the applet and respond by computing and
         // displaying new data.
         
      Display display = new Display(); // Drawing surface, belonging to 
                                       // nested class Display.
                                       
      setContentPane(display);
      display.setBackground(Color.black);
   
      message = getParameter("message");  // Get message to be displayed.
      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();
      
      addMouseListener( new MouseAdapter() {
            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();
            }
         });
      
   } // 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()
   
   
}  // end class RandomStringsWithArray

