Learn how to create an Image Viewer application using Java programming language. This app will allow you to view and navigate through a collection of images with simple controls. It’s perfect for beginners looking to explore GUI development in Java.
Objective
The objective of this project is to develop an Image Viewer application that lets users open and view images in a graphical user interface (GUI). Users will be able to navigate through images with simple buttons, making it an intuitive and user-friendly app.
Java Code for Image Viewer
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.File; import javax.swing.filechooser.FileNameExtensionFilter; public class ImageViewer { private JFrame frame; private JLabel imageLabel; private JButton nextButton, prevButton; private File[] imageFiles; private int currentIndex = 0; public ImageViewer() { frame = new JFrame("Image Viewer"); frame.setLayout(new BorderLayout()); imageLabel = new JLabel(); imageLabel.setHorizontalAlignment(JLabel.CENTER); JPanel buttonPanel = new JPanel(); nextButton = new JButton("Next"); prevButton = new JButton("Previous"); buttonPanel.add(prevButton); buttonPanel.add(nextButton); frame.add(imageLabel, BorderLayout.CENTER); frame.add(buttonPanel, BorderLayout.SOUTH); nextButton.addActionListener(e -> showNextImage()); prevButton.addActionListener(e -> showPreviousImage()); loadImageFiles(); showImage(currentIndex); frame.setSize(800, 600); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } private void loadImageFiles() { JFileChooser fileChooser = new JFileChooser(); fileChooser.setDialogTitle("Select Images"); fileChooser.setMultiSelectionEnabled(true); fileChooser.setFileFilter(new FileNameExtensionFilter("Images", "jpg", "jpeg", "png", "gif")); int result = fileChooser.showOpenDialog(frame); if (result == JFileChooser.APPROVE_OPTION) { imageFiles = fileChooser.getSelectedFiles(); } else { System.exit(0); } } private void showImage(int index) { if (imageFiles == null || imageFiles.length == 0) { return; } ImageIcon imageIcon = new ImageIcon(imageFiles[index].getPath()); imageLabel.setIcon(imageIcon); } private void showNextImage() { if (imageFiles == null || imageFiles.length == 0) return; currentIndex = (currentIndex + 1) % imageFiles.length; showImage(currentIndex); } private void showPreviousImage() { if (imageFiles == null || imageFiles.length == 0) return; currentIndex = (currentIndex - 1 + imageFiles.length) % imageFiles.length; showImage(currentIndex); } public static void main(String[] args) { SwingUtilities.invokeLater(ImageViewer::new); } }
Explanation of the Program Structure
This program creates a simple Image Viewer app using Java Swing. Let’s break down its structure:
- JFrame: This is the main window of the application that holds all components like the image display and buttons.
- JLabel: It is used to display images. The image is updated each time the user navigates through the collection of images.
- JButton: There are two buttons, “Next” and “Previous”, which allow users to navigate through the images. Each button has an event listener attached to perform the respective action.
- JFileChooser: This component is used to select multiple image files. It opens a dialog window for users to choose images from their file system.
- Action Listeners: The “Next” and “Previous” buttons are associated with action listeners that trigger the image navigation logic.
- Image Navigation: The `showNextImage()` and `showPreviousImage()` methods allow the user to cycle through the images. The current image index is updated, and the corresponding image is displayed.
How to Run the Program
Follow these steps to run the Image Viewer application:
- Ensure that you have Java installed on your machine.
- Create a new Java file named ImageViewer.java and copy the code above into it.
- Compile the program using the following command in your terminal or command prompt:
javac ImageViewer.java
- Run the program with the following command:
java ImageViewer
- A window will open, allowing you to select image files. After selecting, you can navigate through the images using the “Next” and “Previous” buttons.