INTRODUCTION TO COMPUTER SCIENCE
Robert Sedgewick and Kevin Wayne


This is the syntax highlighted version of MovingJoker.java.

/*************************************************************************
 *  Compilation:  javac MovingJoker.java
 *  Execution:    java MovingJoker
 *  Dependencies: InteractiveDraw.java DrawListener.java
 *
 *  Display the joker and move it when the user types one of the 
 *  keys 'i', 'j', 'k', or 'l'.
 *
 *************************************************************************/

import java.awt.Color;

public class MovingJoker implements DrawListener {
    private int SIZE = 512;
    private InteractiveDraw d = new InteractiveDraw(SIZE, SIZE);
    private double x = SIZE / 2.0;
    private double y = SIZE / 2.0;

    public MovingJoker() {
        d.addListener(this);
        d.clear(Color.gray);
        d.go(x, y);
        d.spot("joker.gif");
        d.go(256, 40);
        d.write("Press 'i', 'j', 'k' or 'l' to move");
        d.show();
    }


    public void keyTyped(char c) {
        if      (c == 'i')  y += 10;              // up
        else if (c == 'j')  x -= 10;              // left
        else if (c == 'k')  y -= 10;              // down
        else if (c == 'l')  x += 10;              // right
        x = (512 + x) % 512;                      // periodic boundary conditions
        y = (512 + y) % 512;
        d.clear();
        d.go(x, y);
        d.spot("joker.gif");
        d.show();
    }

    public void mouseDragged(double x, double y) { }
    public void mouseClicked(double x, double y) { }



    // test client
    public static void main(String args[]) {
        MovingJoker m = new MovingJoker();
    }
 

   
}


Last updated: Mon May 3 15:16:28 EDT 2004 .
Copyright © 2004, Robert Sedgewick and Kevin Wayne.