Howdy.

Exam next week on Wed. 

The first exam testing if you can read Java: 

http://www.cs.indiana.edu/classes/c212/spr2015/02232015/abcdef.pdf

The second exam is testing if you can write simple programs:

http://www.cs.indiana.edu/classes/c211/lab5.html

http://www.cs.indiana.edu/classes/c211/lab6.html

http://www.cs.indiana.edu/classes/c211/lab4.html

Wed we will test the seating for the exam:

(a) come the envelope will be on your desk

(b) get exam in envelope

(c) get lab assignment for this week

Lab notes will have a worked out example. 

The class extension mechanism: allows one to model in stages. 

It generates:

(a) polymorphism

(b) inheritance

(c) dynamic method lookup (overriding) 

(d) shadowing of variables 

(e) constructor chaining 

I'd like to give you motivation today by developing large(r) program.

I'd like to show the main raw ingredients that we can use to write our own big bang. 

Questions to answer at the end:

(a) what was clearest point today? 

(b) what was muddiest point today? 

Let's get started.

import javax.swing.JFrame; 

class One {
  public static void main(String[] args) {
    JFrame a; 
    a = new JFrame(); 
    // System.out.println( a );
    a.setVisible(true); 
    a.setSize(400, 400);
    // a.setDefaultCloseOperation( 3 ); // JFrame.EXIT_ON_CLOSE
    
  }
}

Now let's write a game:

import javax.swing.JFrame; 

class TV extends JFrame {
  TV() {
    this.setVisible(true); 
    this.setSize(400, 400); 
    // this.setDefaultCloseOperation(EXIT_ON_CLOSE); 
  }
  public static void main(String[] args) {
    JFrame a = new TV();  
  }
}

Now we can: 

(a) start drawing

(b) add time 

(c) mouse interactivity 

(d) keyboard interactivity 

I propose we investigate drawing first. 

What are we going to draw?

Let's look at Circles: 

import javax.swing.JFrame; 

class TV extends JFrame {
  TV() {
    this.add( new Screen() ); 
    this.setVisible(true); 
    this.setSize(400, 400); 
    // this.setDefaultCloseOperation(EXIT_ON_CLOSE); 
  }
  public static void main(String[] args) {
    JFrame a = new TV();  
  }
}

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

class Screen extends JComponent {
  ArrayList<Circle> a = new ArrayList<Circle>();
  Screen() {
    int numberOfCircles = 12;
    for (int i = 0; i< numberOfCircles; i++) {
      this.a.add(new Circle( (int) (Math.random() * 400) , 
                              (int) (Math.random() * 400) , 
                              (int) (Math.random() * (80 - 20) + 20), 
                              new Color ( (float) Math.random() , 
                                          (float) Math.random() , 
                                          (float) Math.random()  ) ));  
    }
  }
  public void paintComponent(Graphics g) {
    for (Circle c : this.a) {
      c.draw(g); 
    }
  }
}

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

class Circle {
  int x, y; // center
  int radius;
  Color color; 
  Circle(int x, int y, int radius, Color color){
    this.x = x;
    this.y = y;
    this.radius = radius; 
    this.color = color;
  }
  void draw(Graphics g) {
    g.setColor(this.color); 
    g.fillOval( this.x - this.radius, this.y - this.radius, this.radius * 2, this.radius * 2 ); 
    g.setColor(Color.BLACK); 
    g.drawOval( this.x - this.radius, this.y - this.radius, this.radius * 2, this.radius * 2 ); 
  }
}

Compile run and look at them. 

I want to move them with the mouse. 

I want to be able to use the mouse. 

import javax.swing.JFrame; 

class TV extends JFrame {
  TV() {
    this.add( new Screen() ); 
    this.setVisible(true); 
    this.setSize(400, 400); 
    // this.setDefaultCloseOperation(EXIT_ON_CLOSE); 
  }
  public static void main(String[] args) {
    JFrame a = new TV();  
  }
}

This didn't change. 

import javax.swing.JComponent; 
import java.util.ArrayList; 
import java.awt.Graphics; 
import java.awt.Color; 
import java.awt.event.MouseMotionListener; 
import java.awt.event.MouseEvent; 

class Screen extends JComponent implements MouseMotionListener {
  public void mouseMoved(MouseEvent e) { 
    System.out.println(" Can you see me now? "); 
  }
  public void mouseDragged(MouseEvent e) { 
    int x = e.getX(),y = e.getY();
    System.out.println( "Mouse dragged at (" + x + ", " + y + ")" ); 
  }
  ArrayList<Circle> a = new ArrayList<Circle>();
  Screen() {
    this.addMouseMotionListener( this ); 
    int numberOfCircles = 12;
    for (int i = 0; i< numberOfCircles; i++) {
      this.a.add(new Circle( (int) (Math.random() * 400) , 
                              (int) (Math.random() * 400) , 
                              (int) (Math.random() * (80 - 20) + 20), 
                              new Color ( (float) Math.random() , 
                                          (float) Math.random() , 
                                          (float) Math.random()  ) ));  
    }
  }
  public void paintComponent(Graphics g) {
    for (Circle c : this.a) {
      c.draw(g); 
    }
  }
}

This changed a little, because of the mouse listener code. 

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

class Circle {
  int x, y; // center
  int radius;
  Color color; 
  Circle(int x, int y, int radius, Color color){
    this.x = x;
    this.y = y;
    this.radius = radius; 
    this.color = color;
  }
  void draw(Graphics g) {
    g.setColor(this.color); 
    g.fillOval( this.x - this.radius, this.y - this.radius, this.radius * 2, this.radius * 2 ); 
    g.setColor(Color.BLACK); 
    g.drawOval( this.x - this.radius, this.y - this.radius, this.radius * 2, this.radius * 2 ); 
  }
}

This didn't change. 

Now I need more listener code: MouseListener and ActionListener. 

I need a Timer and the Timer will produce ActionEvent's.

For the rest of the code please go to lab. 

--