Sunday, June 9, 2013

Photo Viewer

The PhotoViewer program is able to do the following tasks with your photos:
-rotate the photo clockwise and counterclockwise directions,
-zoom the photo in and out,
-move the photo forward and backward, and
-display the slideshow of photos.

Photo Viewer in Java


PhotoViewer source code:

import java.awt.*;
import java.awt.event.*;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.image.*;

import javax.swing.filechooser.*;
import java.util.HashMap;
import java.io.*;


public class PhotoViewer {
public static void main(String args[]){
    new Main();
  
}


}

class ImageDraw extends Canvas{
    BufferedImage img;
    double radian=0.0; 
Dimension ds;
int iw;
    int ih;
int x;
int y;
int adjX;
int adjY;
int adjW;
int adjH;
int mX;
int mY;
int orWidth;
int orHeight;
boolean rotate;
HashMap<Integer, String> imgFiles;
  String filename;
int curImgIndex;
ImageDraw(){
ds=getToolkit().getScreenSize(); //get the screen size
mX=(int)ds.getWidth()/2; //half of the screen width
mY=(int)ds.getHeight()/2;//half of the screen height
filename=null; //initialize filename variable
rotate=false; //initialize rotate variable
//initialize variables used to increase or decrease photo size
adjX=0;
adjY=0;
adjW=0;
adjH=0;
}
public void paint(Graphics g){
if(filename!=null){
if(rotate) //show rotated photo
rotateImage(g);
else
showImage(g); //show normal and zoom photo
}
   

//The rotateImage rotates the photo in clockwise or counterclockwise
//direction based on the radian value
public void rotateImage(Graphics g){
Graphics2D g2d=(Graphics2D)g;
g2d.translate(mX,mY); //move the original coordinate to point(mX,mY)
g2d.rotate(radian); //rotate the photo
g2d.translate(-iw/2,-ih/2); //move the coordinate back to point (iw/2,ih/2)
g2d.drawImage(img,0,0,iw,ih,null); //show the rotate photo
g2d.dispose(); //clean the g2d object
rotate=false; //reset the rotate to false after the photo rotates
}
//Increase the radian value by Math.PI/2
//so the photo is rotated in clockwise direction
public void rotateClockwise(){
radian+=Math.PI/2;
}
//Decrease the radian value by Math.PI/2
//so the photo is rotated in countclockwise direction
public void rotateCounterClockwise(){
radian-=Math.PI/2;
}
//The showImage method has code to display the photo when it is in
//normal view or zoom view
public void showImage(Graphics g){
Graphics2D g2d=(Graphics2D)g;
g2d.translate(x,y);
g2d.drawImage(img,0,0,iw,ih,null);
g2d.dispose();
}
public void readImage(String filename){
try{
//read the photo file so it is ready to display
img=ImageIO.read(new File(filename));
//get the original width and height of the photo
orWidth=img.getWidth();
orHeight=img.getHeight();
//Make sure the photo is not too large to fit the screen
if(mX<orWidth/2)
orWidth=mX*2;
if(mY<orHeight/2)
orHeight=mY*2;
setImageBounds();
}catch(IOException e){};
}
//Determine the appropriate the photo size and location to place the photo
public void setImageBounds(){
try{
//location to place the photo
x=mX-orWidth/2+adjX;
y=mY-orHeight/2+adjY;
//photo size
iw=orWidth+adjW;
ih=orHeight+adjH;
}catch(NullPointerException npe){System.exit(-1);}
}
//Zoom the photo in
//decrease x-axis and y-axis values and at the same time
//increase the photo width and height
public void zoomIn(){
adjX-=10;
adjY-=10;
adjW+=20;
adjH+=20;
setImageBounds();
}
//Zoom the photo out
//increase x-axis and y-axis values and at the same time
//decrease the photo width and height
public void zoomOut(){
if(iw>40 && ih>40){
adjX+=10;
adjY+=10;
adjW-=20;
adjH-=20;
setImageBounds();
}

}
//The storeImages method is invoked when the user select the Open... sub-menu
//item from the program interface
//It displays a file chooser dialog for multiple files selection
//The selected photos file are stored in a HashMap object imgList for 
//later show
public void storeImages(){
imgFiles=new HashMap<Integer, String>();
JFileChooser chooser = new JFileChooser();
    FileNameExtensionFilter filter = new FileNameExtensionFilter("Images", "jpg", "gif","png","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());
            }
        }
}
//The moveFirst method has code to move to the first photo in the ImgList
public void moveFirst(){
if(!imgFiles.isEmpty()){
curImgIndex=0;
filename=imgFiles.get(curImgIndex);
readImage(filename);
repaint();
}
}
//The moveLast method has code to move to the last photo in the ImgList
public void moveLast(){
if(!imgFiles.isEmpty()){
curImgIndex=imgFiles.size()-1;
filename=imgFiles.get(curImgIndex);
readImage(filename);
repaint();
}
}
//The movePrevious method has code to move photo back in the ImgList
public void movePrevious(){
if(!imgFiles.isEmpty() && curImgIndex!=0){
curImgIndex--;
filename=imgFiles.get(curImgIndex);
readImage(filename);
repaint();
}
}
//The moveNext method has code to move photo forward in the ImgList
public void moveNext(){
if(!imgFiles.isEmpty() && curImgIndex<imgFiles.size()-1){
curImgIndex++;
filename=imgFiles.get(curImgIndex);
readImage(filename);
repaint();
}
}

//The getImgFiles method allows access to the list of the photos--ImgList
public HashMap<Integer,String> getImgFiles(){
return imgFiles;
}
}

//The Main class represents the interface of the PhotoView program
//On the interface, you see a File menu that has two sub-menu items: Open..., and Exit
//When the photo is displayed, the program will display a panel that contains 
//all buttons you need to rotate, zoom, move, and play the slideshow of the photos
class  Main extends JFrame implements ActionListener{
JButton btrotateClockwise;
JButton btrotateCounterClockwise;
JButton btZoomIn;
JButton btPrevious;
JButton btNext;
JButton btZoomOut;
JButton btSlideShow;
JPanel panel;
ImageDraw imgDraw;
boolean stop=true;
ImageSlide Slider;
Main(){
setTitle("Photo Viewer");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setExtendedState(this.getExtendedState() | JFrame.MAXIMIZED_BOTH);
setBackground(Color.CYAN);

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);
btrotateClockwise=new JButton(new ImageIcon("dir1.png"));
btrotateClockwise.setBackground(Color.BLACK);
btrotateClockwise.addActionListener(this);

btrotateCounterClockwise=new JButton(new ImageIcon("dir2.png"));
btrotateCounterClockwise.setBackground(Color.BLACK);
btrotateCounterClockwise.addActionListener(this);

btZoomIn=new JButton(new ImageIcon("inc.png"));
btZoomIn.setBackground(Color.BLACK);
btZoomIn.addActionListener(this);

btZoomOut=new JButton(new ImageIcon("dec.png"));
btZoomOut.setBackground(Color.BLACK);
btZoomOut.addActionListener(this);
btPrevious=new JButton(new ImageIcon("previous.png"));
btPrevious.setBackground(Color.BLACK);
btPrevious.addActionListener(this);

btNext=new JButton(new ImageIcon("next.png"));
btNext.setBackground(Color.BLACK);
btNext.addActionListener(this);

btSlideShow=new JButton(new ImageIcon("play.png"));
btSlideShow.setBackground(Color.BLACK);
btSlideShow.addActionListener(this);

panel=new JPanel();
panel.setLayout(new FlowLayout());
panel.add(btrotateClockwise);
panel.add(btrotateCounterClockwise);
panel.add(btZoomIn);
panel.add(btZoomOut);
panel.add(btNext);
panel.add(btPrevious);
panel.add(btSlideShow);
panel.setBackground(Color.BLACK);
add(panel, BorderLayout.SOUTH);
imgDraw=new ImageDraw();
add(imgDraw,BorderLayout.CENTER );
setVisible(true);
panel.setVisible(false);
}
//handling buttons and sub-menu items clicks
public void actionPerformed(ActionEvent e){
if(e.getSource()==btrotateClockwise)
{
imgDraw.rotate=true;
imgDraw.rotateClockwise();
imgDraw.repaint();
}
else if(e.getSource()==btrotateCounterClockwise)
{
imgDraw.rotate=true;
imgDraw.rotateCounterClockwise();
imgDraw.repaint();
}
else if(e.getSource()==btZoomIn)
{
imgDraw.zoomIn();
imgDraw.repaint();
}
else if(e.getSource()==btZoomOut)
{
imgDraw.zoomOut();
imgDraw.repaint();
}
else if(e.getSource()==btPrevious)
{
imgDraw.movePrevious();
}
else if(e.getSource()==btNext)
{
imgDraw.moveNext();
}
else if(e.getSource()==btSlideShow)
{
if(stop){
startClick();
}
else{
stopClick();
}
}
else{

JMenuItem source = (JMenuItem)(e.getSource());
if(source.getText().compareTo("Open...")==0)
{
imgDraw.storeImages();
imgDraw.moveFirst();
panel.setVisible(true);
}
else if(source.getText().compareTo("Exit")==0) 
System.exit(0);
}
}
//The stopClick method invoked when you click the Pause button 
//to stop the slideshow
public void stopClick(){
btSlideShow.setIcon(new ImageIcon("play.png"));
stop=true;
Slider.stopShow();
}
//The startClick method invoked when you click the Play button
//to start the slideshow
public void startClick(){
btSlideShow.setIcon(new ImageIcon("pause.png"));
stop=false;
Slider=new ImageSlide();
Slider.start();
}

//The ImageSlide class handle the photo slideshow process
//When you click the Play button the run method of this class is invoked
//When you click the Pause button the stopShow method of this class is invoked
class ImageSlide extends Thread{
boolean started;
HashMap<Integer,String> map;
ImageSlide(){
started=true;
map=imgDraw.getImgFiles();
}

public void run(){
int i;
try{
for(i=0;i<map.size();i++)
{
if(started!=false){
Thread.sleep(2000); //time interval between each photo show
imgDraw.moveNext(); //show the next photo
}
}
stopClick(); //stop show when all photos are displayed
imgDraw.moveFirst(); //show the first photo again
}catch(InterruptedException ie){System.out.println("Interrupted slide show...");}
}

public void stopShow(){
started=false;
}
}
}

2 comments:

  1. I wanted to thank you for this excellent read!! I definitely loved every little bit of it.Cheers for the info!!!!Photo editing Services | Image editing company

    ReplyDelete