(continued from lecture) 

As we start the lab think how you will draw your Tetris pieces. 

So what have we done so far? 

  (a) We wrote One (a JFrame)

  (b) We defined Screen (a JComponent) 

  (c) We defined Point (certainly useful, has own toString() method). 

Now what? 

We played the code from Chapter 13 of Head-First Java. 

import java.awt.Graphics; 

abstract class Shape {
  Point center;
  Shape(Point center) {
    this.center = center; 
  }
  abstract double area();  
  abstract void draw(Graphics g); 
}

After we discuss Shape and abstract classes in general we design Circle: 

import java.awt.Color; 
import java.awt.Graphics; 

class Circle extends Shape {
  int radius;
  Color color; 
  Circle(int radius, Color color, Point center) {
    super( center ); 
    this.radius = radius;
    this.color = color; 
  }
  double area() { return Math.PI * this.radius * this.radius; } 
  void draw(Graphics g) {
    g.setColor(this.color); 
    g.fillOval(this.center.x - this.radius, 
               this.center.y - this.radius, 
               2 * this.radius, 
               2 * this.radius); 
    g.setColor(Color.BLACK); 
    g.drawOval(this.center.x - this.radius, 
               this.center.y - this.radius, 
               2 * this.radius, 
               2 * this.radius);  
  }
}

Now we modify One a bit to let the user ask for a number of circles: 

import javax.swing.JFrame; 

class One extends JFrame {
  One(int number) {
    this.getContentPane().add( new Screen( number ) ); 
    this.setVisible( true ); 
    this.setSize( 400, 500 ); 
  }
  public static void main(String[] args) {
    JFrame a = new One( Integer.parseInt(args[0]) ); // polymorphism 
  }
}

Circles are being created here: 

import javax.swing.JComponent; 
import java.awt.Graphics; 
import java.awt.Color; 

class Screen extends JComponent {

  Shape[] shapes; // at this point null 

  Screen( int number ) {
    this.shapes = new Shape[number]; // allocate 
    for (int i = 0; i < number; i = i + 1) {
      this.shapes[i] = new Circle( (int) (100 + 30 * Math.random()) ,
                                   new Color( (float) Math.random(), 
                                              (float) Math.random(), 
                                              (float) Math.random() ), 
                                   new Point( (int) (50 + 300 * Math.random()) , 
                                              (int) (50 + 400 * Math.random())
                                            ) 
                                 ); 
    }
  }
  // int count = 0;
  public void paintComponent(Graphics g) {
    // this.count = this.count + 1; 
    // System.out.println("I am painting... " + this.count); 
    // g.setColor(Color.YELLOW); 
    // g.fillOval(50, 50, 100, 100); 
    // g.setColor(Color.BLACK); 
    // g.drawOval(50, 50, 100, 100); 
    for (int i = 0; i < this.shapes.length; i = i + 1) {
      (this.shapes[i]).draw(g); 
    }
  }
}

Here's how we define Rectangle:

import java.awt.Color;
import java.awt.Graphics;

class Rectangle extends Shape {
  int width, height;
  Color color;
  Rectangle(int width, int height, Color color, Point center) {
    super( center );
    this.width = width;
    this.height = height;
    this.color = color;
  }
  double area() {
    return this.width * this.height;
  }
  void draw(Graphics g) {
    g.setColor(this.color);
    g.fillRect(this.center.x - this.width / 2 ,
               this.center.y - this.height / 2,
               this.width,
               this.height);
    g.setColor(Color.BLACK);
    g.drawRect(this.center.x - this.width / 2 ,
               this.center.y - this.height / 2,
               this.width,
               this.height);
  }
}

Here's how we would create Rectangles:

import javax.swing.JComponent;
import java.awt.Graphics;
import java.awt.Color;

class Screen extends JComponent {
  Shape[] shapes; // at this point null
  Screen( int number ) {
    this.shapes = new Shape[number]; // allocate
    for (int i = 0; i < number; i = i + 1) {
      this.shapes[i] = new Rectangle ( (int) (50 + 50 * Math.random()),
                                       (int) (50 + 50 * Math.random()),
                                        new Color( (float) Math.random(),
                                                   (float) Math.random(),
                                                   (float) Math.random() ),
                                        new Point( (int) (50 + 300 * Math.random()) ,
                                                   (int) (50 + 400 * Math.random())));
                       // new Circle( (int) (100 + 30 * Math.random()) ,
                       //             new Color( (float) Math.random(),
                       //                        (float) Math.random(),
                       //                        (float) Math.random() ),
                       //             new Point( (int) (50 + 300 * Math.random()) ,
                       //                        (int) (50 + 400 * Math.random())
                       //                      )
                       //           );
    }
  }
  // int count = 0;
  public void paintComponent(Graphics g) {
    // this.count = this.count + 1;
    // System.out.println("I am painting... " + this.count);
    // g.setColor(Color.YELLOW);
    // g.fillOval(50, 50, 100, 100);
    // g.setColor(Color.BLACK);
    // g.drawOval(50, 50, 100, 100);
    for (int i = 0; i < this.shapes.length; i = i + 1) {
      (this.shapes[i]).draw(g);
    }
  }
}

So please submit a program that shows a number of random circles and rectangles. 

Show about the same amount of rectangles and circles. 

--