There are four exams in this class

Third exam: prepare you for the project

Final exam: based on the project

Time to use our knowledge of Java to learn the libraries. 

Today: events (keyboard, mouse), graphics

I want us to design our big-bang on Wednesday

Sample project: Tetris

Things to remember: 

  (a) JFrame 

  (b) JComponent 

import javax.swing.JFrame; 

public class Program extends JFrame {
  public Program() {
    this.add( new Board() ); 
  }
  public static void main(String[] args) {
    JFrame f = new Program(); 
    f.setVisible(true);
    f.setSize(300, 400); 
  }
}

import javax.swing.JComponent; 
import java.awt.Graphics;

public class Board extends JComponent {
  int i = 0; 
  public void paintComponent(Graphics g) {
    this.i += 1; 
    System.out.println( i + ": Howdy." );      
  }
}

--

import javax.swing.JFrame; 

public class Program extends JFrame {
  public Program() {
    this.add( new Board() ); 
  }
  public static void main(String[] args) {
    JFrame f = new Program(); 
    f.setVisible(true);
    f.setSize(300, 400); 
  }
}



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

public class Board extends JComponent implements MouseMotionListener {
  int x, y; // instance variable
  public Board() {
    this.addMouseMotionListener( this );  
  }
  public void mouseMoved(MouseEvent e) { 
    this.x = e.getX();
    this.y = e.getY(); 
    System.out.println( "(" + x + ", " + y + ")"); 
    repaint();
  }
  public void mouseDragged(MouseEvent e) { 
    System.out.println("Mouse being dragged.");
  }
  int i = 0; 
  public void paintComponent(Graphics g) {
    int RADIUS = 35; 
    this.i += 1; 
    System.out.println( i + ": Howdy." ); 
    g.setColor(Color.YELLOW); 
    g.fillOval(x-RADIUS, y-RADIUS, 2 * RADIUS, 2 * RADIUS); 
    g.setColor(Color.BLUE);
    g.drawOval(x-RADIUS, y-RADIUS, 2 * RADIUS, 2 * RADIUS); 
    g.setColor(Color.RED);
    g.drawString("(" + x + ", " + y + ")", x - RADIUS + 5, y);
  }
}

Final code:

import javax.swing.JFrame; 
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;

public class Program extends JFrame implements KeyListener {
  public void keyPressed(KeyEvent e) { 
    System.out.println( "I am the JFrame..." ); 
    this.board.up(); 
  } 
  public void keyReleased(KeyEvent e) { } 
  public void keyTyped(KeyEvent e) { } 
  Board board = new Board(); 
  public Program() {
    this.add( this.board ); 
    this.addKeyListener(this); 
  }
  public static void main(String[] args) {
    JFrame f = new Program(); 
    f.setVisible(true);
    f.setSize(300, 400); 
  }
}


import javax.swing.JComponent; 
import java.awt.Graphics;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseEvent;
import java.awt.Color; 
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;

public class Board extends JComponent implements MouseMotionListener, KeyListener {
  int x, y; // instance variable
  public Board() {
    this.addMouseMotionListener( this );  
    this.addKeyListener(this); 
  }
  public void mouseMoved(MouseEvent e) { 
    this.x = e.getX();
    this.y = e.getY(); 
    System.out.println( "(" + x + ", " + y + ")"); 
    repaint();
  }
  public void up() {
    this.y -= 1; 
    repaint();
  }
  public void keyPressed(KeyEvent e) { 
    System.out.println( "Ouch." ); 
  } 
  public void keyReleased(KeyEvent e) { } 
  public void keyTyped(KeyEvent e) { } 
  public void mouseDragged(MouseEvent e) { 
    System.out.println("Mouse being dragged.");
  }
  int i = 0; 
  public void paintComponent(Graphics g) {
    int RADIUS = 35; 
    this.i += 1; 
    System.out.println( i + ": Howdy." ); 
    g.setColor(Color.YELLOW); 
    g.fillOval(x-RADIUS, y-RADIUS, 2 * RADIUS, 2 * RADIUS); 
    g.setColor(Color.BLUE);
    g.drawOval(x-RADIUS, y-RADIUS, 2 * RADIUS, 2 * RADIUS); 
    g.setColor(Color.RED);
    g.drawString("(" + x + ", " + y + ")", x - RADIUS + 5, y);
  }
}

Why is the JComponent unable to react to key events? What did we do wrong? 

--