CSCI A202 - Introduction to Programming (II)

Lecture 7: Simple Graphics and a Geometric Class Circle

Here's the code we developed in class on Tuesday.

yimport java.awt.*;
import BreezyGUI.*;

public class Step1 extends GBFrame {

  Color[] colors = { Color.blue, 
                     Color.red, 
                     Color.yellow, 
                     Color.yellow, 
                     Color.blue, 
                     Color.blue}; 

  int[] xCoords = { 230,  40, 120, 120, 210, 320}; 

  int[] yCoords = {  30, 120, 120,  60, 210,  50}; 

  int[] radii   = {  10,  20,  15,  20,  30,  25}; 
  

  public void paint(Graphics g) {
    for (int i = 0; i < radii.length; i++) {
      g.setColor(colors[i]); 
      g.fillOval( xCoords[i], 
                  yCoords[i], 
                  2 * radii[i],
                  2 * radii[i]); 
    } 
  } 

  public static void main(String[] args) {
    Frame f = new Step1(); 
    f.setSize(500, 300); 
    f.setVisible(true); 
  }
}
and the object oriented version of it, that includes the definition of class Circle
import java.awt.*;
import BreezyGUI.*;

public class Step2 extends GBFrame {

  Color[] colors = { Color.blue, 
                     Color.red, 
                     Color.yellow, 
                     Color.yellow, 
                     Color.blue, 
                     Color.blue}; 

  int[] xCoords = { 230,  40, 120, 120, 210, 320}; 

  int[] yCoords = {  30, 120, 120,  60, 210,  50}; 

  int[] radii   = {  10,  20,  15,  20,  30,  25}; 
  
  public void paint(Graphics g) {
    for (int i = 0; i < radii.length; i++) {
      circles[i].draw(g); 
    } 
  } 

  int numberOfCircles = 6; 
  Circle[] circles = new Circle[numberOfCircles]; 

  Step2() {
    for (int i = 0; i < numberOfCircles; i++) {
      circles[i] = new Circle( xCoords[i], 
                               yCoords[i], 
                               radii[i], 
                               colors[i]); 
    } 
  } 

  public static void main(String[] args) {
    Frame f = new Step2(); 
    f.setSize(500, 300); 
    f.setVisible(true); 
  }
}

class Circle {
  int x, y, radius; 
  Color color;
  Circle( int xInitially, int yInitially, 
          int radiusInitially, Color colorInitially) {
    x      = xInitially; 
    y      = yInitially; 
    radius = radiusInitially; 
    color  = colorInitially; 
  } 
  void draw(Graphics g) {
    g.setColor(color); 
    g.fillOval(x, y, 2 * radius, 2 * radius); 
  } 
}
Also, please review Chapter 12 in your text.