/** Copyright (c) 1997 Sams.net Publishing. All Rights Reserved.
 ** Teach Yourself Java 1.1, Laura Lemay and Charles L. Perkins.
 ** Adapted from Listing 9.4:  Random color boxes.
 **
 ** Modified to SimpleApplet5 by John Pais, December 1997.   **/
 
Toggle your browser's Back and Forward buttons to compare the running applet to this code.
Click here to run a fresh copy of the applet:  SimpleApplet5.html

/** USE EXISTING JAVA CLASSES **/

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

/** EXTEND THE JAVA APPLET CLASS **/
 
public class SimpleApplet5 extends Applet
   {int rval, gval, bval;                                 // Define red, green, blue color variables.
 
     public void init()
       {setBackground(Color.white);}          // Change the gray background to white.
 
Toggle your browser's Back and Forward buttons to compare the running applet to this code.
Click here to run a fresh copy of the applet:  SimpleApplet5.html

    /** CREATE THE CURRENT FRAME **/

    public void paint(Graphics g)
       {for (int j = 30; j < (size().height - 25); j += 30)          // Set up vertical spacing.
          {for (int i = 5; i < (size().width - 25); i += 30)          // Set up horizontal spacing.
              {rval = (int) Math.floor(Math.random()*256);      // Generate a random color
                gval = (int) Math.floor(Math.random()*256);     // red, green, blue mix.
                bval = (int) Math.floor(Math.random()*256);     // Note the "casting" here.
 
                g.setColor(new Color(rval,gval,bval));               // Paint a small box with
                g.fillRect(i,j,25,25);                                           // the current random color.
                g.setColor(Color.black);                                   // Paint a black border around
                g.drawRect(i-1,j-1,26,26);                                 // the edge of the small box.
                }
            }
         }
     }