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

/**
 * This class demonstrates the use of a KeyListener
 * to handle keyboard events.  
 */
public class ChangeBallWithKeys extends WindowController implements KeyListener {
    
    private static final double INIT_SIZE = 20;
    private static final double STEP_SIZE = 20;
    private static final double START_X = 100;
    private static final double START_Y = 100;
    
    private FilledOval ball;
    
    public void begin() {
        ball = new FilledOval(START_X, START_Y, INIT_SIZE, INIT_SIZE, canvas);
        ball.setColor(Color.red);
        
        new Text("Press Up, Down, B, or R", 10, 10, canvas);

        // Add a key listener to the canvas so that
        // we get key events. 
        this.setFocusable(true);
        this.requestFocus();
        this.addKeyListener(this);
        canvas.addKeyListener(this);
    }
    
    /*
     * Handle up,down,b,r keystrokes.  Note that we
     * use virtual key code constants (ie VK_UP).  These
     * are used to abstract away the differences between
     * different keyboards and alphabets
     */
    public void keyPressed(KeyEvent event) {
        
        if (event.getKeyCode() == KeyEvent.VK_UP) {
            
            double newSize = ball.getWidth() + STEP_SIZE;
            ball.setSize(newSize, newSize);
            ball.move(-STEP_SIZE/2, -STEP_SIZE/2);
            
        } else if (event.getKeyCode() == KeyEvent.VK_DOWN) {
            
            double newSize = ball.getWidth() - STEP_SIZE;
            ball.setSize(newSize, newSize);
            ball.move(STEP_SIZE/2, STEP_SIZE/2);
            
        } else if (event.getKeyCode() == KeyEvent.VK_B) {
            
            ball.setColor(Color.blue);
            
        } else if (event.getKeyCode() == KeyEvent.VK_R) {
            
            ball.setColor(Color.red);
        } 
    }
    
    /* 
     * Part of KeyListener, but we don't need  
     * to do anything here.
     */
    public void keyTyped(KeyEvent evt) {
    }
    
    /* 
     * Part of KeyListener, but we don't need  
     * to do anything here.
     */
    public void keyReleased(KeyEvent evt) {
    }
}
