如何在Swing计时器完成后更改图像?我知道它在完成后会在actionPerformed类中播放代码,但我无法让它工作。
如果更改了任何内容,我会使用paint方法绘制图像:)
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();
}
编辑这段代码与^^
相似答案 0 :(得分:1)
我无法发表评论所以我会在这里说出来并在你回答时进行编辑,到目前为止你的代码是什么?你试过什么?有很多方法可以实现你想要的,但你的问题有点缺乏细节。
编辑: 您没有显示名为grow的字段,并且通过名称grow获得字段/变量并且使用名为grow的方法不是一个好主意。
根据你的不同,你可以做的一件事就是。
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");
}
}
如果有任何你不明白的事情让我知道,我会很乐意向你总结,我认为所有这些都是你曾经合作的东西,判断你想要做什么。
答案 1 :(得分:1)
最好使用JLabel将图像显示为ImageIcons,只需让Timer交换图像即可。我可以发布一个示例程序,例如:
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();
}
});
}
}
但是,如果不知道你的程序结构,很难知道这是否会对你有帮助。