Hi,
/*
* Program for implementing a Slide show in java
* Copyright 2009 @ yuvadeveloper
* Code By:- Prashant Chandrakar
*/
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.swing.*;
import java.awt.event.*;
class JSlideShow extends JFrame implements ActionListener
{
JLabel image;
File [] files;
int imageNo;
public JSlideShow()
{
super("SliderDemo by prashant chandrakar");
///for setting folder path containing images
setImageArray();
if(files.length > 0)
image = new JLabel(new ImageIcon(files[0].toString()));
////buttons for navigating images
JButton pre = new JButton("Prev");
pre.setActionCommand("pre");
pre.addActionListener(this);
JButton next = new JButton("Next");
next.setActionCommand("next");
next.addActionListener(this);
JPanel jp = new JPanel();
jp.add(pre);
jp.add(next);
this.setLayout(new BorderLayout());
this.getContentPane().add("North", image);
this.getContentPane().add("Center", jp);
this.setSize(1024, 768);
this.setVisible(true);
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowListener we)
{
System.exit(0);
}
});
}
public void setImageArray()
{
////just change the folder path containing images
File path = new File("d:/nondicom/images/");
files = path.listFiles();
}
public void setImage(int number)
{
image.setIcon(new ImageIcon(files[number].toString()));
}
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand().equals("pre"))
{
if (imageNo == 0)
{
imageNo = files.length-1;
}
else
{
imageNo--;
}
setImage(imageNo);
}
else if (e.getActionCommand().equals("next"))
{
if (imageNo == files.length-1)
{
imageNo = 0;
}
else
{
imageNo++;
}
setImage(imageNo);
}
}
public static void main(String a[])
{
new JSlideShow();
}
}
No comments:
Post a Comment