summaryrefslogtreecommitdiff
path: root/java/gjt/animation/Sequence.java
blob: 7b777ecd0d8670cb1f8d9aa58202c72c07f85b86 (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
package gjt.animation;

import java.util.Vector;
import java.awt.*;
import java.awt.image.ImageObserver;
import gjt.Util;
import gjt.Stopwatch;

/**
 * A sequence of images used in an animation.  Each sequence
 * keeps track of the number of cycles the sequence is to run,
 * and reports whether or not the cycles have been completed
 * via the boolean animationOver() method.
 *
 * @version 1.0, Apr 1 1996
 * @author  David Geary
 * @see     Sprite
 * @see     Playfield
 * @see     gjt.test.SimpleAnimationTest
 * @see     gjt.test.BumpAnimationTest
 * @see     gjt.test.TwoDrinkersAnimationTest
 */
public class Sequence {
    private static long infiniteCycle = -1;

    private Vector    cells               = new Vector();
    private Point     lastPaintLocation   = new Point(0,0);
    private Stopwatch cellAdvanceTimer    = new Stopwatch();
    private Image     currentImage, lastImagePainted;
    private long      cellAdvanceInterval = 0,
                      currentCycle        = 0, 
                      cyclesPerAnimation  = 0;

    public Sequence() { }

    public Sequence(Component component, Image[] images) {
        for(int i=0; i < images.length; ++i) {
            addImage(component, images[i]);
        }
        cyclesPerAnimation = infiniteCycle;
    }
    public void  start          () { cellAdvanceTimer.start(); }
    public Image getLastImage   () { return lastImagePainted;  }
    public Point getLastLocation() { return lastPaintLocation; }
    public int   getNumImages   () { return cells.size();      } 

    public long getCurrentCycle()       { return currentCycle; }
    public void setCurrentCycle(long c) { currentCycle = c;    }

    public long getCyclesPerAnimation() { 
        return currentCycle;            
    }
    public void setCyclesPerAnimation(long cyclesPerAnimation) { 
        this.cyclesPerAnimation = cyclesPerAnimation;
    }
    public Image getFirstImage() {
        return (Image)cells.firstElement();
    }
    public Image getCurrentImage() { 
        return currentImage;      
    }
    public int getCurrentImagePosition() { 
        return cells.indexOf(currentImage); 
    }
    public Image getNextImage() {
        int   index = cells.indexOf(currentImage);
        Image image;

        if(index == cells.size() - 1)   
            image = (Image)cells.elementAt(0);
        else                                
            image = (Image)cells.elementAt(index + 1); 

        return image;
    }
    public void setAdvanceInterval(long interval) {
        cellAdvanceInterval = interval;
    }
    public void addImage(Component component, Image image) {
        if(currentImage == null)
            currentImage = image;

        Util.waitForImage(component, image);
        cells.addElement(image);
    }
    public void removeImage(Image image) {
        cells.removeElement(image);
    } 
    public boolean needsRepainting(Point point) {
        return (lastPaintLocation.x != point.x ||
                lastPaintLocation.y != point.y ||
                lastImagePainted    != currentImage);
    }
    public void paint(Graphics g, int x, int y, 
                      ImageObserver observer) {
        g.drawImage(currentImage, x, y,  observer);
        lastPaintLocation.x = x;
        lastPaintLocation.y = y;
        lastImagePainted    = currentImage;
    }
    public boolean isAtLastImage() {
        return getCurrentImagePosition() == (cells.size() - 1);
    }
    public boolean timeToAdvanceCell() {
        return 
          cellAdvanceTimer.elapsedTime() > cellAdvanceInterval;
    }
    public boolean animationOver() {
        return (cyclesPerAnimation != infiniteCycle) &&
               (currentCycle >= cyclesPerAnimation);
    }
    public void advance() {
        if(isAtLastImage())
            ++currentCycle;

        currentImage = getNextImage();
        cellAdvanceTimer.reset();
    }
}