Introduction to Java. A tutorial from A-SQUARE, Inc. January 2003

© 2000-2003 A-Square, Inc. Cambridge, MA

Source Code for Eggs.java

This code is common for Eggs for Exercises 4a and 4b. The code is also the base for the Planes universe in exercises 4c - 4e.

As always you get the sourcecode by selecting and copying any text between the two > that separate the source text from the rest of this HTML document.

Note the three main sections of a Rart universe:

Section 1 Identifying the universe

Section 2 What the universe does

Section 3 uParameters as used in the universe

>

/*
 * @Eggs4a and Eggs4b   3.0  030115
 *
 * © A-Square, Inc.  
 * 175 Richdale Ave, Cambridge MA 02140
 */
 import rrlet.*;
 import rartbase.*;
 import java.awt.*;
 import java.lang.*;


/**
 *    'Eggs Waxing and Waning
 *
 * This Program is one in the Rart®, or Random Art, series of programs
 * that generate ever changing pictures. Simple geometries and random
 * dynamics create visual effects that may be pleasing to the eye.
 * It uses the Oval class for objects, the eggs, that fill the screen.
 *
 * @version 3.0  030115
 *
 * @author Jan Aminoff
 */
public final class Eggs4a extends Universe { 
  
   // Section 1     Identifying the Universe
   // ------------------------------------------------------------------------
   public String getName() // Name,
       {return("Eggs, in Exercise 4a");}   // no more than 20 letters.
   public String getDescription() 
       {return(sd);}       // Description, 
                // no more than 350 letters describing the universe. Will 
               // be displayed in up to five lines of about 70 characters.
                                       
   private String sd=" See the EGGS Universe where ovals wax and wane and \n"+
             "where overlapping ovals cancel each other thus creating \n"+
             "unexpected new shapes.";
    
    public String getRartist()       // Rartist,
       {return("Jan Aminoff 1989");} // no more than 24 characters.


    // Section 2    What the universe does
   // ------------------------------------------------------------------------
    
    // About colors - constants and defaults
    static Color colors[]=      // Standard Java Colors
        {Color.black,Color.blue,Color.cyan,Color.darkGray,Color.gray,
      Color.green,Color.lightGray,Color.magenta,Color.orange,Color.pink,
      Color.red,Color.white,Color.yellow };
   // The default colors are the swedish colors.    Heja Sverige! 
   private int bc=12;   // default number of color for background (yellow )
   private int ec=1;   // default number of color for foreground (blue )
   private Color background = colors[bc];    
   private boolean resetscreen=false; // Signal to recolor background      
    // Other variables
   private int nnmax = 30;  // maximum number of eggs, set in reset
   private int nn=5;        // actual number of eggs, set as number.getCur()
   private Oval eggarray[]=new Oval[nnmax];  // Up to 30 eggs.
   
   private Debug dB;  // used when debugging, especially as dB.dbg(n, string) 
 
    // The method init() is prescribed by Universe 
   public void init(){
      dB= new Debug();
      dB.setLevel(0);      //  Set level to 1 or 2 for tracing outputs.
      setParams();         //  See Section 3.
      reset();             //  See below.      
   }
      
   private void reset(){
       /** Reset is used in init and also in manageChange when restarting
        *  the universe after change of some parameters.
        */
       // Initialize eggarray
       nn=number.getCur();  // Have to use possibly modified parameters. 
       int vv= size.getCur();
       ec=ecolor.getCur();  // Index to colors array.
       dB.dbg( 1 ,"In Eggs, xm: "+xm+", ym: "+ym);
       for (int i=0; i < nn; i++) {                 
         eggarray[i] = newEgg();
      }
      m=RND(nn);   //start at egg m
      e1 = eggarray[m];
      // Set background
      bc=bcolor.getCur();
      background = colors[bc];
      // Reset screen
      resetscreen = true;  // Actual repaint takes place in cycle 
    }
      
    private Oval newEgg(){
        int vv = size.getCur();
        Color eggcolor = colors[ec];
        return new Oval(xm, ym, vv, eggcolor );
   }
   
   // This method is prescribed by Universe  
   public Color getBackground(){return background;}       

   // The following three variables are important in cycle.
   private int m;      // Index to current waxing or waning egg.
   private int status; // See Oval for the significance of status.
   private Oval e1;    // Current egg.
   
   // This method is prescribed by Universe. Everything interesting happens
   // in the cycle method which is called periodically by the RartRunner
   public void cycle ( Graphics g){ 
   /** In init, nn eggs have been initiated in eggarray and one, e1, has been
    * selected for waxing. It waxes to its maximum size, when another egg is 
    * selected for waxing or waning. When an egg has waned to 0, the eggarray
    * is initiated with a new egg in its place and an egg is chosen randomly.
    * The chosen egg will wax or wane according to its internal state.
    */
      if (resetscreen){  // Reset screen in the beginning and after change
                         // of some parameters.
          g.setPaintMode();  // This is important in the Eggs universe which
                             // uses the XOR mode of painting otherwise.
          g.setColor(background);          
          g.fillRect(0,0,xm,ym);
          resetscreen = false;
      }     
      status = e1.drawChange(g);
      if (status == e1.MAX){   // If egg waxed to max,
          m = RND(nn);      // work on another egg.   
          e1=eggarray[m];
      }else if (status == e1.DONE){ // If egg waned to 0,
          eggarray[m]= newEgg(); // put a new egg in eggarray, 
          m = RND(nn);      // and work on another egg.     
          e1 = eggarray[m];
      }
         
   }  // end of cycle(g)
   
   
   // Section 3   uParameters as used in the universe
   // ------------------------------------------------------------------------   

   // number of eggs  
   private uParameter number; //Number of eggs - PARAMETER
   private int nnmin=2;       // nnmax and nn are given values before init    
   private String ndescr=  
      "This parameter indicates the maximum number of 'Eggs' \n"+
      "visible at any one time. The actual number varies over time. \n"+
      "You may consider reducing the Size/Speed if you have a \n"+
      "large number of Eggs to reduce the overall clutter.";
   // maximum size of an egg
   private uParameter size; // a measure of growth speed or size - PARAMETER  
   private int v=4,vmax=10,vmin=2;  
   private String sdescr=
      "The parameter actually determines the maximum size of \n"+
      "all eggs. It determines the speed of growth in that \n"+ 
      "larger eggs grow faster than smaller ones.";
   // cycletime
   private uParameter ct;  // Cycletime - PARAMETER
   // Detailes of the ct parameter can be seen in Universe
   // egg color
   private uParameter ecolor;  // Egg Color - PARAMETER
   private uParameter bcolor;  // Background Color - PARAMETER
   private String cdescr=
      "Foreground and Background colors may be chosen from \n"+
      "the 13 primary colors offered by Java. ";
   static String colornames[]=
       {" black "," blue "," cyan"," dark grey "," grey ",
       " green "," light grey "," magenta "," orange ",
      " pink "," red "," white "," yellow "};
   
   private void setParams(){
       number= new uParameter 
           (NUMBER,"Number of Eggs",nnmin,nn,nnmax,ndescr);
       //The adduParameter() method is defined in Universe.
       adduParameter(number);
       
       size=new uParameter(PARA4,"Size/Speed",vmin,v, vmax, sdescr);
       adduParameter(size);
       
      ct= new uParameter
          (CYCLETIME,"Cycle Time", 40,100,500, CycleTimeDescr);  
          // Since "Cycle Time" is a compulsory parameter, Universe
          // provides the description CycleTimeDescr.
      adduParameter(ct);
      
      // The color parameters ecolor and bcolor have the same description
      // and array, colornames, for selection.  
      ecolor= new uParameter(PARA5,"Egg Color",ec,colornames, cdescr);
      adduParameter(ecolor);
      bcolor= new uParameter(PARA6,"Background Color",bc,colornames, cdescr); 
      adduParameter(bcolor);
    } //end setParams()
   
   // This method is prescribed by universe. Its purpose is to deal with any 
   // changes in parameters by the user. 
   public void manageChange(uParameter uP){
   /** This method is called by the RartRunner when a user has modified any
    * of the parameters. The new information is conveyed in a uParameter.
    * In the case of Eggs, we have elected to restart the universe for any
    * changes even if for some parameters it would not be strictly neccesary.
    */
      int idx=uP.getIndex();
      dB.dbg( 2,"In Eggs, Change in : "+uP.getName());
      if (uPs[idx]!=null) uPs[idx]=uP; // ensures that uPs, the array of 
                                       // current parameters is up to date         
      switch (idx){
         case VIEWSIZE:
            reset();  // Cange in windowsize, just restart.
            break;
         case NUMBER:   
            number=uP;   // A more sophisticated program could handle
            reset();     // changes in the maximum number of eggs,
            break;       // but this is simpler!
         case PARA4:     // used for speed and size.
            size=uP;
            reset();     // simple solution.
            break;
         case PARA5:     // used for for Egg color
             ecolor=uP;
             reset();    // No alternative to restart here.
             dB.dbg(1,"Eggcolor is now "+colornames[ec]);
             break;
         case PARA6:     // used for Background color
             bcolor=uP;
             reset();    // No alternative to restart here.
             dB.dbg(1,"Background is now "+colornames[bc]);
             break;
         }  
      }  // end of manageChange()
      
   
   // Sections 4, and 5 in Universe have no corresponding entries in the
   // Eggs universe.
      
   // Section 6   Methods related to security. 
   // -----------------------------------------------------------------------
   
   // The following constructor invokes the Universe constructor which is 
   // programmed to throw an exception in order to avoid multiple 
   // instantiations by mistake or bad intent.
   
   public Eggs4a( ) throws InstantiationException  // or Eggs4b
   { }

}  // end of Eggs class

>

TOP