http://www.cs.indiana.edu/classes/c212-dgerman/fall2015/labEleven.html


import java.awt.Graphics; 

interface World {
  public void update(); 
  public boolean hasEnded(); 
  public void draw(Graphics g); 
  public void toetsIngedrukt(String wat); 
}

--------------------------------------------------(            )------------

import javax.swing.JComponent; 
import javax.swing.Timer;
import java.awt.event.ActionListener; 
import java.awt.event.KeyListener; 
import java.awt.event.ActionEvent; 
import java.awt.event.KeyEvent; 
import java.awt.Graphics;

public class BigBang extends JComponent 
                     implements ActionListener, 
                                KeyListener 
{ World world; 
  Timer timer;
  public BigBang(World world) {
    this.world = world;  
    this.timer = new Timer(1000, this); 
  }
  public BigBang(int delay, World world) {
    this.world = world;  
    this.timer = new Timer(delay, this); 
  }
  public void start() {
    this.timer.start();  
  }
  public void actionPerformed(ActionEvent e) {
    this.world.update();  
    this.repaint(); 
  }
  public void keyPressed(KeyEvent e) { 
    int wat = e.getKeyCode() ; 
    switch (wat) {
      case 37:
        this.world.toetsIngedrukt("left"); 
        break;
      case 39: 
        this.world.toetsIngedrukt("right"); 
        break;
      case 38: 
        this.world.toetsIngedrukt("up"); 
        break;
      case 40: 
        this.world.toetsIngedrukt("drop"); 
        break;
      case 65 + (114 - 97): // KeyEvent.VK_R:       
        this.world.toetsIngedrukt("rotate"); 
        break;
      default:         
        this.world.toetsIngedrukt("whatever"); 
        break;
    }
    this.repaint(); 
  } 
  public void keyReleased(KeyEvent e) { } 
  public void keyTyped(KeyEvent e) { } 
  public void paintComponent(Graphics g) {
    this.world.draw(g);  
  }
}

--------------------------------------------------(            )------------

import java.awt.Graphics; 
import java.awt.Container;
import javax.swing.JFrame;

public class Tetris implements World {
  Tetromino current; 
  Ground ground; 
  Tetris() {
    super(); 
    this.current = new Tetromino(100, 0); 
    this.ground = new Ground(); 
  }
  public void update() {
    // this.count += 1; 
    if (this.current.overlaps(ground)) {
      this.ground.add(this.current);
      this.current = new Tetromino(100, 0);
    } else 
      this.current.fall(); 
  }
  public boolean hasEnded() {
    return false;  
  }
  public void toetsIngedrukt(String wat) {
    if (wat.equals("left")) {
      // this.x -= 5; 
      this.current.moveLeft(); 
    } else if (wat.equals("right")) {
      this.x += 5;       
       this.current.moveRight(); 
   } else if (wat.equals("drop")) {
      this.y += 5; 
    
    } else if (wat.equals("rotate")) {

    } else if (wat.equals("up")) {
      this.y -= 5;       

    } else {

    }
    System.out.println( wat ); 
  } 
  int x = 50, y = 50; 
  int count;
  public void draw(Graphics g) {
    // g.drawString("Count is: " + this.count, this.x, this.y);  
    this.current.draw(g); 
    this.ground.draw(g); 
  }  
  public static void main(String[] args) {
    JFrame f  = new JFrame(); 
    f.setSize(200, 500); 
    f.setVisible(true); 
    BigBang b = new BigBang(300, new Tetris()); 
    f.addKeyListener(b);
    Container c = f.getContentPane();
    c.add( b );   
    b.start(); 
    
  }
}

--------------------------------------------------(            )------------

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

public class Tetromino {
  public boolean overlaps(Ground g) {
    for (Tetromino t : g) 
      if (t.overlaps(this))
        return true; 
    return false; 
  }
  public boolean overlaps(Tetromino t) {
    return false;  
  }
  private int x, y; 
  private int width, height; 
  private Color color; 
  public Tetromino(int x, int y) {
    this.x = x;
    this.y = y; 
    this.width = (int) (Math.random() * 30 + 20); 
    this.height = (int) (Math.random() * 30 + 20); 
    this.color = new Color((float) (Math.random() / 2 + 0.5), 
                           (float) (Math.random() / 2 + 0.5), 
                           (float) (Math.random() / 2 + 0.5)
                          ); 
  }
  public void fall() {
    this.y += 10;  
  }
  public void moveLeft() {
    this.x -= 5;  
  }
  public void moveRight() {
    this.x += 5;  
  }
  public void draw(Graphics g) {
    // g.drawString("I'm a Tetromino", this.x, this.y);  
    g.setColor(this.color); 
    g.fillRect(this.x, this.y, this.width, this.height);    
    g.setColor(Color.BLACK); 
    g.drawRect(this.x, this.y, this.width, this.height);    
  }
}

--------------------------------------------------(            )------------

import java.awt.Graphics; 
import java.util.ArrayList; 

public class Ground extends ArrayList<Tetromino> {
  public void draw(Graphics g) {
    for (Tetromino t : this)
      t.draw(g); 
  }
}

--------------------------------------------------(            )------------


As I am taking attendance please modify this to include:

  (a) actual overlaps method between tetrominos 

  http://www.cs.indiana.edu/classes/c212/fall2011/1013.jpg

  (b) make the program work with that

  Be careful how the first tetromino hits/initializes the ground!

--------------------

Christian Kayl Levi Seth Jiebo Danny Jared Alex S Rajeev Mike C He He Arif 
Keqin Max Yinan Arthi Joe B Andrew David Lee Alex Q Sunghyun Rerajitha Jiahao 
Siddartha Zhiwei Kevin S Jerry Shen Brandon Andrew Ray Haoyang Xinyu Xing W 
Chenrui Matt Lonis 

See you in lab.