LAB 8  (Comp 170)

Exploring for loops in creating applets

Begin by reviewing Lab 3 which explored the arrangement of ovals and circles in a window.  In today's lab, we will create more interesting geometric arrangements of ovals by using for loops.  Once again, for convenience, we will use the Applet Viewer feature of Visual Cafe instead of a web browser.

//A basic extension of the java.applet.Applet class

import java.awt.*;
import java.applet.*;

import symantec.itools.awt.shape.Circle;
public class Applet1 extends Applet
{
    int x = 36;
    int y = 36;
    int d;
    int n = 1;
 public void init()
 {
  // Remove this line if you don't use symantec.itools.net.RelativeURL or symantec.itools.awt.util.StatusScroller
  symantec.itools.lang.Context.setApplet(this);

  // This code is automatically generated by Visual Cafe when you add
  // components to the visual environment. It instantiates and initializes
  // the components. To modify the code, only use code syntax that matches
  // what Visual Cafe can generate, or Visual Cafe may be unable to back
  // parse your Java file into its visual environment.
  //{{INIT_CONTROLS
  setLayout(null);
  setSize(426,266);
  button1 = new java.awt.Button();
  button1.setLabel("ACTION");
  button1.setBounds(36,168,60,40);
  button1.setBackground(new Color(16776960));
  add(button1);
  SymAction lSymAction = new SymAction();
  button1.addActionListener(lSymAction);
  }
 
    java.awt.Button button1;
 
 class SymAction implements java.awt.event.ActionListener
 {
  public void actionPerformed(java.awt.event.ActionEvent event)
  {
   Object object = event.getSource();
   if (object == button1)
    button1_ActionPerformed(event);
  }
 }

 void button1_ActionPerformed(java.awt.event.ActionEvent event)
 {
     n++;
  repaint();
 }

 public void paint(Graphics g){
     d = 150;
     for (int i = 0; i < n; i++){
         if (i%2==0)  // if i is even
             g.setColor(Color.black);
         else    // if i is odd
             g.setColor(Color.red);
        g.fillOval(x, y, d, d);
         g.fillOval(75+x, y, d, d);
         g.fillOval(150+x, y, d, d);
         d = 80*d/100;
     } // end of for loop
 }
}
 

 We wish to explore the meaning of the paint method which includes a for loop.

(a) Compile and run this applet.  Note that the number of times that you click the ACTION button corresponds to the depth of the for loop.

(b) Recall from Lab 3 the meaning of the four parameters in the fillOval method:  The first two determine the x and y coordinates of the upper-left vertex of the rectangle in which the oval is inscribed.  The third and fourth parameters determine the respective lengths of the horizontal and vertical axes of the oval.  Try changing the values of x and y (in their initial declaration) and see what happens. Next, try changing the value of d from 150 to smaller values (say 25) or to larger values (say 250).

(c) Next replace the line d = 80*d/100; with d = 99*d/100; or d = 70*d/100;  and see what happens.  Continue to change the value of d and see what happens.

(d) Next, instead of setColor to black or red depending upon whether i is odd or even, explore what happens if one of three colors is assigned according to whether i%3 is 0, 1, or 2.  (Try magenta, yellow, or white, for example.)

(e)  Use drawOval instead of fillOval and see what happens.

(f)  Using basic algebra, modify the program to draw a sequence of n concentric circles of diminishing radii.  Explore color combinations.

(g)  Using nested for loops try to "wallpaper" the window with non-overlapping circles.  That is, each row will consist of n circles and each column will consist of n circles as well.
 
 

 Course Home Page              Department Home Page            Loyola Home Page