While loop variations - solve simpler problem first



  1. // Illustrates the while loop for September 30th, 2002 810:061 class...
    
    import objectdraw.*;
    import java.awt.*;
    
    public class Loops extends WindowController
    {
    
       public void begin()
       {
          int xCoord, yCoord;
    
          new FilledRect( 0, 0, 300, 300, canvas).setColor( Color.yellow );
    
          xCoord = 10;
          yCoord = 20;
    
          int i = 0;
    
          while (i < 5)
          {
             FilledRect b = new FilledRect(xCoord, yCoord, 40, 10, canvas);
             b.setColor( new Color( 255 - i * 50, 0, 0 ) );
             xCoord += 20;
             yCoord += 10;
             i++;
          }
    
          xCoord = 10;
          yCoord = 100;
          int width = 20;
          int colorValue = 255;
    
          while (yCoord <= 200)
          {
             FilledRect r = new FilledRect(xCoord, yCoord, width, 20, canvas);
             r.setColor( new Color(colorValue, colorValue, colorValue) );
    
             colorValue = colorValue - 50;
             width += 20;
    
             yCoord += 20;
          }        
       }
    }
    
  2. The 3rd and last portion of the begin() method is not shown. That code creates the column of numbers 1 through 8. Write this code and use a while loop and the Text object from objectdraw.jar package to show the first 8 positive integers on the canvas.

    Study Coin Purse for Text objects techniques and syntax, from objectdraw.jar package of Williams College textbook.