Howdy. 

Lab 09 prototype posted. 

Homework 07 extended. 

Homework 07 help posted last night. 

I'd like to resume our discussion about event handling. 

I would like to propose a BigBang abstraction. 

My abstraction is composed of a class and an interface:

  the class is BigBang.java 

  the interface is World.java 

You need to create a Game class extension of World in a certain way and run it.

Here's what Game needs to look like in the beginning:

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

public class Game implements World { 

  public Game() {

  }

  // define your methods here 

  public static void main(String[] args) {
    BigBang b = new BigBang(new Game()); 
    b.start( 50, // delay 
            400  // size 
           ); 
  }

}


Here's the interface:

import java.awt.Graphics; 
import java.awt.event.KeyEvent; 
import java.awt.event.MouseEvent; 

interface World {
  void draw(Graphics g);  
  void teh(); 
  void meh(MouseEvent e); 
  void keh(KeyEvent e); 
  boolean hasEnded(); 
  void sayBye(); 
}

This is the actual BigBang: 

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

public class BigBang extends JComponent implements ActionListener, MouseListener, MouseMotionListener, KeyListener {
  Timer timer; 
  World world; 
  public BigBang(World world) {
    this.world = world; 
    this.addMouseListener(this); 
    this.addMouseMotionListener(this); 
    this.addKeyListener(this); 
    this.setFocusable(true); 
    this.requestFocus();
  }
  public void start(int delay, int size) {
    JFrame a = new JFrame(); 
    a.add( this ); 
    a.setVisible(true); 
    a.setSize(size, size); 
    this.timer = new Timer(delay, this);  
    this.timer.start(); 
  }
  public void mouseEntered(MouseEvent e) { }
  public void mouseExited(MouseEvent e) { }
  public void mousePressed(MouseEvent e) { 
    this.world.meh(e); 
    this.repaint(); 
  }
  public void mouseDragged(MouseEvent e) { 
    this.world.meh(e); 
    this.repaint(); 
  }
  public void mouseMoved(MouseEvent e) { } 
  public void mouseReleased(MouseEvent u) { 
    this.world.meh(u); 
    this.repaint(); 
  }
  public void mouseClicked(MouseEvent e) { }
  public void keyPressed(KeyEvent e) {
    this.world.keh(e);
    this.repaint();
  }
  public void keyReleased(KeyEvent e) { }  
  public void keyTyped(KeyEvent e) { } 
  // int count;
  public void actionPerformed(ActionEvent e) {
    // this.count += 1; 
    // System.out.println("Ouch" + this.count);     
    if (this.world.hasEnded()) { 
      this.timer.stop(); 
      this.world.sayBye(); 
    } else { 
      this.world.teh();
    }
    this.repaint();
  }
  public void paintComponent(Graphics g) {
    this.world.draw(g); 
  }
}

After I explain this I use the template and then write this:

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

public class Game implements World { 

  public Game() {

  }
  public void draw(Graphics g) { }  
  public void teh() { }
  public void meh(MouseEvent e) { }
  public void keh(KeyEvent e) { } 
  public boolean hasEnded() { 
    return false; 
  }
  public void sayBye() { }

  public static void main(String[] args) {
    BigBang b = new BigBang(new Game()); 
    b.start( 50, // delay 
            400  // size 
           ); 
  }

}

This is my very first game written for this game engine. 

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

public class Game implements World { 
  int count = 0; 
  public Game() {

  }
  public void draw(Graphics g) { }  
  public void teh() { 
    this.count += 1; 
    System.out.println("I am getting older: " + this.count); 
  }
  public void meh(MouseEvent e) {
    System.out.println("Mouse is moving ... "); 
  }
  public void keh(KeyEvent e) { } 
  public boolean hasEnded() { 
    return false; 
  }
  public void sayBye() { }

  public static void main(String[] args) {
    BigBang b = new BigBang(new Game()); 
    b.start( 1000, // delay 
              400  // size 
           ); 
  }

}
This would be my stage two. 

--

Then we ran a stage from the future from Bubble Shooter:

http://silo.cs.indiana.edu:8346/h212/spr2017/04202017/

There is World, BigBang in there. 

That's the framework. 

There's Game and Circle too -- that's your application. 

Note that Game follows the template we stated at the beginning. 

--