Exam 02 on Thursday is in two parts. 

The exam will test all the homework up to now. 

Today we start the project, we will do it together. 

Labs this week will be turned in in person. 

One more homework assignment this week only. 

(require 2htdp/image)
(require 2htdp/universe)

; A Point is a (make-point Number Number)
(define-struct point (x y))

; A Circle is a (make-circl Point Number)
(define-struct circl (center radius))

; A ManyCircl is one of:
;   -- empty

;   -- (cons Circle ManyCircl)

; A World is a ManyCircl 

; (big-bang initial
;           (to-draw ...)
;           (on-tick ...)
;           (on-mouse ...))

(define initial empty)

; render : World -> Image
; (define (render world)
;   (cond ((empty? world) ...)
;         (else (... (first world) ... (render (rest world)) ...))))
(define (render world)
  (cond ((empty? world) (empty-scene 400 400))
        (else (place-image (circle (circl-radius (first world)) "outline" "red")
                           (point-x (circl-center (first world)))
                           (point-y (circl-center (first world)))
                           (render (rest world))))))

(define sample-world (cons (make-circl (make-point 300 300) 30) 
                      (cons (make-circl (make-point 200 100) 80)
                       (cons (make-circl (make-point  50  50) 10) empty))))

; enlarge : circl -> circl
; increases the radius by 1 pixel 
(define (enlarge circl)
  (make-circl (circl-center circl) (add1 (circl-radius circl))))

; update : world -> world

(define (update world)
  (cond ((empty? world) world)
        (else (cons (enlarge (first world))
                    (update (rest world))))))

; meh : world x y event -> world

(define (meh world x y event)
  (cond ((mouse=? "button-down" event) 
         (cons (make-circl (make-point x y) 1)
               world))
        (else world)))

(big-bang sample-world
          (to-draw render)
          (on-tick update)
          (on-mouse meh))

Question: can we do this in Java? 

What does the project look like? 

http://silo.cs.indiana.edu:8346/c212/spr2015/tetris.rkt

Kevin S Andrew T Brandon P Andrew R Mr Hu Rajeev Chenrui Mr Zhao 
Zhiwei David Lee Alex Q Sunghyun L Xing Rerajitha Vanessa Matt L
Mike C He He Arif Jiahao Keqin Max (Yinan) Arthi Joe B Christian 
Kayl Levi Seth Jiebo Jared Alex S Jerry Haoxuan S Siddartha R C

Our plan remains to completely read the entire book. 

To get there we need to first prove we own assignments thus far. 

Let's get started. 

We need a BigBang:


import javax.swing.*; 
import java.awt.event.*; 
import java.awt.*; 

class BigBang extends JComponent implements KeyListener, 
                                            ActionListener, 
                                            MouseListener 
{
  Timer timer; 
  World world; 
  BigBang(int delay, World world) {
    timer = new Timer(delay, this); 
    this.world = world;
  } 
  public void start() {
    timer.start();  
  }
  BigBang(World world) {
    this(1000, world);  
  }
  public void paintComponent(Graphics g) {
    world.draw(g);  
  }
  public void actionPerformed(ActionEvent e) {
    world.update(); 
    if (world.hasEnded())
      timer.stop(); 
    this.repaint(); 
  }
  public void keyPressed(KeyEvent e) { 
    world.keyPressed(e); 
    this.repaint(); 
  } 
  public void keyTyped(KeyEvent e) { } 
  public void keyReleased(KeyEvent e) { } 

  public void mousePressed(MouseEvent e) { 
    world.mousePressed(e); 
    this.repaint(); 
  } 
  public void mouseReleased(MouseEvent e) { } 
  public void mouseClicked(MouseEvent e) { } 
  public void mouseEntered(MouseEvent e) { } 
  public void mouseExited(MouseEvent e) { } 

}


We also need a notion of World: 

import java.awt.*; 
import java.awt.event.*; 

interface World  {
  public void draw(Graphics g);
  public void update(); 
  public boolean hasEnded();
  public void keyPressed(KeyEvent e); 
  public void mousePressed(MouseEvent e); 
}

And here's the World implementation with a main:

// Tetris.java 

import javax.swing.*;
import java.awt.event.*; 
import java.util.*; 
import java.awt.*;

public class Tetris implements World {

  public void update() {
    // System.out.println("Tetris world updated now."); 
  }
  public void draw(Graphics g) {
    // System.out.println("Tetris world being drawn."); 
  }
  public void mousePressed(MouseEvent e) { 
    System.out.println("Why do you press the mouse in Tetris?"); 
  } 
  public void keyPressed(KeyEvent e) { 
    System.out.println( e ); 
  }
  public static void main(String[] args) {
    BigBang game = new BigBang(30, new Tetris());  
    JFrame frame = new JFrame("Tetris"); 
    frame.getContentPane().add( game ); 
    game.addMouseListener( game ); 
    frame.addKeyListener( game ); // why? 
    frame.setVisible(true); 
    frame.setSize(200, 500); 
    // frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); 
    game.start(); 
  }
  public boolean hasEnded() {
    return false; // never!! 
  }
}

We are almost done aren't we? 

--