-1

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();
}

编辑代码是这样的^^

4

2 回答 2

1

最好使用 JLabel 将图像显示为 ImageIcons 并简单地让计时器交换您的图像。我可以发布一个示例程序,例如:

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();
         }
      });
   }
}

但是,在不知道程序结构的情况下,很难知道这是否对您有帮助。

于 2013-07-12T01:30:11.187 回答
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");

    }
}

如果您有任何不明白的地方,请告诉我,我很乐意为您总结,我认为所有这些都是您使用过的东西,判断您正在尝试做的事情。

于 2013-07-12T00:43:46.090 回答