forked from CCSF-Coders/ExplosionDemo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimationTest.java
More file actions
131 lines (111 loc) · 3.44 KB
/
Copy pathAnimationTest.java
File metadata and controls
131 lines (111 loc) · 3.44 KB
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
131
// AnimationTest Demo
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.awt.image.*;
import java.net.*;
public class AnimationTest extends JFrame implements Runnable
{
static int SCREENWIDTH = 640;
static int SCREENHEIGHT = 480;
Thread gameloop;
Random rand = new Random();
// double buffer objects
BufferedImage backbuffer;
Graphics2D g2d;
//sprite variables
Image image;
Point pos = new Point(300,200);
// Animation variables
int currentFrame = 0;
int totalFrames = 30;
int animationDirection = 1;
int frameCount = 0;
int frameDelay = 10;
public static void main(String[] args)
{
new AnimationTest();
}
public AnimationTest()
{
super("Animation Test");
setSize(SCREENWIDTH,SCREENHEIGHT);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// create the back buffer for smooth graphics
backbuffer = new BufferedImage(SCREENWIDTH,SCREENHEIGHT,
BufferedImage.TYPE_INT_RGB);
g2d = backbuffer.createGraphics();
// load the ball animation strip
Toolkit tk = Toolkit.getDefaultToolkit();
image = tk.getImage(getURL("explosion.png"));
gameloop = new Thread(this);
gameloop.start();
}
private URL getURL(String filename)
{
URL url = null;
try{
url= this.getClass().getResource(filename);
}
catch(Exception e) {}
return url;
}
public void run()
{
Thread t = Thread.currentThread();
while (t==gameloop)
{
try{
Thread.sleep(5);
}
catch (InterruptedException e) {
e.printStackTrace();
}
gameUpdate();
}
}
public void gameUpdate()
{
// Clear the background
g2d.setColor(Color.BLACK);
g2d.fill(new Rectangle(0,0, SCREENWIDTH-1,SCREENHEIGHT-1));
// draw the current frame of animation
drawFrame(image, g2d, pos.x, pos.y, 6, currentFrame, 128, 128);
g2d.setColor(Color.WHITE);
g2d.drawString("Position: " + pos.x +","+pos.x, 10, 50);
g2d.drawString("Animation: " + currentFrame, 10, 70);
// see if it's time to animate;
frameCount++;
if(frameCount > frameDelay)
{
frameCount = 0;
// update the animation frame
currentFrame +=animationDirection;
if (currentFrame > totalFrames -1)
{
currentFrame = 0;
pos.x = rand.nextInt(SCREENWIDTH - 128);
pos.y = rand.nextInt(SCREENHEIGHT - 128);
}
else if(currentFrame < 0)
{
currentFrame = totalFrames - 1;
}
}
repaint();
}
public void paint(Graphics g)
{
// draw the back buffer to the screen
g.drawImage(backbuffer, 0, 0, this);
}
// draw a single frame of animation
public void drawFrame(Image source, Graphics2D dest,
int x, int y, int cols, int frame, int width, int height)
{
int fx = (frame % cols) * width;
int fy = (frame / cols) * height;
dest.drawImage(source, x, y, x+width, y+height, fx, fy, fx+width, fy+height, this);
}
}