summaryrefslogtreecommitdiff
path: root/java/gjt/test/TwoDrinkersAnimationTest.java
blob: ae4041b9eb6981fd8e3484f0711fb028d6b9b257 (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package gjt.test;

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

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

/**
 * An animation playfield containing two "java drinkers", that
 * both bounce off the sides of the playfield.<p>
 *
 * One of the java drinkers moves slow and spins fast, while
 * the other java drinker moves fast and spins slow.  When
 * the two java drinkers collide, they both play a bump
 * sequence - at different speeds.<p>
 *
 * @version 1.0, Apr 1 1996
 * @author  David Geary
 * @see     gjt.test.AnimationTest
 * @see     gjt.animation.Playfield
 * @see     gjt.animation.Sprite
 */
public class TwoDrinkersAnimationTest extends UnitTest {
    public String title() { 
        return 
            "TwoDrinkers Animation - Mouse Down Starts/Stops"; 
    } 
    public Panel centerPanel() { 
        return new TwoDrinkersAnimationTestPanel(this); 
    }
}

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

class TwoDrinkersPlayfield extends Playfield {
    private Applet   applet;
    private URL      cb;
    private Sprite   moveFastSpinSlow, moveSlowSpinFast;
    private Sequence fastSpinSequence, 
                     slowSpinSequence,
                     fastBumpSequence,
                     slowBumpSequence;

    public TwoDrinkersPlayfield(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) {
        if(moveSlowSpinFast.getSequence() != fastBumpSequence) {
            sprite.reverse();
            sprite2.reverse();

            moveSlowSpinFast.play(fastBumpSequence, 3);
            moveFastSpinSlow.play(slowBumpSequence, 3);
        }   
    }
    public void edgeCollision(Sprite      sprite, 
                              Orientation orientation) {
        if(orientation == Orientation.RIGHT || 
           orientation == Orientation.LEFT) 
            sprite.reverseX();
        else 
            sprite.reverseY();
    }
    private void makeSequencesAndSprites() {
        String  file;
        Image[] spinImages = new Image[19];
        Image[] bumpImages = new Image[6];
        Image[] volleyball = new Image[4];

        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);
        }
        fastSpinSequence = new Sequence(this, spinImages);
        slowSpinSequence = new Sequence(this, spinImages);

        fastBumpSequence = new Sequence(this, bumpImages);
        slowBumpSequence = new Sequence(this, bumpImages);

        moveFastSpinSlow = 
            new Sprite(this, 
                slowSpinSequence, new Point(25, 75));

        moveSlowSpinFast = 
            new Sprite(this, 
                fastSpinSequence, new Point(250,250));

        fastSpinSequence.setAdvanceInterval(50);
        slowSpinSequence.setAdvanceInterval(300);

        fastBumpSequence.setAdvanceInterval(25);
        slowBumpSequence.setAdvanceInterval(200);

        moveFastSpinSlow.setMoveVector(new Point(2,3));
        moveSlowSpinFast.setMoveVector(new Point(-1,-1));

        moveSlowSpinFast.setMoveInterval(100);

        addSprite(moveFastSpinSlow);
        addSprite(moveSlowSpinFast);
    }
}