School Lecture Study/과제를 해보자

[소프트웨어프로젝트] 과제 6

vㅔ로 2021. 6. 20. 16:05
728x90

한 학기 동안 정말 별 생각이 다 들었던 소프트웨어프로젝트 과제 6을 올려보려한다. 

해커톤 때문에 바빠서 과제 6은 제출 당일에 작성하게 되었는데, 10시간 정도 걸렸다. 

막 걱정한 것 치고는 그렇게 어렵지는 않았는데, 교수님이 의도한 바를 잘 구현했는지는 잘 모르겠다. 

그렇게 깔끔하게 짜지는 못한 것 같다. 교수님이 Java 스타일을 강조하셨는데, 이렇게 짠 것도 Java 스타일이라고 할 수 있을까 싶다. 

 

과제를 하면서 고민했던 것은 어떻게 화면 갱신을 할까였다. 그냥 component에 revalidate, repaint만 하면 되는 줄 알았는데 전혀 갱신이 되지 않아서 고민했었다. 그래서 아예 JFrame에서 삭제하고 다시 add하는 방식으로 구현하였다. 다행히 이렇게 하니 구현되었다.

 

소프트웨어프로젝트 과제를 모르시는 분들을 위해... 파일 저장, 저장한 파일 로드, 새로운 이미지 파일 추가, 이미지 정렬 기능, 이미지 정보 수정 기능 등을 구현하였다. 

다만 스크롤은 되지 않기 때문에 어느 정도 넘어가면 사진이 안 보인다. 

 

(과제 6에서 2점이 감점되어 과제 4등을 했다. 어디서 감점된거지...?)

Album.class

package sw_project6;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.Timer;
import java.util.TimerTask;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.filechooser.FileNameExtensionFilter;
// Album 클래스
public class PhotoInfoInput extends JFrame{
	private JFrame addPictures;
	private JPanel imageInfos, addLabels, addInsertInfo, addPictureInfoDownButtons,
	subDownButtons, subLabels, setTextFields;
	private JButton selectButton, okButton, cancelButton;
	private JLabel name, addedTime, category, createdTime, imageFile, select;
	private JTextField nameText, categoryText, createdTimeText, imageFileText, addedTimeText;
	private Album album;
	private String filePath;
	private Photo photo;
	private int whatButton = 0, selectedNumber;
	private static PhotoFrame photoFrame;
	
	public PhotoInfoInput(PhotoFrame photoFrame, Album album) {
		this.album = album;
		setJFrame();
		whatButton = 0;
		this.photoFrame = photoFrame;
	}
	
	public PhotoInfoInput(PhotoFrame photoFrame,Album album, Photo photo, int selectedNumber) {
		this.album = album;
		setJFrame();
		nameText.setText(photo.getPhotoName());
		imageFileText.setText(photo.getFileName());
		categoryText.setText(photo.getCategory());
		createdTimeText.setText(photo.getPicturedTime());
		whatButton = 1;
		this.selectedNumber = selectedNumber;
		this.photoFrame = photoFrame;
	}
	
	private void setJFrame() {
		setLayout(new GridLayout(3, 1));
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		
		addImageInfo();
		add(subLabels);
		add(setTextFields);
		add(addPictureInfoDownButtons);
		pack();
		setVisible(true);
	}
	
	private void addImageInfo() {
		setImageInfoLabels();
		setImageInfoTextField();
		setImageInfoButtons();
	}
	
	private void Framecancel() {
		Timer timer = new Timer();
		TimerTask task = new TimerTask() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				dispose();
			}
		};
		timer.schedule(task, 60000);
	}
	
	private void setImageInfoButtons() {
		addPictureInfoDownButtons = new JPanel();
		subDownButtons = new JPanel();
		subDownButtons.setLayout(new FlowLayout());
		addPictureInfoDownButtons.setLayout(new BorderLayout());
		cancelButton = new JButton("Cancel");
		
		cancelButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				dispose();
			}
			
		});
		
		okButton = new JButton("OK");
		okButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if(whatButton == 1) {
					modifyPhoto();
				}
				else {
					transferNewImageInfo();
					photoFrame.refreshDateAlbum();
				}
				dispose();
			}
		});
		
		subDownButtons.add(cancelButton);
		subDownButtons.add(okButton);
		addPictureInfoDownButtons.add(subDownButtons, BorderLayout.CENTER);
		pack();
	}
	public Album getNewAlbum() {
		return this.album;
	}
	
	private void modifyPhoto() {
		String fileName, addedTime, filePath, fileCategory, createdTime;
		fileName = nameText.getText();
		filePath = imageFileText.getText();
		fileCategory = categoryText.getText();
		createdTime = createdTimeText.getText();
		album.photoAlbum.get(selectedNumber).setCategory(fileCategory);
		album.photoAlbum.get(selectedNumber).setFileName(filePath);
		album.photoAlbum.get(selectedNumber).setPicturedTime(createdTime);
		album.photoAlbum.get(selectedNumber).setPhotoName(fileName);
		album.photoAlbum.get(selectedNumber).setAddedTimeAndId();
	}
	
	
	private void transferNewImageInfo() {
		String fileName, addedTime, filePath, fileCategory, createdTime;
		fileName = nameText.getText();
		addedTime = addedTimeText.getText();
		filePath = imageFileText.getText();
		fileCategory = categoryText.getText();
		createdTime = createdTimeText.getText();
		// String photoName, String category, String fileName, String picturedTime
		Photo photo = new Photo(fileName, fileCategory, filePath, createdTime);
		album.createFilePhoto(photo);
	}
	
	private void setImageInfoLabels() {
		subLabels = new JPanel();
		subLabels.setLayout(new GridLayout(1, 6));
		name = new JLabel("Name");
		addedTime = new JLabel("Added Time");
		category = new JLabel("Category");
		createdTime = new JLabel("Created Time...");
		imageFile = new JLabel("Image File");
		select = new JLabel("Select");
		subLabels.add(name);
		subLabels.add(addedTime);
		subLabels.add(category);
		subLabels.add(createdTime);
		subLabels.add(imageFile);
		subLabels.add(select);
		pack();
	}
	
	private void setImageInfoTextField() {
		setTextFields = new JPanel();
		setTextFields.setLayout(new GridLayout(1, 6));
		nameText = new JTextField();
		addedTimeText = new JTextField();
		categoryText = new JTextField();
		createdTimeText = new JTextField();
		imageFileText = new JTextField();
		selectButton = new JButton("File");
		selectButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				fileSearch();
				imageFileText.setText(filePath);
			}
		});
		addedTimeText.setVisible(false);
		setTextFields.add(nameText);
		setTextFields.add(addedTimeText);
		setTextFields.add(categoryText);
		setTextFields.add(createdTimeText);
		setTextFields.add(imageFileText);
		setTextFields.add(selectButton);
		pack();
	}
	
	private void fileSearch() {
		JFrame fileChooseWindow = new JFrame();
		
		JFileChooser fileChooser = new JFileChooser();
		FileNameExtensionFilter filter = new FileNameExtensionFilter("img 파일", ImageIO.getReaderFileSuffixes());
		fileChooser.addChoosableFileFilter(filter);
		int result = fileChooser.showOpenDialog(this);
		
		if(result == JFileChooser.APPROVE_OPTION) {
			File selectedFile = fileChooser.getSelectedFile();
			this.filePath = selectedFile.toString();
			System.out.println(filePath);
		}
	}
	
}

Photo.class

package sw_project6;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class Photo {
	private String id, photoName, addedTime, category, fileName, picturedTime = "";
	
	Photo(String imageFileName){
		setAddedTimeAndId();
	}
	
	Photo(String photoName, String category, String fileName, String picturedTime) {
		setAddedTimeAndId();
		setCategory(category);
		setFileName(fileName);
		setPhotoName(photoName);
		setPicturedTime(picturedTime);
	}

	Photo(String id, String photoName, String addedTime, String category, String fileName){
		setId(id);
		setPhotoName(photoName);
		setAddedTime(addedTime);
		setCategory(category);
		setFileName(fileName);
	}
	
	Photo(String id, String photoName, String addedTime, String category, String fileName, String picturedTime){
		this(id, photoName, addedTime, category, fileName);
		setPicturedTime(picturedTime);
	}
	
	public void setAddedTimeAndId() {
		LocalDateTime date = LocalDateTime.now();
		DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd_HH:mm:ss:SSS");
		this.addedTime = date.format(dtf);
		this.id = "IMG" + date.format(dtf);
	}

	public void setAddedTime(String addedTime) {
		this.addedTime = addedTime;
	}

	public void setCategory(String category) {
		this.category = category;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public void setId(String id) {
		this.id = id;
	}
	
	public void setPhotoName(String photoName) {
		this.photoName = photoName;
	}

	public void setPicturedTime(String picturedTime) {
		this.picturedTime = picturedTime;
	}

	public String getId() {
		return id;
	}

	public String getAddedTime() {
		return addedTime;
	}

	public String getCategory() {
		return category;
	}

	public String getFileName() {
		return fileName;
	}

	public String getPhotoName() {
		return photoName;
	}

	public String getPicturedTime() {
		return picturedTime;
	}
	
	public void print() {
		System.out.println(id + ";" +  photoName + ";" + addedTime + ";" + category + ";" + fileName + ";");
	}
}

FileSave.class

중간에 시험 준비를 하느라 이진 파일로 저장하는 메소드도 구현되어 있다. 

package sw_project6;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;

public class FileSave {
	private Album album;
	File file = new File("PhotoSaveFile.txt");
	
	FileSave(Album album){
		this.album = album;
		saveAlbumInfosBinaryFile("PhotoSave.data");
	}
	
	private void saveAlbumInfos(String filePath){
		try {
			FileWriter writer = new FileWriter(file);
			for (int i = 0; i<album.numPhotos(); i++){
				String fileContent = album.getPhoto(i).getId() + ";" + album.getPhoto(i).getPhotoName() + ";" + 
				album.getPhoto(i).getAddedTime() + ";" + album.getPhoto(i).getCategory() + ";" + 
				album.getPhoto(i).getFileName() + ";" + album.getPhoto(i).getPicturedTime() + ";" + "\n";
				writer.write(fileContent);
			}
			writer.flush();
			writer.close();
		} catch(Exception e){
			e.printStackTrace();
		}
	}
	
	private void saveAlbumInfosBinaryFile(String filepPath) {
		DataOutputStream output = null;
		try {
			output = new DataOutputStream(new FileOutputStream("PhotoSave.data"));
			try {
				for(int i = 0; i<album.numPhotos();i++) {
					String fileContent = album.getPhoto(i).getPhotoName() + ";" + 
							album.getPhoto(i).getAddedTime() + ";" + album.getPhoto(i).getCategory() + ";" + 
							album.getPhoto(i).getFileName() + ";" + album.getPhoto(i).getPicturedTime() + ";" + "\n";
					output.writeBytes(album.getPhoto(i).getId() + ";" );
					output.writeUTF(fileContent);
				}
				output.close();
			} catch(IOException e) {
				System.out.println("파일 입력 오류입니다.");
			}
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}
}

 

PhotoFrame.class

메인 GUI를 담당하는 PhotoFrame 클래스이다. 

package sw_project6;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import javax.swing.*;
import javax.swing.border.TitledBorder;

public class PhotoFrame extends JFrame {
	private JPanel upperButtons, downButtons, imageInfos, imageOtherInfos, imageDateTotalInfos, imageCategoryTotalInfos;
	private JButton editButton, addButton, delButton, loadButton, saveButton, dateButton, categoryButton;
	private int selectedPhotoNum = -1;
	private Album photoAlbum;
	private PhotoInfoInput newPhotoFile, editPhotoFile;
	private PhotoFrame thisFrame;
	
	public PhotoFrame(Album album) {
		super("Simple Photo Info");
		this.photoAlbum = album;
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLayout(new BorderLayout());
		setUpperButtons();
		add(upperButtons, BorderLayout.NORTH);
		setDownButtons();
		add(downButtons, BorderLayout.SOUTH);
		setDateAlbumPhotos(album);
		add(imageDateTotalInfos, BorderLayout.CENTER);
		pack();
		
		setButtonImplementations();
		setVisible(true);
		thisFrame = this;
	}
	
	
	
	private void setDialog() {
		JFrame jframe = new JFrame();
		JButton confirm = new JButton("확인");
		JOptionPane.showMessageDialog(null,  "이미지를 선택해주세요", "선택 오류", JOptionPane.WARNING_MESSAGE);
		confirm.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				jframe.dispose();
			}
		});
	}
	
	private void setUpperButtons() {
		upperButtons = new JPanel();
		upperButtons.setLayout(new BorderLayout());
		dateButton = new JButton("Date");
		categoryButton = new JButton("Category");
		upperButtons.add(dateButton, BorderLayout.WEST);
		upperButtons.add(categoryButton, BorderLayout.EAST);
		pack();
	}
	
	private void setDownButtons() {
		downButtons = new JPanel();
		downButtons.setLayout(new GridLayout(1, 5));
		editButton = new JButton("EDIT");
		addButton = new JButton("ADD");
		delButton = new JButton("DEL...");
		loadButton = new JButton("LOAD");
		saveButton = new JButton("SAVE");
		downButtons.add(editButton);
		downButtons.add(addButton);
		downButtons.add(delButton);
		downButtons.add(loadButton);
		downButtons.add(saveButton);
		pack();
	}
	
	
	
	public void refreshDateAlbum() {
		getContentPane().remove(imageDateTotalInfos);
		imageDateTotalInfos.removeAll();
		setDateAlbumPhotos(photoAlbum);
		getContentPane().add(imageDateTotalInfos, BorderLayout.CENTER);
		imageDateTotalInfos.revalidate();
		imageDateTotalInfos.repaint();
		revalidate();
		repaint();
		pack();
	}
	
	private void setCategoryAlbumPhotos(Album album) {
		imageDateTotalInfos = new JPanel();
		ArrayList<String> photoCategory = getCategory(album);
		ArrayList<String> photosFilePath = getPhotoFilePath(album);
		ArrayList<String> photosName = getPhotoName(album);
		imageDateTotalInfos.setLayout(new GridLayout(photoCategory.size(), 1));  // 카테고리 순서대로 정렬할 때
		
		for(int i = 0; i<photoCategory.size(); i++) {
			imageInfos = new JPanel();
			imageInfos.setBorder(new TitledBorder(photoCategory.get(i)));
			imageInfos.setLayout(new GridLayout(0,3));
			for(int j = 0; j<album.numPhotos(); j++) {
				if(isSameCategory(album.getPhoto(j), photoCategory.get(i))) {
					insertPictureInfo(imageInfos, j, photosFilePath.get(j), photosName.get(j));
				}
			}
			imageDateTotalInfos.add(imageInfos);
			pack();
		}
	}
	
	private boolean isSameCategory(Photo photo, String category) {
		if(photo.getCategory().equals(category)) return true;
		else return false;
		 
	}
	
	private void setDateAlbumPhotos(Album album){
		imageDateTotalInfos = new JPanel();
		ArrayList<String> dateSet = getPhotoSameDateNum(album);
		ArrayList<String> photosFilePath = getPhotoFilePath(album);
		ArrayList<String> photosName = getPhotoName(album);
		imageDateTotalInfos.setLayout(new GridLayout(dateSet.size(), 1));  // 시간 순서대로 정렬할 때
	
		for(int i = 0; i<dateSet.size(); i++) {
			imageInfos = new JPanel();
			imageInfos.setBorder(new TitledBorder(dateSet.get(i)));
			imageInfos.setLayout(new GridLayout(0,3));
			for(int j = 0; j<album.numPhotos(); j++) {
				if(isSameDate(album.getPhoto(j), dateSet.get(i))) {
					insertPictureInfo(imageInfos, j, photosFilePath.get(j), photosName.get(j));
				}
			}
			imageDateTotalInfos.add(imageInfos);
			pack();
		}
		
	}
	
	private boolean isSameDate(Photo photo, String date) {
		String photoOriginDate = photo.getAddedTime();
		String[] strArray = photoOriginDate.split("_");
		if(strArray[0].equals(date)) return true;
		else return false;
	}
	
	private ArrayList<String> getPhotoName(Album album){
		// 앨범 사진 이름 가져오기
		ArrayList<String> photoNames = new ArrayList<>();
		for(int i = 0; i<album.numPhotos(); i++) {
			photoNames.add(album.getPhoto(i).getPhotoName());
		}
		return photoNames;
	}
	
	private ArrayList<String> getCategory(Album album) {
		// 앨범 사진 카테고리 가져오기
		ArrayList<String> photoCategory = new ArrayList<>();
		for(int i = 0; i<album.numPhotos(); i++) {
			String category = album.getPhoto(i).getCategory();
			if(!photoCategory.contains(category)) {
				photoCategory.add(category);
			}
		}
		return photoCategory;
	}
	
	private ArrayList<String> getPhotoFilePath(Album album){
		// 앨범에 있는 사진들 filePath 가져오기
		ArrayList<String> photoFilePath = new ArrayList<>();
		for(int i = 0; i<album.numPhotos(); i++) {
			photoFilePath.add(album.getPhoto(i).getFileName());
		}
		return photoFilePath;
	}
	
	private ArrayList<String> getPhotoSameDateNum(Album album) {
		// 앨범 사진들의 날짜 가져오기 (년월일만 파싱)
		ArrayList<String> dateSet = new ArrayList<>();
		for(int i = 0; i<album.numPhotos(); i++) {
			String photoAddedDate = album.getPhoto(i).getAddedTime();
			String[] strArray = photoAddedDate.split("_");
			if(!dateSet.contains(strArray[0])) {
				dateSet.add(strArray[0]);
			}
		}
		adjustDateArray(dateSet);
		return dateSet;
	}
	
	private void adjustDateArray(ArrayList<String> dateSet) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	    try {
	    	String temp;
	    	for(int i = 0; i<dateSet.size(); i++) {
				Date date1 = sdf.parse(dateSet.get(i));
				for(int j = i; j<dateSet.size(); j++) {
					Date date2 = sdf.parse(dateSet.get(j));
					if(date1.before(date2)) {
						temp = dateSet.get(j);
						dateSet.set(j, dateSet.get(i));
						dateSet.set(i, temp);
					}
				}
			}
	    } catch(Exception e) {
	    	e.printStackTrace();
	    }
		
	}
	
	private void insertPictureInfo(JPanel panel, int i, String path, String pictureNameString) {
		JPanel bindImageString = new JPanel();
		bindImageString.setLayout(new FlowLayout());
		ImageIcon icon = new ImageIcon(path);
		Image originImg = icon.getImage();
		Image changedImg = originImg.getScaledInstance(100,  100,  Image.SCALE_SMOOTH);
		icon = new ImageIcon(changedImg);
		JButton picture = new JButton(pictureNameString, icon);
		picture.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				selectedPhotoNum = i;
			}
		});
		
		// 버튼 배경, 테두리 없애기
		picture.setBorderPainted(false);
		picture.setContentAreaFilled(false);
		
		picture.setHorizontalTextPosition(SwingConstants.CENTER);
		picture.setVerticalTextPosition(SwingConstants.BOTTOM);
		picture.setIconTextGap(5);
		bindImageString.add(picture);
		panel.add(bindImageString);
		pack();
	}
	
	private void setButtonImplementations() {
		delButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				if(selectedPhotoNum == -1) {
					setDialog();
				} else {
					ArrayList<Photo> modifiedAlbum = photoAlbum.getOriginAlbum();
					modifiedAlbum.remove(selectedPhotoNum);
					photoAlbum.photoAlbum = modifiedAlbum;
					Album.num--;
					refreshDateAlbum();
					selectedPhotoNum = -1;
				}
			}
			
		});
		addButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				newPhotoFile = new PhotoInfoInput(thisFrame, photoAlbum);
				photoAlbum = newPhotoFile.getNewAlbum();
				refreshDateAlbum();
			}
		});
		
		categoryButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				SwingUtilities.invokeLater(new Runnable() {
					@Override
					public void run() {
						getContentPane().remove(imageDateTotalInfos);
						imageDateTotalInfos.removeAll();
						setCategoryAlbumPhotos(photoAlbum);
						getContentPane().add(imageDateTotalInfos, BorderLayout.CENTER);
						imageDateTotalInfos.revalidate();
						imageDateTotalInfos.repaint();
						revalidate();
						repaint();
						pack();
						selectedPhotoNum = -1;
					}
				});
			}
			
		});
		saveButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				new FileSave(photoAlbum);
				selectedPhotoNum = -1;
			}
			
		});
		loadButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				fileLoad("PhotoSave.data");
				selectedPhotoNum = -1;
			}
			
		});
		dateButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				SwingUtilities.invokeLater(new Runnable() {
					@Override
					public void run() {
						refreshDateAlbum();
						selectedPhotoNum = -1;
					}
				});
				
			}
			
		});
		editButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if(selectedPhotoNum == -1) {
					setDialog();
				} else {
					editPhotoFile = new PhotoInfoInput(thisFrame, photoAlbum, photoAlbum.getPhoto(selectedPhotoNum), selectedPhotoNum);
					photoAlbum = editPhotoFile.getNewAlbum();
					refreshDateAlbum();
					selectedPhotoNum = -1;
				}
			}
		});
	}
	
	private void fileLoad(String filePath) {
		Album.num = 0;
		Album newAlbum = new Album(filePath);
		photoAlbum = newAlbum;
		refreshDateAlbum();
	}
}

 

PhotoInfoInput.class

파일 수정, 추가를 할 수 있는 JFrame인 PhotoInfoInput 클래스이다.

 

package sw_project6;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.Timer;
import java.util.TimerTask;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.filechooser.FileNameExtensionFilter;

public class PhotoInfoInput extends JFrame{
	private JFrame addPictures;
	private JPanel imageInfos, addLabels, addInsertInfo, addPictureInfoDownButtons,
	subDownButtons, subLabels, setTextFields;
	private JButton selectButton, okButton, cancelButton;
	private JLabel name, addedTime, category, createdTime, imageFile, select;
	private JTextField nameText, categoryText, createdTimeText, imageFileText, addedTimeText;
	private Album album;
	private String filePath;
	private Photo photo;
	private int whatButton = 0, selectedNumber;
	private static PhotoFrame photoFrame;
	
	public PhotoInfoInput(PhotoFrame photoFrame, Album album) {
		this.album = album;
		setJFrame();
		whatButton = 0;
		this.photoFrame = photoFrame;
	}
	
	public PhotoInfoInput(PhotoFrame photoFrame,Album album, Photo photo, int selectedNumber) {
		this.album = album;
		setJFrame();
		nameText.setText(photo.getPhotoName());
		imageFileText.setText(photo.getFileName());
		categoryText.setText(photo.getCategory());
		createdTimeText.setText(photo.getPicturedTime());
		whatButton = 1;
		this.selectedNumber = selectedNumber;
		this.photoFrame = photoFrame;
	}
	
	private void setJFrame() {
		setLayout(new GridLayout(3, 1));
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		
		addImageInfo();
		add(subLabels);
		add(setTextFields);
		add(addPictureInfoDownButtons);
		pack();
		setVisible(true);
	}
	
	private void addImageInfo() {
		setImageInfoLabels();
		setImageInfoTextField();
		setImageInfoButtons();
	}
	
	private void Framecancel() {
		Timer timer = new Timer();
		TimerTask task = new TimerTask() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				dispose();
			}
		};
		timer.schedule(task, 60000);
	}
	
	private void setImageInfoButtons() {
		addPictureInfoDownButtons = new JPanel();
		subDownButtons = new JPanel();
		subDownButtons.setLayout(new FlowLayout());
		addPictureInfoDownButtons.setLayout(new BorderLayout());
		cancelButton = new JButton("Cancel");
		
		cancelButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				dispose();
			}
			
		});
		
		okButton = new JButton("OK");
		okButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if(whatButton == 1) {
					modifyPhoto();
				}
				else {
					transferNewImageInfo();
					photoFrame.refreshDateAlbum();
				}
				dispose();
			}
		});
		
		subDownButtons.add(cancelButton);
		subDownButtons.add(okButton);
		addPictureInfoDownButtons.add(subDownButtons, BorderLayout.CENTER);
		pack();
	}
	public Album getNewAlbum() {
		return this.album;
	}
	
	private void modifyPhoto() {
		String fileName, addedTime, filePath, fileCategory, createdTime;
		fileName = nameText.getText();
		filePath = imageFileText.getText();
		fileCategory = categoryText.getText();
		createdTime = createdTimeText.getText();
		album.photoAlbum.get(selectedNumber).setCategory(fileCategory);
		album.photoAlbum.get(selectedNumber).setFileName(filePath);
		album.photoAlbum.get(selectedNumber).setPicturedTime(createdTime);
		album.photoAlbum.get(selectedNumber).setPhotoName(fileName);
		album.photoAlbum.get(selectedNumber).setAddedTimeAndId();
	}
	
	
	private void transferNewImageInfo() {
		String fileName, addedTime, filePath, fileCategory, createdTime;
		fileName = nameText.getText();
		addedTime = addedTimeText.getText();
		filePath = imageFileText.getText();
		fileCategory = categoryText.getText();
		createdTime = createdTimeText.getText();
		// String photoName, String category, String fileName, String picturedTime
		Photo photo = new Photo(fileName, fileCategory, filePath, createdTime);
		album.createFilePhoto(photo);
	}
	
	private void setImageInfoLabels() {
		subLabels = new JPanel();
		subLabels.setLayout(new GridLayout(1, 6));
		name = new JLabel("Name");
		addedTime = new JLabel("Added Time");
		category = new JLabel("Category");
		createdTime = new JLabel("Created Time...");
		imageFile = new JLabel("Image File");
		select = new JLabel("Select");
		subLabels.add(name);
		subLabels.add(addedTime);
		subLabels.add(category);
		subLabels.add(createdTime);
		subLabels.add(imageFile);
		subLabels.add(select);
		pack();
	}
	
	private void setImageInfoTextField() {
		setTextFields = new JPanel();
		setTextFields.setLayout(new GridLayout(1, 6));
		nameText = new JTextField();
		addedTimeText = new JTextField();
		categoryText = new JTextField();
		createdTimeText = new JTextField();
		imageFileText = new JTextField();
		selectButton = new JButton("File");
		selectButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				fileSearch();
				imageFileText.setText(filePath);
			}
		});
		addedTimeText.setVisible(false);
		setTextFields.add(nameText);
		setTextFields.add(addedTimeText);
		setTextFields.add(categoryText);
		setTextFields.add(createdTimeText);
		setTextFields.add(imageFileText);
		setTextFields.add(selectButton);
		pack();
	}
	
	private void fileSearch() {
		JFrame fileChooseWindow = new JFrame();
		
		JFileChooser fileChooser = new JFileChooser();
		FileNameExtensionFilter filter = new FileNameExtensionFilter("img 파일", ImageIO.getReaderFileSuffixes());
		fileChooser.addChoosableFileFilter(filter);
		int result = fileChooser.showOpenDialog(this);
		
		if(result == JFileChooser.APPROVE_OPTION) {
			File selectedFile = fileChooser.getSelectedFile();
			this.filePath = selectedFile.toString();
			System.out.println(filePath);
		}
	}
	
}

 

TestClass

테스트를 위한 클래스이다. 

package sw_project6;

public class TestClass {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String filePath = "Photo-normal.data";
		Album album = new Album(filePath);
		new PhotoFrame(album);
	}

}

혹시라도 테스트를 해보고 싶으신 분들을 위해 Photo-normal.data도 올려두겠다.

Photo-normal.data
0.00MB

 

다시 보니 길다...

728x90