Sprites: Sprite Animation
Sprites: Sprite Animation
Sprites
A sprite cannot draw itself without the JFrame and Graphics2D objects in a
main program.
SPRITE ANIMATION
Essential ways to animate a graphic object on the screen:
1) Loading individual frames, each stored in its own bitmap file (in sequence),
in my opinion not quite viable.
2) Loading a single bitmap containing rows and columns of animation frames
(as tiles), in my opinion a better way, since you can easily add more and
more images.
This will give you the correct row down into the image where your desired
frame is located, but it will not provide you with the actual column, or X value.
For that, you need a similar solution. Instead of dividing the frame number by
columns, we will use modulus. This calculation focuses on the remainder as
the answer we want.
Complete method:
Alejandro Gleason Méndez A01703013 Videojuegos
public BufferedImage grabImage(int col, int row, int width, int height){
BufferedImage img = image.getSubimage((col*95)-95,(row*52)-
52, width, height);//Starting point
return img;
}
Update method that increments the animation frame and then tests it
against the bounds of the animation sequence. For instance:
frameCount++;
if (frameCount > frameDelay) {
frameCount=0;
currentFrame += animationDirection;
if (currentFrame > totalFrames-1) {
currentFrame = 0;
}
else if (currentFrame < 0) {
currentFrame = totalFrames-1;
}
}
Images are a central part of every game, and this chapter examines how we
can (efficiently) load and display them, apply visual effects such as blurring,
fading, and rotation, and animate them.
import javax.swing.*;
import java.awt.*;
public class ShowImage extends JApplet
{
private Image im;
{ g.drawImage(im, 0, 0, this); } }
The getDocumentBase() method returns the URL of the directory holding the
original Web document, and this is prepended to the image's filename to get
a URL suitable for getImage(). The problem with image retrieval is the speed.
The getImage() method is poorly named since it doesn't get (or download)
the image at all. Instead it prepares an empty Image object (im) for holding
the image, returning immediately after that.
On the other hand, a JAR (Java Archive) file is a way of packing code and
resources together into a single, compressed file, resources can be anything.
If an applet (or application) is going to utilize a lot of images, repeated
network connections to download them will severely reduce execution
speed. It's much better to create a single JAR file containing the applet (or
application) and all the images, and have the browser (or user) download it.
Then, when an image comes to be loaded, it's a fast, local load from the JAR
file, loading changes a little:
}
Alejandro Gleason Méndez A01703013 Videojuegos
{
private BufferedImage im;
im = ImageIO.read( getClass().getResource("ball.gif") ); }
catch(IOException e) {
System.out.println("Load Image error:");
}
} // end of init()
public void paint(Graphics g)
{ g.drawImage(im, 0, 0, this); }
}