Lecture notes for July 21 @11:28am SB221 6W2 2016

This weekend (starting today) I need to be in Indy mornings and evenings

Still, I will hope to be able to offer office hours FrSaSu 12:00noon-2:00pm 

I will let you know tonight by e-mail. Lab today: see below. 

Lecture attendance: 

 Christian Levi      Seth     Jerry   Jiebo  Jared   Vanessa
 Joe B     Arthi     Max      Keqin   Kayl W Arif    He        Mike Siddartha
 Andrew T  Sunghyun  Alex Q   Zhiwei  Xing   Jiahao  Rerajitha 
 Kevin S   Brandon P Andrew R Haoyang Xinyu  Chenrui Matt      Rajeev

Lab attendance will be taken. 

import java.awt.Graphics; 

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

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

http://www.petefreitag.com/cheatsheets/ascii-codes/

https://docs.oracle.com/javase/8/docs/api/java/awt/event/KeyEvent.html#VK_KP_DOWN

http://docs.oracle.com/javase/tutorial/uiswing/events/index.html

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

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 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 {
  public void update() {
    this.count += 1; 
  }
  public boolean hasEnded() {
    return false;  
  }
  public void toetsIngedrukt(String wat) {
    if (wat.equals("left")) {
      this.x -= 5; 

    } else if (wat.equals("right")) {
      this.x += 5;       
    
    } 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);  
  }  
  public static void main(String[] args) {
    JFrame f  = new JFrame(); 
    f.setSize(200, 500); 
    f.setVisible(true); 
    BigBang b = new BigBang(new Tetris()); 
    f.addKeyListener(b);
    Container c = f.getContentPane();
    c.add( b );   
    b.start(); 
    
  }
}

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

Now the plan for the lab: 

  (a) introduce a new instance variable in Tetris: 

      Tetromino current; 

  (b) introduce a new instance variable in Tetris 

      ArrayList<Tetromino> ground; 

  (c) define a class Tetromino that shows as Rectangles/Circles

      It needs to draw itself and have overlaps abilities. 

  (d) make sure draw in Tetris looks like this: 

      public void draw(Graphics g) {
        // g.drawString("Count is: " + this.count, this.x, this.y);  
        this.current.draw(g); 
        this.ground.draw(g); 
      } 

  (e) inside update in Tetris we should do this: 

      public void update() {
        // this.count += 1; 
        this.currrent.moveDown(5); 
      }  

  (f) modify the code above so when it hits the ground

      1. the current Retromino is added to the ground

      2. new random current Tetromino starts falling

--

If you get that far you are in good shape. See you in lab!

--