1 Replies - 5580 Views - Last Post: 19 July 2011 - 02:25 PM Rate Topic: -----

#1 kamran619  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 74
  • Joined: 05-July 11

Create a Slideshow in Java

Posted 19 July 2011 - 02:13 PM

My pictures won't change every-time my timer executes..I tried making an array of JLabels and adding them as well as making an array of ImageIcons, neither work. What am I doing wrong?

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Slideshow extends JFrame{
  public Slideshow(){
    add(new PicturePanel());
  }
  
  public static void main(String[] args){
   Slideshow frame = new Slideshow(); 
   frame.setTitle("Shhh");
   frame.setLocationRelativeTo(null);
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   frame.setSize(800,600);
   frame.setVisible(true);
  }
  
  class PicturePanel extends JPanel{
    private int counter = 0;
    private ImageIcon[] image = new ImageIcon[25];
    public PicturePanel(){
     Timer timer = new Timer(100, new TimerListener());
     timer.start();
     
    }
    
    class TimerListener implements ActionListener{
      public void actionPerformed(ActionEvent e){
        if (counter == 25){
      counter = 0; 
     }
        repaint(); 
        counter++;
      }
    }
    
    protected void paintComponent(Graphics g){
     super.paintComponent(g);
     image[counter] = new ImageIcon("C:/Users/Kamran/Desktop/evennumberedexercise/image/slide" + counter + ".jpg");
     add(new JLabel(image[counter]));
    }
    
  }
  
  
  
}


Is This A Good Question/Topic? 0
  • +

Replies To: Create a Slideshow in Java

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Create a Slideshow in Java

Posted 19 July 2011 - 02:25 PM

No this is not the way to do it
You cannot just add and add and add JLabel to a JPanel

Add only a single one JLabel only once
When your actionPerformed is called just change the icone of that JLabel single JLabel
No need to call repaint() if you touch a JComponent contained in a JComponent the father component will be repaiint()

Also not a good idea to read your ImageIcon in an ActionListener better to read it once and use it later

Something like

  class PicturePanel extends JPanel{
    private int counter = 0;
    private ImageIcon[] image = new ImageIcon[25];
    private JLabel label;
    
    public PicturePanel(){
     for(int i = 0; i < image.length; ++ i)
         image[counter] = new ImageIcon("C:/Users/Kamran/Desktop/evennumberedexercise/image/slide" + i + ".jpg");

     label = new JLabel();
     add(label);
     
     Timer timer = new Timer(100, new TimerListener());
     timer.start();
     
    }
    
    class TimerListener implements ActionListener{
      public void actionPerformed(ActionEvent e){
        counter++;
        counter %= image.length;
        label.setIcon(image[counter]);  
      }
    }
    


This post has been edited by pbl: 19 July 2011 - 02:26 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1