MovingJoker.java


Below is the syntax highlighted version of MovingJoker.java from §3.6 Case Study: Purple America.


/******************************************************************************
 *  Compilation:  javac MovingJoker.java
 *  Execution:    java MovingJoker
 *  Dependencies: Draw.java DrawListener.java joker.gif
 *
 *  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 Draw draw = new Draw();
    private double x = 0.5, y = 0.5;

    public MovingJoker() {
        draw.addListener(this);
        draw.enableDoubleBuffering();
        draw.clear(Color.GRAY);
        draw.picture(x, y, "joker.gif");
        draw.text(.5, .1, "Press 'i', 'j', 'k' or 'l' to move");
        draw.show();
        draw.pause(10);
    }

    // this method is called whenever a key is typed in the Draw window
    public void keyTyped(char c) {
        if      (c == 'i')  y += 0.02;   // up
        else if (c == 'j')  x -= 0.02;   // left
        else if (c == 'k')  y -= 0.02;   // down
        else if (c == 'l')  x += 0.02;   // right
        x = (1 + x) % 1;                 // periodic boundary conditions
        y = (1 + y) % 1;
        draw.clear(Color.GRAY);
        draw.picture(x, y, "joker.gif");
        draw.show();
        draw.pause(10);
    }

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



}


Copyright © 2000–2022, Robert Sedgewick and Kevin Wayne.
Last updated: Thu Aug 11 10:27:28 EDT 2022.