// From A Beginning Programmer's Guide to Java
// at http://beginwithjava.blogspot.com/

// Referenced in the articles 'Interactive Keyboard Input in Java: KeyListeners"
// at http://beginwithjava.blogspot.com/
// and 'Listeners in Java: Can Java Hear You?'
// at http://beginwithjava.blogspot.com/2009/01/listeners-can-java-hear-you.html

/* KeyPanel.java

    This program adds the ability to respond to
    keystroke events to the BasicPanel program.

    The keystrokes are used to draw on the
    panel area by moving the cursor, leaving a
    path of rectangles behind. The space key
    clears the drawing area without moving the
    cursor location.

    mag-01Sep2010
*/

// Import the basic necessary classes.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class KeyPanel extends JPanel implements KeyListener{

    public KeyPanel(){
        super();
        pointX=0;
        pointY=0;
        oldX=0;
        oldY=0;
        addKeyListener(this);
    }

    int pointX, pointY, oldX, oldY;
    boolean erase;

    public void paintComponent(Graphics g){
    // Erase the board if it's been requested.
        if (erase) {
           g.clearRect(0, 0 , getBounds().width, getBounds().height);
           erase = false; // We're done, turn off this flag now.
        }

    // Draw gray where the pointer was..
        g.setColor(Color.GRAY); 
        g.fillRect(oldX-2, oldY-2, 4, 4);
    // Draw "Cursor" at current location in black.
        g.setColor(Color.BLACK);
        g.fillRect(pointX-2,pointY-2, 4, 4);
    }

    public void keyPressed(KeyEvent key){

    // Copy the last clicked location into the 'old' variables.
        oldX=pointX;
        oldY=pointY;
    // Move the current point depending on which key was pressed.
    if (key.getKeyCode() == key.VK_DOWN){
        pointY=pointY+5;
        if (pointY > getBounds().height){
            pointY=getBounds().height;
        }
    }
    if (key.getKeyCode() == key.VK_UP){
        pointY=pointY-5;
        if (pointY < 0){pointY=0;}
    }
    if (key.getKeyCode() == key.VK_LEFT){
        pointX=pointX-5;
        if (pointX < 0){pointX=0;}
    }
    if (key.getKeyCode() == key.VK_RIGHT){
        pointX=pointX+5;
        if (pointX > getBounds().width){
            pointX=getBounds().width;
        }
    }

    // Set a flag to tell paintComponent() to erase the screen.
    if (key.getKeyCode() == key.VK_SPACE){
        erase = true;
    }

    // Tell the panel that we need to redraw things.
        repaint();
    }

/* The following methods have to be here to comply
   with the MouseListener interface, but we don't
   use them, so their code blocks are empty. */
    public void keyTyped(KeyEvent key){ }   
    public void keyReleased(KeyEvent key){ }

    public static void main(String arg[]){
        JFrame frame = new JFrame("Use Arrows to Draw, Space to Erase.");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(640,480);

        KeyPanel panel = new KeyPanel();
        frame.setContentPane(panel);
        frame.setVisible(true);

        // We *must* do this to see KeyEvents.
    panel.setFocusable(true);

        // Initialize the drawing pointer.
        panel.oldX=panel.getBounds().width/2;
        panel.oldY=panel.getBounds().height/2;
        panel.pointX=panel.oldX;
        panel.pointY=panel.oldY;

    }
}
