Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
288 views
in Technique[技术] by (71.8m points)

java - How do I get a timer to change a picture?

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 ^^

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...