开发者

Frame refreshing issue

开发者 https://www.devze.com 2023-01-14 14:07 出处:网络
I am trying to create a panel with changing pictures. This is my panel: public class AdvertisementPanel extends JPanel {

I am trying to create a panel with changing pictures. This is my panel:

public class AdvertisementPanel extends JPanel {
private BufferedImage image;

private ArrayList<String> pictures;
private int index = 0;

public AdvertisementPanel(String... pics) {
pictures = new ArrayList<String>(Arrays.asList(pics));
Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(
 new Runnable() {
  @Override
  public void run() {
   changeImage();
  }
}, 0, 5, TimeUnit.SECONDS);

}

public void p开发者_运维技巧aint(Graphics g) {
 g.drawImage(image, 0, 0, null);
}

private void changeImage() {
 String name = pictures.get(index);
try {
 File input = new File(name);
 image = ImageIO.read(input);
 index++;
 index %= pictures.size();
} catch (IOException ie) {
 Logger.getLogger().log(Level.SEVERE,
  "No adds found in given path: " + name);
}
}

I have a frame that holds the panel, but no pictures are shown. Tried to repaint periodically from the frame - caused some funny, yet unwanted results... Any ideas why? What am I doing wrong? How should I refresh the frame's components?


you need to repaint each time you change the image.

Oh, and it should be done by the swing event handling thread:

Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(
 new Runnable() {
  @Override
  public void run() {
    SwingUtilities.invokeLater(new Runnable() {
      @Override
      public void run() {
        changeImage();
        repaint();
       }
    };
  }
}, 0, 5, TimeUnit.SECONDS);

UPDATE To correct a few other issues

public class AdvertisementPanel extends JPanel {
    private BufferedImage image;
    private ArrayList<String> pictures;
    private int index = 0;

    public AdvertisementPanel(String... pics) {
        pictures = new ArrayList<String>(Arrays.asList(pics));
        Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(
            new Runnable() {
                @Override
                public void run() {
                    changeImage();
                }
            }, 0, 5, TimeUnit.SECONDS);
    }

    private void changeImage() {
        final BufferedImage img = nextImage();
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                image = img;
                repaint();
            }
        });
    }

    public void paint(Graphics g) {
        if (image != null) {
            g.drawImage(image, 0, 0, null);
        }
    }

    private BufferedImage nextImage() {
        String name = pictures.get(index);
        try {
            index++;
            index %= pictures.size();
            File input = new File(name);
            return ImageIO.read(input);
        } catch (IOException ie) {
            Logger.getLogger("").log(Level.SEVERE,
                    "No adds found in given path: " + name);
            return null;
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消