ImageSlideshow source code:
import
java.awt.*;
import
java.awt.event.*;
import
javax.swing.*;
import
javax.swing.event.*;
import
java.awt.image.*;
import
javax.swing.filechooser.*;
import
java.util.HashMap;
import
java.io.*;
class
ImageShow extends Canvas implements ImageObserver{
Image
img;
Dimension
ds;
HashMap<Integer, String>
imgFiles;
String
filename;
int curImgIndex;
ImageShow(){
1 ds=getToolkit().getScreenSize();
2 filename=null;
}
3 public void paint(Graphics g){
if(filename!=null){
img=getToolkit().getImage(filename);
drawIt(g,img);
}
}
4 public void drawIt(Graphics g, Image
img){
Graphics2D
g2d=(Graphics2D)g;
int
x=-img.getWidth(this)/2;
int
y=-img.getHeight(this)/2;
int w=img.getWidth(this);
int
h=img.getHeight(this);
int
mX=(int)ds.getWidth()/2;
int
mY=(int)ds.getHeight()/2;
g2d.translate(mX,mY);
g2d.drawImage(img,x,y,w,h,this);
}
5 public void storeImages(){
imgFiles=new
HashMap<Integer, String>();
JFileChooser chooser =
new JFileChooser();
FileNameExtensionFilter
filter = new FileNameExtensionFilter("Image Files", "jpg",
"gif","png","gif","bmp");
chooser.setFileFilter(filter);
chooser.setMultiSelectionEnabled(true);
int
returnVal = chooser.showOpenDialog(this);
if(returnVal
== JFileChooser.APPROVE_OPTION) {
File[]
Files=chooser.getSelectedFiles();
for(
int i=0;i<Files.length;i++){
imgFiles.put(i,Files[i].toString());
}
}
}
6 public void moveFirst(){
if(!imgFiles.isEmpty()){
curImgIndex=0;
filename=imgFiles.get(curImgIndex);
repaint();
}
}
7 public void moveNext(){
if(!imgFiles.isEmpty()
&& curImgIndex<imgFiles.size()-1){
curImgIndex++;
filename=imgFiles.get(curImgIndex);
repaint();
}
}
8 public HashMap<Integer,String>
getImgFiles(){
return imgFiles;
}
}
9 class ShowInterface extends JFrame implements
ActionListener{
JButton btSlideShow;
JPanel panel;
ImageShow show;
boolean stop=true;
ImageSlider slider;
ShowInterface(){
setTitle("Image
Slide Show");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setExtendedState(this.getExtendedState()
| this.MAXIMIZED_BOTH);
JMenuBar mainmenu=new
JMenuBar();
JMenu menu=new
JMenu("File");
JMenuItem mopen=new
JMenuItem("Open...");
mopen.setMnemonic(KeyEvent.VK_O);
mopen.addActionListener(this);
JMenuItem mexit=new
JMenuItem("Exit");
mexit.setMnemonic(KeyEvent.VK_X);
mexit.addActionListener(this);
menu.add(mopen);
menu.add(mexit);
mainmenu.add(menu);
setJMenuBar(mainmenu);
btSlideShow=new
JButton(new ImageIcon("play.png"));
btSlideShow.setBackground(Color.BLACK);
btSlideShow.addActionListener(this);
btSlideShow.setEnabled(false);
panel=new JPanel();
panel.setLayout(new
FlowLayout());
panel.add(btSlideShow);
panel.setBackground(Color.BLACK);
add(panel,
BorderLayout.SOUTH);
show=new ImageShow();
add(show,BorderLayout.CENTER
);
setVisible(true);
}
10 public void actionPerformed(ActionEvent
e){
if(e.getSource()==btSlideShow)
{
if(stop){
startClick();
}
else{
stopClick();
}
}
else {
JMenuItem
source = (JMenuItem)(e.getSource());
if(source.getText().compareTo("Open...")==0)
{
show.storeImages();
if(show.getImgFiles().size()>0){
show.moveFirst();
btSlideShow.setEnabled(true);
}
}
else
if(source.getText().compareTo("Exit")==0)
System.exit(0);
}
}
11 public void startClick(){
btSlideShow.setIcon(new
ImageIcon("pause.png"));
stop=false;
slider=new
ImageSlider();
slider.start();
}
12 public void stopClick(){
btSlideShow.setIcon(new
ImageIcon("play.png"));
stop=true;
slider.stopShow();
}
13 class ImageSlider extends Thread{
boolean started;
HashMap<Integer,String>
map;
ImageSlider(){
started=true;
map=show.getImgFiles();
}
public void run(){
int i;
try{
for(i=0;i<map.size();i++)
{
if(started!=false){
Thread.sleep(2000);
show.moveNext();
}
}
stopClick();
show.moveFirst();
}catch(InterruptedException
ie){System.out.println("Interrupted slide show...");}
}
public void stopShow(){
started=false;
}
}
}
15 public
class ImageSlideShow{
public static void main(String
args[]){
new ShowInterface();
}
}
Code Explanation:
1 Get the dimension (width and height) of the computer screen.
2 Initialize the filename variable to empty string. The filename variable stores a image file path. When the program first loads, there is no image to display on the screen so the filename should be empty.
3 The paint method of the Canvas class is where the drawing begins. It invokes the drawIt method to display the image at the center of the screen when the program opens.
4 The drawIt method is implemented to display the image. The translate(x,y) method of the Graphics2D class is used to move the original coordinate to the (x,y) coordinate or position. We use the drawImage method to display the image at the (x,y) position.
5 The storeImages method is invoked when the user clicks the Open sub menu item from the File menu. This method allows the user to selection image files and store them in the HashMap object called imgFiles. We use the put method to add each file to this object.
6 The moveFirst method moves to the first image in the imgFiles object. The repaint method of the Canvas class is invoked to repaint the component so the image is displayed.
7 The moveFirst method moves to the next image in the imgFiles object.
8 The getImageFiles method return the images in the imgFiles object.
9 The showInterface class has code to show the interface of the Image Slideshow program. In this program,
we need one menu bar. In the menu bar we place the File menu. The File menu has two sub menu items: mopen and mexit. Each sub menu item is registered with the action event. To support the shortcut key pressed, you need to use the setMnemonic method. The sub meu item is added to the File menu by using the add method. You will use the setJMenubar method to add the menu bar
to the program window(JFrame) We also need one button to let the user play or stop the slideshow.
10 The actionPerformed method of the ActionListener interface is rewritten to enable the actions of the play button and the menu selection.
11 The startClick method has code to start the slideshow by invoking the start method the ImageSlider class. The icon of the button is changed from play to stop icon at this time. If the user does not interrupt the slideshow, it plays all images in the imgFiles.
12 The stopClick method has code to stop the slideshow by invoking the stopShow method the ImageSlider class. The icon of the button is changed from stop to play icon at this time.
13 The ImageSlider class extends the Thread class. The imageSlider has two methods: run and stopShow.
When you invoke the start method of ImageSlider by clicking the play button (inherited from the Thread) the run method automatically invoked to start playing the slideshow. The started variable has true value at this time. We use the sleep method of the Thread class to create time interval between each image show. This method accept the time value in milliseconds.
Image slideshow is a must thing these days as it can help us to stay focus on custom essays reviews. If they know what they are doing about it, then it's going to be kinda hard thing for all of us.
ReplyDeleteThe Impossible Game is an entertainment website suitable for all ages. Here, in addition to your entertainment, you can also find many other games that serve the purpose of training some skills for children such as Sudoku, Impossible Game, Tetris... Wish you have a good time. relaxing rewarding and fun!
ReplyDeleteDazzling! Incomprehensible article man! Thankful to you, However I am encountering issues with your RSS. I don't appreciate the inspiration driving why I can't oblige it. Is there some other individual having an identical RSS issues? Any individual who enjoys the fitting reaction will you liberally respond? Much appreciated!!tamilprintmob
ReplyDelete