我如何获得更改图片的计时器?

时间:2023-01-21 07:27:27

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.

Swing计时器完成后如何更改图像?我知道它在完成后会在actionPerformed类中播放代码,但我无法让它工作。

I have the image painted using the paint method if that changes anything :)

我有使用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();
}

EDIT The code is something along the lines of this ^^

编辑代码是这个^^的行

2 个解决方案

#1


1  

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:

最好使用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();
         }
      });
   }
}

But again without knowing the structure of your program, it will be hard to know if this will help you.

但是,如果不知道程序的结构,很难知道这是否会对您有所帮助。

#2


1  

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.

编辑:您没有显示名为grow的字段,并且通过名称grow获得字段/变量并且使用名为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.

如果你有任何不明白的事情让我知道,我很乐意总结给你,我认为所有这些都是你曾经合作的东西,判断你想要做什么。

#1


1  

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:

最好使用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();
         }
      });
   }
}

But again without knowing the structure of your program, it will be hard to know if this will help you.

但是,如果不知道程序的结构,很难知道这是否会对您有所帮助。

#2


1  

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.

编辑:您没有显示名为grow的字段,并且通过名称grow获得字段/变量并且使用名为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.

如果你有任何不明白的事情让我知道,我很乐意总结给你,我认为所有这些都是你曾经合作的东西,判断你想要做什么。