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

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

Source for Exercise 3c

All Exercises 3x use Logo and ASquareLogo, which you can find following the links.

Banner3c.java

This code has been mostly purged from the comments that relate to the other 3x exercises.
The source for Banner3a with the comments retained can be found on Banner3-source.html

>
/*
 * @Banner3c    2.0  030105
 *
 * © A-Square, Inc.  
 * 175 Richdale Ave, Cambridge MA 02140 
 */

import java.awt.*;
import java.applet.Applet;
import java.util.Date;
import rartbase.*;

/**
 * A simple Rart universe
 * @version 2.0  000928
 *
 * @author Jan Aminoff
 */

final public class Banner3c extends Universe {
   /*
    * This source file is provided in sections corresponding to sections
    * with the the same name and order of the Universe.java source file.
    */

   // Section 1   Identifying the universe
   // -------------------------------------------------------------------------
   public String getName(){return "Banner3c, a Rart Universe";}
   
   public String getDescription(){ return descr;  } 
   public String descr=  
         "This universe is used to demonstrate the basic Rart framework \n"+
         "in the Introduction to Java Tutorial. It should be compared \n"+ 
         "to the Banner2a applet of the same tutorial. \n" +
     	 "In Banner3c one counter steps the number of frames and another \n"+
         "is used to check the uParameter mechanism. We are also timing \n"+
         " 100 executions with and without overhead. " +
         " ";          // NEVER FORGET THE SEMICOLON!
                       
     
   public String getRartist(){ return "Jan Aminoff 2000";}
   // -------------------------------------------------------------------------       
   
   // Section 2   What the universe does
   // -------------------------------------------------------------------------
   // Variables
	   private int maxv = 8;         // Max speed of logo
	   private int size = 35;        // Size of logo
	   private Logo logoarray[] = new Logo[10];
   
  // The following are used for timing and are for Ex2c only
  		private long t0,t1,t2; // Timers for measuring cycle lengths
   		private int ndt;     // 100 cycles, absolute time
   		private int sdt;     // accumulates lengths of 100 cycles, with overhead
   		private int ssdt;    // accumulates lengths of 100 cycles, no overhead
   		private int ns;      // number of cycles modulo 100.
   
   // init(), implementation of abstract method in Universe.
   public void init(){
      setParams();  //See Section 3 
      // We collect all reactions to changes in a reset method
      reset();      
   }

   private void reset(){
      // Note: The two variables xm and ym are available in the parent class
      // Universe. They give the size of the available window at all times.
     
   // Following five lines are specific to Ex3b and Ex3c , where we have an 
   // array. The max number of Logos are given by number.getMax()
   		for (int i=0; i< number.getMax(); i++){       
   			int s =  10+RND(size);                     
   			logoarray[i] = new ASquareLogo(xm, ym, s); 
   			logoarray[i].setMaxSpeed(maxv);            
   		}                 
   // The following is for Ex3c only
   		t1 = System.currentTimeMillis();  // t1 gives current time
    }  // end of reset
   
   // cycle(g), implementation of abstract method in Universe.
   public void cycle(Graphics g){
   // The following lines are for Ex3c only
   // The first part deals with the measuring of cycle time.
   		t0=System.currentTimeMillis(); // t0 gives time at beginning of cycle.
    	if (ns == 0) {                  // Is this the first of 100 cycles?
    		
    		t2=System.currentTimeMillis();
    		ndt=(int)(t2-t1);             // ndt absolute time
    		t1=t2;
    		ssdt=sdt;
    		sdt=0;
    	}     // end of Ex3c portion
      // Compare with the update method of Banner2a!
      // We reset the whole rectangle (0,0, maxx, maxy) 
        g.setColor(Color.lightGray);   
        g.fillRect(0, 0, xm, ym);    // Paint the screen light gray    
      // Following three lines are specific to Ex3b and Ex3c
   		for (int i=0; i< number.getCur(); i++){
   			logoarray[i].drawLogo(g);
   			logoarray[i].moveLogo();
   		}                                 
   
     
    	// The following line is for Ex3c only
     	String s1 = "RART®";
        printString(g, s1, 50, 45, 20);     
        // The following line is for Ex3c only
      	s1 =  "Counter of Frames mod 100: "+ns;
      	printString(g, s1, 120, 80 ,14);
     	// The following lines are for Ex3c only
      	printString(g,"Width by height: "+xm+" x "+ym,120,100,14);
      	printString(g,"Time of 100 cycles(with overhead): "+ndt+" ms",120,120,14);
      	printString(g,"Time of 100 cycles(no overhead): "+ssdt+" ms",120,140,14);
      	int nn=number.getCur();
     	// The following line is for Ex3c only
      	s1 = "The present value of the parameter NUMBER: "+ nn;
      	printString(g, s1,xm-250,ym-5,10);
	  	s1 = "Java Environment: "+
		    System.getProperty("java.vendor")+"..."+
		    System.getProperty("java.version");
		printString(g, s1, 5, 15 ,12);
   
      // This has to do with timing at the end of the cycle.
      // nc, from Universe, is actual number of cycles. ns is nc modulo 100
      // The following lines are for Ex3c only
       	ns = nc % 100;
       	sdt += (int)(System.currentTimeMillis()-t0);
  }   // end of cycle(g)
   
   // manageChange(uP), implementation of abstract method in Universe
   public void manageChange(uParameter uP){
   /*
    * The Banner3c universe has three parameters, with
    * index VIEWSIZE, CYCLETIME, and NUMBER respectively
    * Section 3 gives the definitions. Here we have to program what to
    * do if any of these parameters is modified.
    */
    int type = uP.getIndex();
    if (type == VIEWSIZE){      // If the size of the screen has changed.
        reset();                // Just start all over
    }else if (type == CYCLETIME){   // If the cycletime is modified
        cycletime = uP;         // Make sure cycletime has right value        
    }else if (type == NUMBER){
        number = uP;            // number has right value
        reset();                // Just start all over.
    }
   }    // end of manageChange(uP)
   
   // getBackground(), overriding method in Universe
   public Color getBackground(){return background;}
   private Color background = Color.lightGray;
   // -------------------------------------------------------------------------

   // Section 3   uParameters as used in the universe
   // -------------------------------------------------------------------------
   
   // There are two uParameters defined here: cycletime defined almost always,
   // and number which is specific to this universe.
   private uParameter cycletime;
   private uParameter number;
   
   // The setParams() method is called only once, in init(), it is defined here
   // in Section 3, so all parameter related stuff is together
   private void setParams(){
        // First cycletime
        // Since "Cycle Time" is a compulsory parameter, the description is
        // provided in Universe as CycleTimeDescr.
        cycletime = new uParameter
                (CYCLETIME,"Cycle Time", 10,50,100, CycleTimeDescr);
        // we now have to add the parameter to the array of uParameters
        // associated with this universe
        adduParameter(cycletime);
        
        // Second number
        String ndescr = "In EX2b, EX2c, and EX2d double click \n"+
				"to see this parameter change!"; 
        number= new uParameter
                (NUMBER, "Number of objects",0,5,10, ndescr);
         adduParameter(number); 
   }  // end of setParams()
   // ------------------------------------------------------------------------- 

   // Section 4   Utility Methods
   // -------------------------------------------------------------------------   
   // All provided in Universe, include  int RND(int x), printString, etc
   
   // Section 5 Useful Variables
   // -------------------------------------------------------------------------
   // Provided in Universe, include xm, ym, for width and height of window and
   // nc for number of frames.
   
   // Section 6 Security related methods
   // Provided in Universe, except for the following constructor.
   
   public Banner3c()throws InstantiationException
   {
      // Empty statement. This will activate the constructor of Universe
      // which only allows the activation of one universe at a time in
      // order to preclude bogging down the system by mistake or intent.
   }
   
}  //end of definition of Banner3c
>