Simplest version - 5 shades of RED rectangles



  1. Here is the solution:
    
    // See the URL 
    // http://www.cns.uni.edu/~jacobson/061/applets/ver3Boxball/CAST.html
    
    import java.awt.*;
    import objectdraw.*;
    
    public class CAST0 extends WindowController
    {
       private FilledRect r;
       private Text t;
    
       public void begin()
       {
          r = new FilledRect(50, 50, 400, 400, canvas);
          r.setColor( Color.yellow );
          t = new Text("Great exam practice - while loops" +
                       " - onMouseRelease() event - Colors, FilledRects", 
                       20, 5, canvas);
    
          t.setBold();      
       }
    
       public void onMouseRelease(Location point)
       {
          if ( r.contains(point) )
          {
             // Initialize:  Prepare for the FIRST rectangle...
    
             double x = point.getX();
             double y = point.getY();
    
             int i = 1;
             int red = 255; 
    
             while (i <= 5)
             {
                // Create the current rectangle...
    
                Color theColor = new Color(red, 0, 0);       
                new FilledRect(x, y, 50, 10, canvas).setColor(theColor);
    
                // Prepare for the NEXT rectangle - 2nd, 3rd, 4th, or 5th
                red -= 50;      
    
                x += 5;
                y += 10;
    
                i++;           // when i == 6 the loop is done...
             }
          }
       }
    }
    
  2. Write down and draw a picture of WHAT you see first. Describe WHAT you see. Then later figure out the HOW. You should separate these two concerns. What is one concern. How is the other.

  3. What things have to happen in the while loop initialization?

  4. What things have to happen in the while loop body?

  5. How many times does the while loop body get executed?