Java Program: Photo Viewer
1. Write an app called viewer that will have a Label at the top saying "My Viewer" (or
something like that)
2. Will have JButtons at the bottom that will do Next, Previous, and Quit
3. Have the whole middle be a JLabel in which you will display Images stored in a directory.
4. The directory can be named Resource.
5. When you run the program (java viewer) it will read all the names in the Resource Directory.
Then, when you click Next or Previous it will display an Image.
6. Note: you will need to find a java method that exists for reading a whole directory of
filenames. You can store all those names in a String Array when run the program.
7. You will use a counter or index that is an int an when you click Next it will increment the
counter until it reach some maximum value and then you will set it to 0. Previous will decrement
the counter until it goes negative and then it will set the counter to the Maximum index ( which
is how many filenames you have in the Image names array)
8. Submit the program viewer.java I should be able to use it with my own Resource directory.
Solution
Please refer this code for your application :
--------------------------------------------------------------------------------------------------------------------
--------------
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
public class ImageViewer extends javax.swing.JFrame {
public ImageViewer() {
initComponents();
listFiles(Path); //Lists all the files in the directory on window opening
setLabelIcon(Path,filenames[position]); //sets the label to display the first
//image in the directory on window opening.
PreviousButton.setEnabled(false);
}
/**
*Initialize components
*/
private void initComponents() {
setTitle("Image Viewer");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new java.awt.BorderLayout());// The layout is BorderLayout
//setBorder(javax.swing.BorderFactory.createEtchedBorder());
setBackground(java.awt.Color.GRAY);
picLabel = new javax.swing.JLabel(); //Create the Label to display the picture
picLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
picLabel.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
PreviousButton = new javax.swing.JButton();
PreviousButton.setText("Previous");
PreviousButton.setIconTextGap(10); //Distance between the icon and text is 10
PreviousButton.addActionListener(new java.awt.event.ActionListener() { //Register an
actionListener for the PreviousButton
public void actionPerformed(java.awt.event.ActionEvent evt) {
PreviousButtonActionPerformed(evt);
}
});
NextButton = new javax.swing.JButton();
NextButton.setPreferredSize(PreviousButton.getPreferredSize());
NextButton.setText("Next");
NextButton.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);
NextButton.setIconTextGap(10); //Distance between the icon and text is 10
NextButton.addActionListener(new java.awt.event.ActionListener() { //Registers an
actionListener for the NextButton
public void actionPerformed(java.awt.event.ActionEvent evt) {
NextButtonActionPerformed(evt);
}
});
javax.swing.Box vbox = javax.swing.Box.createVerticalBox(); //VerticalBox to hold the
pictureLabel and the buttons
vbox.add(javax.swing.Box.createVerticalStrut(30));
vbox.add(picLabel);
vbox.add(javax.swing.Box.createVerticalStrut(50));
javax.swing.JPanel prev_next_pane = new javax.swing.JPanel(); //Panel to hold the Previous and
Next buttons.
java.awt.FlowLayout flow = new java.awt.FlowLayout(java.awt.FlowLayout.CENTER);
flow.setHgap(30);
prev_next_pane.setLayout(flow);
prev_next_pane.add(PreviousButton);
prev_next_pane.add(NextButton);
prev_next_pane.setOpaque(false);
vbox.add(prev_next_pane); //Add the panel to the verticalBox
add(vbox);
java.awt.Toolkit theKit = getToolkit();
java.awt.Dimension size = theKit.getScreenSize();
setLocation(size.width/5,size.height/10);
setMinimumSize(new java.awt.Dimension(900,600));
//setMaximumSize(new Dimension(size.width/4 + 50,size.height/4));
}//END:initComponents
/**
*Handler for previous button
*/
private void PreviousButtonActionPerformed(java.awt.event.ActionEvent evt) {
position--; //Decrement the index position of the array of filenames by one on buttonPressed
if(!NextButton.isEnabled()){ //if NextButton is
NextButton.setEnabled(true); //disabled, enable it
}
if(position == 0){ //If we are viewing the first Picture in
PreviousButton.setEnabled(false); //the directory, disable previous button
}
setLabelIcon(Path,filenames[position]);
}//End of PreviousButton handler
/**
*Handler for NextButton
*/
private void NextButtonActionPerformed(java.awt.event.ActionEvent evt) {
position++; //Increment the index position of the array of filenames by one on buttonPressed
if(!PreviousButton.isEnabled()){
listFiles(Path);
PreviousButton.setEnabled(true);
}
if(position == filenames.length){
NextButton.setEnabled(false);
position --;
return;
}
setLabelIcon(Path,filenames[position]);
}//End of NextButton handler
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info :
javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(ImageViewer.class.getName()).log(java.util.logging.Level.S
EVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(ImageViewer.class.getName()).log(java.util.logging.Level.S
EVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(ImageViewer.class.getName()).log(java.util.logging.Level.S
EVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(ImageViewer.class.getName()).log(java.util.logging.Level.S
EVERE, null, ex);
}
/*
* Create and display the form
*/
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ImageViewer().setVisible(true);
}
});
}
/**Method to list all the files in the directory
*And store their names in an array
*/
private void listFiles(String Path){
File file = new File(Path);
filenames = file.list(); //store the file names in the array of strings.
for(String names : filenames){
System.out.println(names); //Print the filenames to the console just so you can see
}
}
//Method to set the picture on the label
private void setLabelIcon(String Path,String name){
File file = new File(Path+""+name);
java.awt.image.BufferedImage image = null;
try{
image = ImageIO.read(file); //Reas the image from the file.
}catch(IOException ie){
javax.swing.JOptionPane.showMessageDialog(this,"Error reading image file","Error",
javax.swing.JOptionPane.ERROR_MESSAGE);
}
ImageIcon icon = new ImageIcon(image);
int width = 600;
int height = 400;
Image img =
icon.getImage().getScaledInstance(width,height,java.awt.Image.SCALE_SMOOTH); //Images
produced will remain a fixed size, 600 * 400
ImageIcon newIcon = new ImageIcon(img); //Create a new imageicon from an image object.
//Now we want to create a caption for the pictures using their file names
String pictureName = file.getName();
int pos = pictureName.lastIndexOf("."); //This removes the extensions
String caption = pictureName.substring(0,pos); //from the file names. e.g .gif, .jpg, .png
picLabel.setIcon(newIcon); //Set the imageIcon on the Label
picLabel.setText(caption); //Set the caption
picLabel.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM); //Caption appears
below the image
}
//Variables declaration
int position = 0; //Initial position is 0
String[] filenames; //Array to hold the file names
final String Path = System.getProperty("user.home") +"User_Namepictures"; //path
to your images
private javax.swing.JButton NextButton;
private javax.swing.JButton PreviousButton;
private javax.swing.JLabel picLabel;
// End of variables declaration//GEN-END:variables
}//End of class
--------------------------------------------------------------------------------------------------------------------
--------------
feel free to reach out to me if you have any doubts in understanding this code. Keep learning :)

Java Program Photo Viewer1. Write an app called viewer that will .pdf

  • 1.
    Java Program: PhotoViewer 1. Write an app called viewer that will have a Label at the top saying "My Viewer" (or something like that) 2. Will have JButtons at the bottom that will do Next, Previous, and Quit 3. Have the whole middle be a JLabel in which you will display Images stored in a directory. 4. The directory can be named Resource. 5. When you run the program (java viewer) it will read all the names in the Resource Directory. Then, when you click Next or Previous it will display an Image. 6. Note: you will need to find a java method that exists for reading a whole directory of filenames. You can store all those names in a String Array when run the program. 7. You will use a counter or index that is an int an when you click Next it will increment the counter until it reach some maximum value and then you will set it to 0. Previous will decrement the counter until it goes negative and then it will set the counter to the Maximum index ( which is how many filenames you have in the Image names array) 8. Submit the program viewer.java I should be able to use it with my own Resource directory. Solution Please refer this code for your application : -------------------------------------------------------------------------------------------------------------------- -------------- import java.awt.Image; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.ImageIcon; public class ImageViewer extends javax.swing.JFrame { public ImageViewer() { initComponents(); listFiles(Path); //Lists all the files in the directory on window opening setLabelIcon(Path,filenames[position]); //sets the label to display the first //image in the directory on window opening. PreviousButton.setEnabled(false);
  • 2.
    } /** *Initialize components */ private voidinitComponents() { setTitle("Image Viewer"); setDefaultCloseOperation(EXIT_ON_CLOSE); setLayout(new java.awt.BorderLayout());// The layout is BorderLayout //setBorder(javax.swing.BorderFactory.createEtchedBorder()); setBackground(java.awt.Color.GRAY); picLabel = new javax.swing.JLabel(); //Create the Label to display the picture picLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); picLabel.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); PreviousButton = new javax.swing.JButton(); PreviousButton.setText("Previous"); PreviousButton.setIconTextGap(10); //Distance between the icon and text is 10 PreviousButton.addActionListener(new java.awt.event.ActionListener() { //Register an actionListener for the PreviousButton public void actionPerformed(java.awt.event.ActionEvent evt) { PreviousButtonActionPerformed(evt); } }); NextButton = new javax.swing.JButton(); NextButton.setPreferredSize(PreviousButton.getPreferredSize()); NextButton.setText("Next"); NextButton.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT); NextButton.setIconTextGap(10); //Distance between the icon and text is 10 NextButton.addActionListener(new java.awt.event.ActionListener() { //Registers an actionListener for the NextButton public void actionPerformed(java.awt.event.ActionEvent evt) { NextButtonActionPerformed(evt); } }); javax.swing.Box vbox = javax.swing.Box.createVerticalBox(); //VerticalBox to hold the pictureLabel and the buttons vbox.add(javax.swing.Box.createVerticalStrut(30));
  • 3.
    vbox.add(picLabel); vbox.add(javax.swing.Box.createVerticalStrut(50)); javax.swing.JPanel prev_next_pane =new javax.swing.JPanel(); //Panel to hold the Previous and Next buttons. java.awt.FlowLayout flow = new java.awt.FlowLayout(java.awt.FlowLayout.CENTER); flow.setHgap(30); prev_next_pane.setLayout(flow); prev_next_pane.add(PreviousButton); prev_next_pane.add(NextButton); prev_next_pane.setOpaque(false); vbox.add(prev_next_pane); //Add the panel to the verticalBox add(vbox); java.awt.Toolkit theKit = getToolkit(); java.awt.Dimension size = theKit.getScreenSize(); setLocation(size.width/5,size.height/10); setMinimumSize(new java.awt.Dimension(900,600)); //setMaximumSize(new Dimension(size.width/4 + 50,size.height/4)); }//END:initComponents /** *Handler for previous button */ private void PreviousButtonActionPerformed(java.awt.event.ActionEvent evt) { position--; //Decrement the index position of the array of filenames by one on buttonPressed if(!NextButton.isEnabled()){ //if NextButton is NextButton.setEnabled(true); //disabled, enable it } if(position == 0){ //If we are viewing the first Picture in PreviousButton.setEnabled(false); //the directory, disable previous button } setLabelIcon(Path,filenames[position]); }//End of PreviousButton handler /** *Handler for NextButton */ private void NextButtonActionPerformed(java.awt.event.ActionEvent evt) {
  • 4.
    position++; //Increment theindex position of the array of filenames by one on buttonPressed if(!PreviousButton.isEnabled()){ listFiles(Path); PreviousButton.setEnabled(true); } if(position == filenames.length){ NextButton.setEnabled(false); position --; return; } setLabelIcon(Path,filenames[position]); }//End of NextButton handler public static void main(String args[]) { try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(ImageViewer.class.getName()).log(java.util.logging.Level.S EVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(ImageViewer.class.getName()).log(java.util.logging.Level.S EVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(ImageViewer.class.getName()).log(java.util.logging.Level.S EVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(ImageViewer.class.getName()).log(java.util.logging.Level.S EVERE, null, ex); }
  • 5.
    /* * Create anddisplay the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new ImageViewer().setVisible(true); } }); } /**Method to list all the files in the directory *And store their names in an array */ private void listFiles(String Path){ File file = new File(Path); filenames = file.list(); //store the file names in the array of strings. for(String names : filenames){ System.out.println(names); //Print the filenames to the console just so you can see } } //Method to set the picture on the label private void setLabelIcon(String Path,String name){ File file = new File(Path+""+name); java.awt.image.BufferedImage image = null; try{ image = ImageIO.read(file); //Reas the image from the file. }catch(IOException ie){ javax.swing.JOptionPane.showMessageDialog(this,"Error reading image file","Error", javax.swing.JOptionPane.ERROR_MESSAGE); } ImageIcon icon = new ImageIcon(image); int width = 600; int height = 400; Image img = icon.getImage().getScaledInstance(width,height,java.awt.Image.SCALE_SMOOTH); //Images produced will remain a fixed size, 600 * 400
  • 6.
    ImageIcon newIcon =new ImageIcon(img); //Create a new imageicon from an image object. //Now we want to create a caption for the pictures using their file names String pictureName = file.getName(); int pos = pictureName.lastIndexOf("."); //This removes the extensions String caption = pictureName.substring(0,pos); //from the file names. e.g .gif, .jpg, .png picLabel.setIcon(newIcon); //Set the imageIcon on the Label picLabel.setText(caption); //Set the caption picLabel.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM); //Caption appears below the image } //Variables declaration int position = 0; //Initial position is 0 String[] filenames; //Array to hold the file names final String Path = System.getProperty("user.home") +"User_Namepictures"; //path to your images private javax.swing.JButton NextButton; private javax.swing.JButton PreviousButton; private javax.swing.JLabel picLabel; // End of variables declaration//GEN-END:variables }//End of class -------------------------------------------------------------------------------------------------------------------- -------------- feel free to reach out to me if you have any doubts in understanding this code. Keep learning :)