/*
 * PictureViewer.java
 *
 * Copyright 2013 Ajay Bhatia <ajay@dumb-box>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 *
 *
 */

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JFileChooser;
import javax.swing.ImageIcon;
import javax.swing.Timer;
import java.awt.FlowLayout;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.io.File;

public class PictureViewer extends JFrame implements ActionListener {
    private JLabel lblImage;
    private JButton btnPrev;
    private JButton btnNext;
    private JButton btnBrowse;
    private JButton btnSlideShow;
    private JPanel pnlImage;
    private JPanel pnlBottom;
    private JSpinner spnTime;
    private ArrayList<File> imagePath;
    private Timer timer;
    private static int pos;
  
    PictureViewer() {
        btnSlideShow = new JButton("Start Show");
        btnBrowse = new JButton("Browse...");
        btnNext = new JButton("Next");
        btnPrev = new JButton("Prev");
        pnlBottom = new JPanel();
        pnlImage = new JPanel();                      
        lblImage = new JLabel();
        spnTime = new JSpinner();

        pnlBottom.setLayout(new FlowLayout());
        pnlBottom.add(btnSlideShow);
        pnlBottom.add(spnTime);
        pnlBottom.add(btnBrowse);
        pnlBottom.add(btnPrev);
        pnlBottom.add(btnNext);

        btnSlideShow.setEnabled(false);
        btnPrev.setEnabled(false);
        btnNext.setEnabled(false);

        pnlImage.setLayout(new FlowLayout());
        pnlImage.add(lblImage);

        setLayout(new BorderLayout());
      
        add(pnlImage, BorderLayout.CENTER);
        add(pnlBottom, BorderLayout.SOUTH);
  
        btnBrowse.addActionListener(this);
        btnNext.addActionListener(this);
        btnPrev.addActionListener(this);
        btnSlideShow.addActionListener(this);
    }
  
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == btnNext) {
            pos++;
            if (pos > imagePath.size() - 1) pos = 0;
            lblImage.setIcon(new ImageIcon(imagePath.get(pos).getAbsolutePath()));
        } else if (e.getSource() == btnPrev) {
            pos--;
            if (pos == 0) pos = imagePath.size();
            lblImage.setIcon(new ImageIcon(imagePath.get(pos).getAbsolutePath()));
        } else if (e.getSource() == btnBrowse) {
            JFileChooser fileChooser = new JFileChooser();
            fileChooser.setMultiSelectionEnabled(true);
          
            int choice = fileChooser.showOpenDialog(null);
          
            if (choice == JFileChooser.APPROVE_OPTION) {
                File[] temp = fileChooser.getSelectedFiles();
                              
                imagePath = new ArrayList<File>();
              
                for (int i = 0; i < temp.length; i++)
                    imagePath.add(temp[i]);
                  
                btnSlideShow.setEnabled(true);
                btnPrev.setEnabled(true);
                btnNext.setEnabled(true);
            }
          
            pos = 0;
            lblImage.setIcon(new ImageIcon(imagePath.get(pos).getAbsolutePath()));
        } else {
            if (btnSlideShow.getText().equalsIgnoreCase("start show")) {
                timer = new Timer(Integer.parseInt(spnTime.getValue().toString()) * 1000, new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        pos++;
                        if (pos > imagePath.size() - 1) pos = 0;
                        lblImage.setIcon(new ImageIcon(imagePath.get(pos).getAbsolutePath()));
                    }
                });
              
                timer.start();
                btnSlideShow.setText("Stop Show");
                btnPrev.setEnabled(false);
                btnNext.setEnabled(false);
            } else {
                timer.stop();
                btnSlideShow.setText("Start Show");
                btnPrev.setEnabled(true);
                btnNext.setEnabled(true);
            }
        }
    }
      
    public static void main (String args[]) {
        PictureViewer frame = new PictureViewer();
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.setExtendedState(MAXIMIZED_BOTH);
        frame.setVisible(true);  
    }
  
}

Comments

Popular posts from this blog

Zeller's Congruence

Property Event-Delegation

Method with variable arguments