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;
}
}
}

6 comments:

  1. --------------
    http://bantalsilikon01.blogspot.com/2014/10/rekomendasi-untuk-cloud-dedicated.html
    ---------------
    http://kopiluwakliar01.blogspot.com/2014/07/kopi-luwak-liar-arabica-dari-dataran.html
    http://vvty.in/uncategorized/bantal-silikon-185-000-free-ongkir-seluruh-nusantara/
    http://marinirseo.blogspot.com/2014/10/perumahan-baru-di-tangerang-graha-raya.html
    http://bantalsilikon01.blogspot.com/2014/10/perumahan-baru-di-tangerang-graha-raya.html
    http://bantalsilikon01.blogspot.com/2014/11/bantal-silikon.html
    http://bantalsilikon01.blogspot.com/
    http://marinir-seo.blogspot.com/2014/10/marinir-seo.html
    http://kopiluwakliar01.blogspot.com/2014/10/perumahan-baru-di-tangerang-graha-raya.html

    lkmnprapto@gmail.com
    http://vvty.in/http://vvty.in/http://vvty.in/
    http://vvty.in/http://vvty.in/http://vvty.in/
    http://marinir-seo-konsultan-12a.blogspot.com/2014/10/aaa.html
    http://asdf-14.blogspot.com/2014/10/hhh.html
    http://vvtiin.blogspot.com/2014/10/iii.html
    http://marinir-seo-8.blogspot.com/2014/10/jjj.html
    http://bantaljkt1.blogspot.com/2014/10/sss.html
    http://dfes12.blogspot.com/2014/10/rrr.html
    http://seo-guru01.blogspot.com/2014/10/qqq.html
    http://jksbindo.blogspot.com/2014/10/ppp.html
    http://jkt-49.blogspot.com/2014/10/ooo.html
    http://seokonsultan1.blogspot.com/2014/10/nnn.html
    http://guru-seo-malang.blogspot.com/2014/10/mmm.html
    http://seo-aaa.blogspot.com/2014/10/lll.html
    http://seo-konsultasi-mudah.blogspot.com/2014/10/kkk.html
    http://bantal-silikon15.blogspot.com/2014/10/ggg.html
    http://agen-seo16.blogspot.com/2014/10/fff.html
    http://agen-seo17.blogspot.com/2014/10/eee_30.html
    http://agen-seo18.blogspot.com/2014/10/ddd.html
    http://agen-seo19.blogspot.com/2014/10/ccc.html
    http://agen-seo20.blogspot.com/2014/10/bbb.html
    http://bumbu-pecel01.blogspot.com/2014/10/xxx.html

    bumbupecel1.blogspot.com
    http://bantalasdf.blogspot.com/2014/10/aaa.html
    http://seo-malang-19.blogspot.com/2014/10/bbb.html
    http://agenseomalang18.blogspot.com/2014/10/ccc.html
    http://marinirseo17.blogspot.com/2014/10/ddd.html
    http://bumbupecel16.blogspot.com/2014/10/eee.html
    http://domainhosting15.blogspot.com/2014/10/fff.html
    http://bumbupecel15.blogspot.com/2014/10/ggg.html
    http://marinirseo14.blogspot.com/2014/10/hhh.html
    http://bantalsilikon12a.blogspot.com/2014/10/iii.html
    http://marinirseo12.blogspot.com/2014/10/jjj.html
    http://bumbu-pecel-hz.blogspot.com/2014/10/kkk.html
    http://marinir-seo-001.blogspot.com/2014/10/lll.html
    http://bantal-silikon-murah.blogspot.com/2014/10/mmm.html
    http://vvty-ine.blogspot.com/2014/10/nnn.html
    http://give-a-power.blogspot.com/2014/10/ooo.html
    http://bumbupecel-12.blogspot.com/2014/10/ppp.html
    http://marinir-seo-konsultan.blogspot.com/2014/10/qqq.html
    http://guru-seo-praktisi-123.blogspot.com/2014/10/rrr.html
    http://guru-seminar-online01.blogspot.com/2014/10/sss.html
    http://kopiluwakmalang11.blogspot.com/2014/10/ttt.html

    ReplyDelete
  2. 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
  3. We are really grateful for your blog post. You will find a lot of approaches after visiting your post. Great work.
    hotmail login | red ball

    ReplyDelete
  4. It is nice article to improve my knowledge.thank you for sharing useful post
    visit
    web programming tutorial
    welookups

    ReplyDelete
  5. dewacintaqq
    Bandar poker terpercaya
    Poker Indonesia
    Poker Online Indonesia
    Agen Ceme Online
    Daftar Situs Poker
    Poker Online Terpercaya
    Judi Poker
    Poker Online Uang Asli
    Poker Uang Asli
    Situs Poker Online
    Agen Poker
    Situs Judi Online Terpercaya
    bandar poker
    situs judi
    poker online
    ceme online
    bandar ceme
    situs poker
    bandar judi
    poker idn
    judi poker
    judi online
    agen judi
    bandar judi
    poker idn
    agen poker
    idn poker
    Situs Judi Poker Online
    DominoQQ IDN POKER
    Agen judi kartu deposit uang asli pkv
    games
    poker online
    dominoqq
    bandarqq
    bandar poker
    bandarq
    sakong
    aduq
    capsa susun
    Perang bacarat
    -------------------------------------------------------------------------------------------
    Daftar Dominohalo
    Bandar poker terpercaya
    judi poker online
    bandar poker online
    judi online
    poker online
    dominoqq
    situs judi online
    situs poker online
    -----------------------------------------------------------------------------------------
    Daftar twinpoker88
    Bandar poker terpercaya
    judi online
    situs judi online
    poker online
    dominoqq
    bandarqq
    bandar poker
    bandarq
    sakong
    aduq
    capsa susun
    -----------------------------------------------------------------------------------
    Daftar Delimapoker
    Bandarq online terpercaya
    Delimapoker situs resmi daftar judi online bandar poker
    bandar66
    sakong
    capsa
    domino99
    aduq uang asli qq online domino QQ
    bandarq
    judi online
    bandar poker
    qq online
    ----------------------------------------------------------------------------------
    Daftar Papadomino
    Bandar poker terpercaya
    situs judi online
    judi online
    situs poker online
    poker online
    dominoqq
    domino99
    bandarq

    ReplyDelete