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);
    }


    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);
    }


    public void keyPressed(int keycode)  { }
    public void keyReleased(int keycode) { }
    public void mousePressed (double x, double y) { }
    public void mouseDragged (double x, double y) { }
    public void mouseReleased(double x, double y) { }
    public void mouseClicked(double x, double y) { }

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

   
}


Copyright © 2000–2019, Robert Sedgewick and Kevin Wayne.
Last updated: Wed Jun 9 04:39:13 EDT 2021.