5

For class I'm working on my first GUI application. It's just a simple image viewer with four buttons: Previous, Next, Stop, Play. Previous and Next work fine, but honestly I don't even know how to begin working on the slideshow part (Play & Stop). I know there's a timer class that would probably be handy for controlling the speed as the images change...but I'm not sure what kind of logic is typically used to cycle through the images. Can anyone point me in the right direction, my brain is a little fried at this point :0

I've included my code below. I'm new to this, so hopefully people won't be too critical of my technique. If it matters, I'm working in eclipse.

here's my code so far:

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


public class ImageGallery extends JFrame
{
    private ImageIcon myImage1 = new ImageIcon ("Chrysanthemum.jpg");
    private ImageIcon myImage2 = new ImageIcon ("Desert.jpg");
    private ImageIcon myImage3 = new ImageIcon ("Jellyfish.jpg");
    private ImageIcon myImage4 = new ImageIcon ("Penguins.jpg");
    JPanel ImageGallery = new JPanel();
    private ImageIcon[] myImages = new ImageIcon[4];
    private int curImageIndex=0;

    public ImageGallery ()
        {   
            ImageGallery.add(new JLabel (myImage1));
            myImages[0]=myImage1;
            myImages[1]=myImage2;
            myImages[2]=myImage3;
            myImages[3]=myImage4;

            add(ImageGallery, BorderLayout.NORTH);

            JButton PREVIOUS = new JButton ("Previous");
            JButton PLAY = new JButton ("Play");
            JButton STOP = new JButton ("Stop");
            JButton NEXT = new JButton ("Next");

            JPanel Menu = new JPanel();
            Menu.setLayout(new GridLayout(1,4));
            Menu.add(PREVIOUS);
            Menu.add(PLAY);
            Menu.add(STOP);
            Menu.add(NEXT);

            add(Menu, BorderLayout.SOUTH);

            //register listener
            PreviousButtonListener PreviousButton = new PreviousButtonListener ();
            PlayButtonListener PlayButton = new PlayButtonListener ();
            StopButtonListener StopButton = new StopButtonListener ();
            NextButtonListener NextButton = new NextButtonListener ();

            //add listeners to corresponding componenets 
            PREVIOUS.addActionListener(PreviousButton);
            PLAY.addActionListener(PlayButton);
            STOP.addActionListener(StopButton);
            NEXT.addActionListener(NextButton);

        }

    public static void main (String [] args)
        {
            ImageGallery frame = new ImageGallery();

            frame.setSize(490,430);
            frame.setVisible(true);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLocationRelativeTo(null);
        }



    class PreviousButtonListener implements ActionListener 
    {

        public void actionPerformed(ActionEvent e)
            {
                if(curImageIndex>0 && curImageIndex <= 3)
                    {   ImageGallery.remove(0);
                        curImageIndex=curImageIndex-1;
                        ImageIcon TheImage= myImages[curImageIndex];
                        ImageGallery.add(new JLabel (TheImage));
                        ImageGallery.validate();
                        ImageGallery.repaint(); 
                    }
                else 
                    {   
                        ImageGallery.remove(0);
                        ImageGallery.add(new JLabel (myImage1));
                        curImageIndex=0;
                        ImageGallery.validate();
                        ImageGallery.repaint();
                    }
            }
    }

    class PlayButtonListener implements ActionListener 
    {
        public void actionPerformed(ActionEvent e)
            {
                        // *need help here*//

            }
    }

    class StopButtonListener implements ActionListener 
    {
        public void actionPerformed(ActionEvent e)
            {
                        // *need help here*//
            }
    }

    class NextButtonListener implements ActionListener 
    {


        public void actionPerformed(ActionEvent e)
        {

            if(curImageIndex>=0 && curImageIndex < 3)
                {   ImageGallery.remove(0);
                    curImageIndex = curImageIndex + 1;
                    ImageIcon TheImage= myImages[curImageIndex];
                    ImageGallery.add(new JLabel (TheImage));
                    ImageGallery.validate();
                    ImageGallery.repaint(); 
                }
            else 
                {   
                    ImageGallery.remove(0);
                    ImageGallery.add(new JLabel (myImage4));
                    curImageIndex=3;
                    ImageGallery.validate();
                    ImageGallery.repaint();
                }

        }
    }
}
BenMorel
  • 34,448
  • 50
  • 182
  • 322
ThisBetterWork
  • 505
  • 3
  • 12
  • 24
  • 1
    Never use `java.util.Timer` for Swing, avoid it as much as you can, since updates for Swing done through that, will become one big headache for you, since you have to do those updates on EDT - Event Dispatcher Thread. Try looking at [javax.swing.Timer](http://docs.oracle.com/javase/7/docs/api/javax/swing/Timer.html), Here is a small program for your help [SlideShow](http://stackoverflow.com/a/9631116/1057230) – nIcE cOw Apr 23 '12 at 05:37

4 Answers4

5

Why complicating simple things,

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • Not sure whether a `CardLayout` is the best option here. This would mean you have all images in memory and depending on the size/number of images you want to view might consume a lot of memory. In this case I would opt for a `BorderLayout` and always replace the image – Robin Apr 23 '12 at 06:08
  • @Robin there are 1) memory for already loaded image(s) 2) Graphics2D memory 3) something that I missed, conclusion for 2nd. point AFAIK only currently visible whatever rendering Graphics2D, – mKorbel Apr 23 '12 at 06:43
  • Agreed that in his SSCCE he already allocated memory for the images. I was more talking in general for an image viewer to loop through a whole set of images a CardLayout is not the best option IMO – Robin Apr 23 '12 at 07:57
  • hmmm well bussines concept, issues, then post here an answer about SwingWorker and java.util.Queue for Icons, – mKorbel Apr 23 '12 at 08:20
3

This example shows a start/stop button that controls a javax.swing.Timer. Instead of replacing the label each time, just update the label's Icon, as suggested by @mKorbel and shown here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
1

You need use a thread for the slideshow. You can use a flag in the run method for continue with the show or stop if this flag change, for example, a boolean var. One example you can see in http://java.sun.com/developer/technicalArticles/Threads/applet/.

Paul Vargas
  • 41,222
  • 15
  • 102
  • 148
0

These are some guidelines that might get you started:

First you will need a separate thread to control the changing images. I suggest you write a class that implements TimerTask. Override the run() method in this class. In this run method you should put the functionality to change the current image being displayed (similar to what you did in the next and previous function).

In the actionPerformed() method for the play button you will need to create an instance of a Timer class and start your timer using the scheduleAtFixedRate(TimerTask task, long delay, long period) method (other methods in this class may be used as well, scheduleAtFixedRate() seem more appropriate though).

For the stop you will then need to add enough functionality to stop the running timer using the cancel() method in the Timer class

Brendan Cutajar
  • 697
  • 6
  • 14
  • 5
    For Swing related operations, you are better of using the Swing timer. Since all Swing related actions should happen on the EDT, and the Swing timer takes care of that for you – Robin Apr 23 '12 at 06:06