Wednesday 24 February 2016

Image Slideshow in Java

Image slide show in java  can made by several methods. There are many ways to implement these methods. But here i am going to show you how we can make a simple image slide show from images which are present in our computer. First rename the images which will be used in the slide show as a1, a2, a3, a4, a5 and so on. We will also use a jframe as window, timer for introducing delay in slide show and jlabel for showing image. Now see the simple slide show program created by me.

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import javax.swing.*;

public class slide extends JFrame {

    JPanel panel;
    JLabel label;
    ImageIcon img;
    Timer timer;
    int i=0;
    public slide() {
        setLayout(new FlowLayout());

        panel = new JPanel();
        add(panel);
        label = new JLabel();
        timer = new Timer(0, null);
        timer.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if(i>4) {
                    i=0;
                    label.setIcon(new ImageIcon("/home/amar/Desktop/a"+i+".jpg"));
                    i++;
                    panel.add(label);
                }else {

                    label.setIcon(new ImageIcon("/home/amar/Desktop/a"+i+".jpg"));
                    i++;
                    panel.add(label);
                }
            }
        });
        timer.setDelay(1000);
        timer.start();
    }
    public static void main(String[] args) {
   
        slide amar = new slide();
        amar.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        amar.setSize(500,500);
        amar.setVisible(true);
    }

   
}


Run the program and you will see the slide show. Here delay is 1000ms, you can make it as you want.
Good Luck guys. Follow my blog.