You are being forwarded to the lastest updates ot his page!
Or you can Click Here if it doesn't work or you don't wish to wait.

Sprite Animation

Gregg Bolinger
This thread and the fact that I have started looking into some rudamentary 2D Graphics type games make me start wondering how to handle sprite animation in Java. I'm not talking about simply moving an image around on the screen but actually loading several images that when looped over make an animation, say, of a guy walking, or whatever. Does anyone have any links to the best methods for handling this in Java?

Thanks.


saager mhatre
Hey Gregg,
It's just an idea, but wouldn't this kind of sprite animation be better off included in the Image Producer/Consumer API ??


crwood
I got started with the old Sun tutorial which had a page on animation. It has been retired but (they say it) is available for download on this page: Download the Java Tutorial. I think you want the Online Tutorial link. The rest I learned on my own.

For background information on painting you might check the currrent tutorial page: Implementing a Custom Component.

Here's an example of animation in Swing:
code:

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.io.*;
import java.net.*;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.Timer;

public class AnimationTest
{
public static void main(String[] args)
{
AnimationPanel panel = new AnimationPanel();
AnimationController control = new AnimationController(panel);
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(control.getUIPanel(), "North");
f.getContentPane().add(panel);
f.setSize(400,200);
f.setLocation(200,200);
f.setVisible(true);
}
}

class AnimationPanel extends JPanel
{
Image[] images;
Timer timer;
int imageIndex;

public AnimationPanel()
{
loadImages();
imageIndex = 0;
timer = new Timer(175, new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
imageIndex = (imageIndex + 1) % images.length;
repaint();
}
});
timer.start();
setBackground(Color.white);
}

protected void paintComponent(Graphics g)
{
super.paintComponent(g);
int w = getWidth();
int h = getHeight();
int imageWidth = images[imageIndex].getWidth(this);
int imageHeight = images[imageIndex].getHeight(this);
int x = (w - imageWidth)/2;
int y = (h - imageHeight)/2;
g.drawImage(images[imageIndex], x, y, this);
}

private void loadImages()
{
String prefix = "http://java.sun.com/docs/books/tutorial/uiswing/" +
"components/example-1dot4/images/tumble/T";
String ext = ".gif";
images = new Image[17];
for(int j = 0; j < images.length; j++)
try
{
URL url = new URL(prefix + (j + 1) + ext);
images[j] = ImageIO.read(url);
}
catch(MalformedURLException mue)
{
System.out.println("bad url: " + mue.getMessage());
}
catch(IOException ioe)
{
System.out.println("read trouble: " + ioe.getMessage());
}
}

public void setDelay(int delay)
{
timer.setDelay(delay);
}

public void start()
{
if(!timer.isRunning())
timer.start();
}

public void stop()
{
if(timer.isRunning())
timer.stop();
}
}

class AnimationController
{
AnimationPanel animationPanel;

public AnimationController(AnimationPanel ap)
{
animationPanel = ap;
}

public JPanel getUIPanel()
{
final JButton
start = new JButton("start"),
stop = new JButton("stop");
ActionListener l = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JButton button = (JButton)e.getSource();
if(button == start)
animationPanel.start();
if(button == stop)
animationPanel.stop();
}
};
start.addActionListener(l);
stop.addActionListener(l);
final SpinnerNumberModel model = new SpinnerNumberModel(175, 75, 200, 1);
JSpinner spinner = new JSpinner(model);
spinner.addChangeListener(new ChangeListener()
{
public void stateChanged(ChangeEvent e)
{
int value = model.getNumber().intValue();
animationPanel.setDelay(value);
}
});
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(2,1,2,1);
gbc.weightx = 1.0;
panel.add(start, gbc);
panel.add(stop, gbc);
gbc.anchor = gbc.EAST;
panel.add(new JLabel("delay"), gbc);
gbc.anchor = gbc.WEST;
panel.add(spinner, gbc);
return panel;
}
}

[ June 27, 2004: Message edited by: Craig Wood ]