You are on page 1of 2

import javax.swing.

*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

class GalleryAppGUI extends JFrame {


private List<String> photoList;
private JPanel photoPanel;
private JScrollPane photoScrollPane;

public GalleryAppGUI() {
super("Photo Gallery App");
this.photoList = new ArrayList<>();

// GUI components setup


JButton addButton = new JButton("Add Photo");
JButton displayButton = new JButton("Display Photos");
JButton deleteButton = new JButton("Delete Photo");

photoPanel = new JPanel();


photoPanel.setLayout(new GridLayout(0, 4, 10, 10)); // 4 photos per row,
10px horizontal and vertical gap
photoScrollPane = new JScrollPane(photoPanel);

// Layout setup
setLayout(new BorderLayout());

JPanel buttonPanel = new JPanel(new FlowLayout());


buttonPanel.add(addButton);
buttonPanel.add(displayButton);
buttonPanel.add(deleteButton);

add(buttonPanel, BorderLayout.NORTH);
add(photoScrollPane, BorderLayout.CENTER);

// Event listeners
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
addPhoto();
}
});

displayButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
displayPhotos();
}
});

deleteButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
deletePhoto();
}
});

// Set up the frame


setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(1200, 800);
setLocationRelativeTo(null); // Center the frame
setVisible(true);
}

private void addPhoto() {


JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
fileChooser.setMultiSelectionEnabled(true);

int returnValue = fileChooser.showOpenDialog(this);

if (returnValue == JFileChooser.APPROVE_OPTION) {
File[] selectedFiles = fileChooser.getSelectedFiles();
for (File selectedFile : selectedFiles) {
String filePath = selectedFile.getAbsolutePath();
photoList.add(filePath);
}
}
}

private void displayPhotos() {


photoPanel.removeAll();

for (String photo : photoList) {


ImageIcon imageIcon = new ImageIcon(photo);
Image scaledImage = scaleImage(imageIcon.getImage(), 200, 200);
ImageIcon scaledIcon = new ImageIcon(scaledImage);

JLabel photoLabel = new JLabel(scaledIcon);


photoPanel.add(photoLabel);
}

photoPanel.revalidate();
photoPanel.repaint();
}

private Image scaleImage(Image image, int width, int height) {


return image.getScaledInstance(width, height, Image.SCALE_SMOOTH);
}

private void deletePhoto() {


int option = JOptionPane.showConfirmDialog(this, "Are you sure you want to
delete all photos?", "Delete Photos", JOptionPane.YES_NO_OPTION);

if (option == JOptionPane.YES_OPTION) {
photoList.clear();
photoPanel.removeAll();
photoPanel.revalidate();
photoPanel.repaint();
}
}
}

public class GalleryApp {


public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new GalleryAppGUI());
}
}

You might also like