summaryrefslogtreecommitdiff
path: root/java/gjt/test/BumpAnimationTest.java
blob: 81b6fb5ce8e53cdb4393a06bd04338f0020536d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package gjt.test;

import java.net.URL;
import java.applet.Applet;
import java.awt.*;

import gjt.Util;
import gjt.Orientation;
import gjt.animation.*;

/**
 * A simple animation playfield with one sprite that bounces
 * off the boundaries of the playfield.<p>
 *
 * When the sprite bounces off the left wall, it plays a
 * bump sequence once; when it bounces off the right wall
 * it plays the bump sequence twice.<p>
 *
 * @version 1.0, Apr 1 1996
 * @author  David Geary
 * @see     gjt.animation.Playfield
 * @see     gjt.animation.Sprite
 */
public class BumpAnimationTest extends UnitTest {
    public String title() { 
        return "Bump Animation - Mouse Down Starts/Stops"; 
    } 
    public Panel centerPanel() { 
        return new BumpAnimationTestPanel(this); 
    }
}

class BumpAnimationTestPanel extends Panel {
    public BumpAnimationTestPanel(Applet applet) {
        setLayout(new BorderLayout());
        add("Center", new BumpPlayfield(applet)); 
    }
}

class BumpPlayfield extends Playfield {
    private Applet   applet;
    private URL      cb;
    private Sprite   javaDrinker;
    private Sequence spinSequence, bumpSequence;

    public BumpPlayfield(Applet applet) {
        this.applet = applet;
        cb          = applet.getCodeBase();
        makeSequencesAndSprites();
    }
    public void paintBackground(Graphics g) {
        Image bg = applet.getImage(cb, "gifs/background.gif");
        Util.wallPaper(this, g, bg);
    }
    public boolean mouseDown(Event event, int x, int y) {
        if(running() == true) stop ();
        else                  start();
        return true;
    }
    public void spriteCollision(Sprite sprite, Sprite sprite2) {
        // Nothing to do:  only 1 sprite!
    }
    public void edgeCollision(Sprite      sprite, 
                              Orientation orientation) {
        if(orientation == Orientation.RIGHT || 
           orientation == Orientation.LEFT) {
            if(sprite.getSequence() != bumpSequence) {
                sprite.reverseX();

                if(orientation == Orientation.RIGHT)
                    sprite.play(bumpSequence, 1);
                else
                    sprite.play(bumpSequence, 2);
            }
        }   
        else 
            sprite.reverseY();
    }
    private void makeSequencesAndSprites() {
        String  file;
        Point   startLoc   = new Point(10, 10);
        Image[] spinImages = new Image[19];
        Image[] bumpImages = new Image[6];

        for(int i=0; i < spinImages.length; ++i) {
            file = "gifs/spin";

            if(i < 10) file += "0" + i + ".gif";
            else       file += i + ".gif";

            spinImages[i] = applet.getImage(cb, file);
        }
        for(int i=0; i < bumpImages.length; ++i) {
            file = "gifs/bump0" + i + ".gif";
            bumpImages[i] = applet.getImage(cb, file);
        }
        spinSequence = new Sequence(this, spinImages);
        bumpSequence = new Sequence(this, bumpImages);
        javaDrinker  = new Sprite(this, spinSequence, startLoc);

        spinSequence.setAdvanceInterval(100);
        bumpSequence.setAdvanceInterval(200);

        javaDrinker.setMoveVector(new Point(2,2));
        addSprite(javaDrinker);
    }
}