import java.awt.*;
import java.applet.*;
import java.util.*;
import java.awt.image.*;
import java.net.*;
public class AnimationClass extends Applet implements Runnable {
static int SCREENWIDTH = 640;
static int SCREENHEIGHT = 480;
Thread gameloop;
Random rand = new Random();
//double buffer objects
BufferedImage backbuffer;
Graphics2D g2d;
Image background;
//sprite variables
AnimatedSprite ball;
AnimatedSprite blackhole;
private URL getURL(String filename) {
URL url = null;
try {
url = this.getClass().getResource(filename);
}
catch (Exception e) {}
return url;
}
public void init() {
//create the back buffer for smooth graphics
backbuffer = new BufferedImage(SCREENWIDTH, SCREENHEIGHT,
BufferedImage.TYPE_INT_RGB);
g2d = backbuffer.createGraphics();
//load the background image
Toolkit tk = Toolkit.getDefaultToolkit();
background = tk.getImage(getURL("woodgrain.png"));
//load the ball animation strip
ball = new AnimatedSprite(this, g2d);
ball.load("xball.png", 8, 8, 64, 64);
ball.setPosition(new Point2D(300,200));
ball.setFrameDelay(1);
ball.setVelocity(new Point2D(1,1));
ball.setRotationRate(1.0);
blackhole = new AnimatedSprite(this, g2d);
blackhole.load("blackhole64x64x16.png", 8, 8, 64, 64);
blackhole.setPosition(new Point2D(100,200));
blackhole.setFrameDelay(2);
blackhole.setVelocity(new Point2D(1,1));
blackhole.setRotationRate(1.0);
}
public void start() {
gameloop = new Thread(this);
gameloop.start();
}
public void stop() {
gameloop = null;
}
public void run() {
Thread t = Thread.currentThread();
while (t == gameloop) {
try {
Thread.sleep(5);
}
catch (InterruptedException e) {
e.printStackTrace();
}
gameUpdate();
repaint();
}
}
public void gameUpdate() {
//see if it's time to animate
ball.updateAnimation();
blackhole.updateAnimation();
//update the ball position
ball.updatePosition();
if ((ball.position().X() < 0) || (ball.position().X() > SCREENWIDTH - 64)) {
double x = ball.velocity().X() * -1;
ball.setVelocity(new Point2D(x, ball.velocity().Y()));
x = ball.position().X();
ball.setPosition(new Point2D(x, ball.position().Y()));
}
if ((ball.position().Y() < 0) || (ball.position().Y() > SCREENHEIGHT - 64)) {
double y = ball.velocity().Y() * -1;
ball.setVelocity(new Point2D(ball.velocity().X(), y));
y = ball.position().Y() + ball.velocity().Y();
ball.setPosition(new Point2D(ball.position().X(), y));
}
blackhole.updatePosition();
if ((blackhole.position().X() < 0) || (blackhole.position().X() > SCREENWIDTH - 64)) {
double x = blackhole.velocity().X() * -1;
blackhole.setVelocity(new Point2D(x, blackhole.velocity().Y()));
x = blackhole.position().X();
blackhole.setPosition(new Point2D(x, blackhole.position().Y()));
}
if ((blackhole.position().Y() < 0) || (blackhole.position().Y() > SCREENHEIGHT - 64)) {
double y = blackhole.velocity().Y() * -1;
blackhole.setVelocity(new Point2D(blackhole.velocity().X(), y));
y = blackhole.position().Y() + blackhole.velocity().Y();
blackhole.setPosition(new Point2D(blackhole.position().X(), y));
}
//lastly, update the rotation
ball.updateRotation();
blackhole.updateRotation();
}
public void update(Graphics g) {
//draw the background
g2d.drawImage(background, 0, 0, SCREENWIDTH-1, SCREENHEIGHT-1, this);
//draw the current frame of animation
ball.draw();
blackhole.draw();
g2d.setColor(Color.BLACK);
g2d.drawString("Position: " + ball.position().X() + "," +
ball.position().Y(), 5, 10);
g2d.drawString("Velocity: " + ball.velocity().X() + "," +
ball.velocity().Y(), 5, 25);
g2d.drawString("Animation: " + ball.currentFrame(), 5, 40);
g2d.drawString("Position: " + blackhole.position().X() + "," +
ball.position().Y(), 5,450 );
g2d.drawString("Velocity: " + blackhole.velocity().X() + "," +
ball.velocity().Y(), 5, 465);
g2d.drawString("Animation: " + blackhole.currentFrame(), 5, 480);
paint(g);
}
public void paint(Graphics g) {
//draw the back buffer to the screen
g.drawImage(backbuffer, 0, 0, this);
}
}