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

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

Source Code for Oval.java

This code is common for Oval for Exercises 4a and 4b You have to modify the commented code for Exercise 4b. See comments at the end of the source.

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.

>

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

public class Oval{
/** --------------------------------------------------------------------------
 * Oval, the oval waxes at an even rate to its ultimate size. It stays there  
 * until it is selected for waning, when it is reduced in size until it 
 * disappears. It is painted using the XOR method of painting which means 
 * that if two ovals overlap they will cancel each other. It is used in the 
 * Eggs universe.
 *
 * @version 3.0 030115
 *
 * @author Jan Aminoff  
 */
   private int x;        // x pos of center of oval
   private int y;        // y pos of center of oval 
   private int xys = 40;   // minimum space between center and edge of window
   private int dx;          // growth velocity in x dir
   private int dy;          // growth velocity in y dir
   private int vmax;    // max size of band
   private int nb;         // number of bands (nb*vmax) max size of Oval
   private int k;          // current band of Oval
   private boolean grow;   //status (vaxing or waning)
   public int status;  
   public static final int INITIALIZED=1;         
   public static final int WAXING=2;
   public static final int MAX=3; 
   public static final int WANING=4;
   public static final int DONE=5; 
   
   private static int RND(int x){
       return(Universe.RND(x));
   }
   private Color ovalColor;
   
   static int primes[] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31,   //11
                41, 43, 43,47, 53, 59, 61, 67, 71, 73,      //10
                  47, 83, 83};                        //3

   // constructor 
   
   public Oval(int xmax, int ymax, int vv, Color c) {
   /** Constructor xmax,ymax is width,height of window/screen. The ovals may 
    * display partly. vv is a number relating to the maximum speed and growth
    * of the oval. The oval is drawn in XOR mode in the color c.
    */
    
    // The size of the oval is selected from the primes array
      nb = primes[3+RND(2*vv)]; // This works quite well, I think.
      // Center of oval is more than xys away from edge
      x = xys+RND(xmax-xys);
      y = xys+RND(ymax-xys);
      status = INITIALIZED;
      ovalColor = c;
      // Note that dx and dy are selected independently. Causes great 
      // variation in the configurations.
      dx = 1 + RND(nb);   // max size of this oval: nb*dx=nb*nb
      dy = 1 + RND(nb);   // max size of this oval: nb*dy=nb*nb      
   }
   
   public int drawChange(Graphics g){
   /** The drawChange method provides the functionality of the Oval class.
    * It provides the changes incrementally by adding bands when waxing
    * and removing bands when waning. It returns its status of which
    * MAX, when it has grown to its maximum size and DONE, when it has 
    * waned to nothing, is of interest to the Eggs universe.
    */
       int newstatus=status;
       switch(status){
           case INITIALIZED:
               newstatus = WAXING;
               k=0;          
           case WAXING:
               drawBand(k,g);
               k++;
               if (k == nb)newstatus =MAX;
               break;
           case MAX:
               newstatus = WANING;
               k--;
           case WANING:
               drawBand(k,g);
               k--;
               if (k < 0) newstatus = DONE;
               break;
       }
       status = newstatus;
       return (status);
   }
           
   private void drawBand(int k, Graphics g){
   // Draws the k-th band of the oval when waxing
   // or erases the k-th band when waning
      drawOval(k,g);   // oval, size k*dx, k*dy 
      drawOval(k+1,g); // oval, size (k+1)*dx, (k+1)*dy                               
   }
   private void drawOval(int k, Graphics g){
   // Used in drawBand only
      int w = k*dx;   //k = 0,1, ... nb-1
      int h = k*dy;
      g.setXORMode(ovalColor);
      g.fillOval(x-w,y-h,2*w,2*h); // oval, size k*dx, k*dy
      // or to make the Eggs rectangular
      // g.fillRect(x-w,y-h,2*w,2*h); // rectangle, size k*dx, k*dy
   }

}  // end class Oval

>