How do I get an image to change after a Swing timer is done? I know it plays the code in the actionPerformed class when it's done, but I could not get it to work.
I have the image painted using the paint method if that changes anything :)
public class Sprite extends JFrame implements ActionListener{
private Board board;
private Timer timer;
public Sprite() {
timer = new Timer(1000, this);
grow=false;
}
public Image grow() {
if(grow)
return image;
else
return other_image;
}
public void actionPerformed(ActionEvent e) {
grow = false;
board.repaint();
}
EDIT The code is something along the lines of this ^^
Better to use a JLabel to display the Images as ImageIcons and simply have the Timer swap your images. I can post a sample program such as this one:
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;
public class TimerImageSwapper {
public static final String[] IMAGE_URLS = {
"http://imaging.nikon.com/lineup/dslr/d7000/img/sample/img_01.png",
"http://imaging.nikon.com/lineup/dslr/d7000/img/sample/img_02.png",
"http://imaging.nikon.com/lineup/dslr/d7000/img/sample/img_04.png",
"http://imaging.nikon.com/lineup/dslr/d3200/img/sample/img_08.png",
"http://imaging.nikon.com/lineup/dslr/d3200/img/sample/img_05.png",
"http://imaging.nikon.com/lineup/dslr/d3200/img/sample/img_01.png",
"http://imaging.nikon.com/lineup/dslr/d3200/img/sample/img_06.png"
};
private static final int TIMER_DELAY = 1000;
private ImageIcon[] icons = new ImageIcon[IMAGE_URLS.length];
private JLabel mainLabel = new JLabel();
private int iconIndex = 0;;
public TimerImageSwapper() throws IOException {
for (int i = 0; i < icons.length; i++) {
URL imgUrl = new URL(IMAGE_URLS[i]);
BufferedImage image = ImageIO.read(imgUrl);
icons[i] = new ImageIcon(image);
}
mainLabel.setIcon(icons[iconIndex ]);
new Timer(TIMER_DELAY, new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
iconIndex++;
iconIndex %= IMAGE_URLS.length;
mainLabel.setIcon(icons[iconIndex]);
}
}).start();
}
public Component getMainComponent() {
return mainLabel;
}
private static void createAndShowGui() {
TimerImageSwapper timerImageSwapper;
try {
timerImageSwapper = new TimerImageSwapper();
JFrame frame = new JFrame("Timer Image Swapper");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(timerImageSwapper.getMainComponent());
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
But again without knowing the structure of your program, it will be hard to know if this will help you.
I can't comment so I will just say it here and edit when you answer, What is your code like so far? what have you tried ? there are quite a few ways to achieve what you want, but your question is sort of lacking detail.
edit:
You arn't showing a field named grow, and it isn't a good idea to have a field/variable by the name of grow and have a method named grow.
One thing you can do according to yours would be something like.
public class Sprite extends JFrame implements ActionListener {
public boolean Grown;
public Board board; // never set in your example,
// nor do i have any idea what it is for
public Timer timer;
public BufferedImage UngrownImage; // Initialize these images yourself
public BufferedImage GrownImage;
public sprite() {
Grown = false;
timer = new Timer(1000, this);
timer.start();
}
public void actionPerformed(ActionEvent e) {
Grown = true;
board.repaint(); // you may want to call super.repaint() too
// but again I do not know what board is.
}
// unless your "Board" is taking care of it, here you can paint the images
public void paint(Graphics g) {
super.paint(g);
int imgX = 0, imgY = 0;
if(Grown && GrownImage != null)
g.drawImage(GrownImage, imgX, imgY, null);
else if(UngrownImage != null)
g.drawImage(UngrownImage, imgX, imgY, null);
else
System.out.println("error: No images loaded");
}
}
If there is anything you do not understand let me know, I will gladly summarize it to you, I assumed that all this is stuff you have worked with, judging off of what you are trying to do.