Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make an image gallery with java

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();
                }

        }
    }
}
like image 354
ThisBetterWork Avatar asked Apr 23 '12 04:04

ThisBetterWork


People also ask

Can you display images in Java?

Images can be from a static source, such as a JPEG file, or a dynamic one, such as a video stream or a graphics engine. AWT Images are created with the getImage() and createImage() methods of the java. awt. Toolkit class.

What is an image gallery in HTML?

Image Gallery is used to store and display collection of pictures. This example create a responsive Image Gallery using HTML and CSS. Steps 1: Creating a basic gallery structure. Each gallery contains number of div section. Each div section contains an image and its description.


1 Answers

Why complicating simple things,

  • I think that this is job for CardLayout and for slideshow is there Swing Timer

  • put images as Icon to the JLabel

like image 135
mKorbel Avatar answered Oct 07 '22 20:10

mKorbel