Lab7

Lab7 – Collection (Due by 6/7)

java1-lab7-PhotoManager

Lab7_template

Lab7 프로젝트 디렉토리 안에 모든 파일(src/*.java & bin/*.class)와 보고서 (장수 제한 없음)를 넣고 Lab7_학번_이름.zip 압축한 후 e-learning (http://lms.dankook.ac.kr/index.jsp)으로 제출

Photo 클래스는 이미지파일을 읽어서 이미지 버퍼(BufferedImage)로 저장
-Photo 클래스의 Comparable vs Comparator 인터페이스를 비교분석한다.
-Photo 클래스의 equals vs hashCode를 비교분석한다.
ImageLabel 클래스는 Photo로 썸네일이미지레이블을 만들어 주는 클래스 (fullPath로 탐색가능하게 해줌)
-MainFrame 클래스는 Add/Find/Remove/RemoveAll/Sort/Close 버튼을 누르면 PhotoManager를 이용하여 Photo 이미지레이블을 추가/탐색/제거/모두제거/정렬/종료하는 GUI 를 구현한다.
-PhotoManager는 List 관리자를 구현한다.
-Photo[] vs ArrayList<Photo>를 비교분석한다.
-ArrayList<Photo> vs HashMap<Photo, Integer> 를 비교분석한다.

보고서의 내용은 기존 코드 분석과 이해한 내용을 적는다.

Lab6 open


// File->Open MenuItem, O - Mnemonic
JMenuItem openMenuItem = new JMenuItem("Open", KeyEvent.VK_O);
fileMenu.add(openMenuItem);
openMenuItem.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent event) {
        System.out.println("OPEN");
        File[] selectedFiles = openFileDialog();
        if (selectedFiles == null) return;
        List photoList = new ArrayList(); // photoList
        for (int i = 0; i < selectedFiles.length; i++) {
            try {
                String fullPath = selectedFiles[i].getCanonicalPath(); //String fullPath = file.getCanonicalPath().replace('\\', '/');
                System.out.println("OPEN fullPath=" + fullPath);
                Photo photo = new Photo(fullPath);
                if (photo.getImg() != null) { // if image is loaded
                    photoList.add(photo);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }	
	photoArray = new Photo[photoList.size()]; // create photoArray with photoList.size()
	photoArray = photoList.toArray(photoArray); // photoList => photoArray
	for (Photo p : photoArray) p.print(); // debug to print photoArray
	
	displayPanel.loadPhotos(photoArray); // reload photoArray in displayPanel
	displayPanel.repaint(); // repaint
    }
});

== vs equal vs hashCode vs contains

EqualsHashCodeCollectionContains


class EqualsHashCodeCollectionContainsTest {
    // getReference 
    public static String getReference(Object o){
        return o.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(o));
    }

    public static void main(String[] args)  {

	// primitive type (== 연산자는 값이 같으면 true)
	int i = 10;
	int j = 10;
	int k = i;
	System.out.println("i == j " + (i == j));
	System.out.println("i == k " + (i == k));
	System.out.println();

	Object o1 = i; // boxing
	Object o2 = j; // boxing
	Object o3 = o1;
	System.out.println(getReference(o1) + " o1=" + o1);
	System.out.println(getReference(o2) + " o2=" + o2);
	System.out.println(getReference(o3) + " o3=" + o3);
	System.out.println("o1 == o2 " + (o1 == o2));
	System.out.println("o1 == o3 " + (o1 == o3));
	System.out.println("o1 equals o2 " + o1.equals(o2));
	System.out.println("o1 equals o3 " + o1.equals(o3));
	System.out.println("o1.hashCode() == o2.hashCode() " + (o1.hashCode() == o2.hashCode()));
	System.out.println("o1.hashCode() == o3.hashCode() " + (o1.hashCode() == o3.hashCode()));
	System.out.println();

	// reference type (== 연산자는 reference가 같으면 true)
	Person p1 = new Person("P",10);
	Person p2 = new Person("P",10);
	Person p3 = p1;
	System.out.println(getReference(p1) + " p1=" + p1);
	System.out.println(getReference(p2) + " p2=" + p2);
	System.out.println(getReference(p3) + " p3=" + p3);
	System.out.println("p1 == p2 " + (p1 == p2));
	System.out.println("p1 == p3 " + (p1 == p3));
	System.out.println("p1 equals p2 " + p1.equals(p2));
	System.out.println("p1 equals p3 " + p1.equals(p3));
	System.out.println("p1.hashCode() == p2.hashCode() " + (p1.hashCode() == p2.hashCode()));
	System.out.println("p1.hashCode() == p3.hashCode() " + (p1.hashCode() == p3.hashCode()));
	System.out.println();

	// String type (== 연산자는 reference가 같으면 true)
	String s1 = "PP"; // String literal을 사용할 경우, pool에서 관리
	String s2 = "PP"; // String literal을 사용할 경우, pool에서 관리
	String s3 = s1;
	String s4 = "P" + "P"; // String literal을 사용할 경우, pool에서 관리
	String s5 = new String("PP");
	String s6 = "PP" + ""; // String literal을 사용할 경우, pool에서 관리
	String s7 = s1 + "";
	System.out.println(getReference(s1) + " s1=" + s1);
	System.out.println(getReference(s2) + " s2=" + s2);
	System.out.println(getReference(s3) + " s3=" + s3);
	System.out.println(getReference(s4) + " s4=" + s4);
	System.out.println(getReference(s5) + " s5=" + s5);
	System.out.println(getReference(s6) + " s6=" + s6);
	System.out.println(getReference(s7) + " s7=" + s7);

	System.out.println("s1 == s2 " + (s1 == s2));
	System.out.println("s1 == s3 " + (s1 == s3));
	System.out.println("s1 == s4 " + (s1 == s4));
	System.out.println("s1 == s5 " + (s1 == s5));
	System.out.println("s1 == s6 " + (s1 == s6));
	System.out.println("s1 == s7 " + (s1 == s7));
	System.out.println("s1 equals s2 " + s1.equals(s2));
	System.out.println("s1 equals s3 " + s1.equals(s3));
	System.out.println("s1 equals s4 " + s1.equals(s4));
	System.out.println("s1 equals s5 " + s1.equals(s5));
	System.out.println("s1 equals s6 " + s1.equals(s6));
	System.out.println("s1 equals s7 " + s1.equals(s7));
	System.out.println("s1.hashCode() == s2.hashCode() " + (s1.hashCode() == s2.hashCode()));
	System.out.println("s1.hashCode() == s3.hashCode() " + (s1.hashCode() == s3.hashCode()));
	System.out.println("s1.hashCode() == s4.hashCode() " + (s1.hashCode() == s4.hashCode()));
	System.out.println("s1.hashCode() == s5.hashCode() " + (s1.hashCode() == s5.hashCode()));
	System.out.println("s1.hashCode() == s6.hashCode() " + (s1.hashCode() == s6.hashCode()));
	System.out.println("s1.hashCode() == s7.hashCode() " + (s1.hashCode() == s7.hashCode()));
	System.out.println();

	// ArrayList
	System.out.println("pList");
	List<Person> pList = new ArrayList<Person>();
	pList.add(p1);
	pList.add(p2);
	pList.add(p3);
	pList.forEach((p) -> System.out.println(p));
	System.out.println("pList contains p1: " + pList.contains(p1));
	System.out.println("pList contains p2: " + pList.contains(p2));
	System.out.println("pList contains p3: " + pList.contains(p3));
	System.out.println("pList contains new Person: " + pList.contains(new Person("P",10)));
	System.out.println();

	// HashSet의 경우 hashCode가 일치하면 동일한 것으로 간주하여 replace함
	System.out.println("pSet");
	Set<Person> pSet = new HashSet();
	pSet.add(p1);
	pSet.add(p2);
	pSet.add(p3);
	pSet.forEach((p) -> System.out.println(p));
	System.out.println("pSet contains p1: " + pSet.contains(p1));
	System.out.println("pSet contains p2: " + pSet.contains(p2));
	System.out.println("pSet contains p3: " + pSet.contains(p3));
	System.out.println("pSet contains new Person: " + pSet.contains(new Person("P",10)));
	System.out.println();

	// HashMap의 경우 key 값은 hashCode가 일치하면 동일한 것으로 간주하여 replace함
	System.out.println("pMap");
	Map<Person, Integer> pMap = new HashMap<Person, Integer>();
	pMap.put(p1, 1);
	pMap.put(p2, 2);
	pMap.put(p3, 3);
	pMap.forEach((p, e) -> System.out.println(p + " " + e));
	System.out.println("pMap contains p1: " + pMap.containsKey(p1));
	System.out.println("pMap contains p2: " + pMap.containsKey(p2));
	System.out.println("pMap contains p3: " + pMap.containsKey(p3));
	System.out.println("pMap contains new Person: " + pMap.containsKey(new Person("P",10)));
	System.out.println();

	// Array
	System.out.println("pArray");
	Person[] pArray = new Person[3];
	pArray[0] = p1;
	pArray[1] = p2;
	pArray[2] = p3;
	Arrays.asList(pArray).forEach(p -> System.out.println(p));
	System.out.println("pArray contains p1: " + Arrays.asList(pArray).contains(p1));
	System.out.println("pArray contains p2: " + Arrays.asList(pArray).contains(p2));
	System.out.println("pArray contains p3: " + Arrays.asList(pArray).contains(p3));
	System.out.println("pArray contains new Person: " + Arrays.asList(pArray).contains(new Person("P",10)));
	System.out.println();
    }
}

Array vs ArrayList

ArrayArrayList

int[] integerArray = new int[3];

integerArray[0] = 1;

integerArray[1] = 2;

integerArray[2] = 3;

for (int i : integerArray) System.out.println(i);

for (int j=0; j<integerArray.length; j++) System.out.println(integerArray[ j ]);

int k = 0;

while (k < integerArray.length) System.out.println(integerArray[k++]);

 

ArrayList<Integer> integerList = new ArrayList<Integer>();

integerList.add(1); //cannot store primitive in ArrayList, instead autoboxing will convert int to Integer object

integerList.add(2); //cannot store primitive in ArrayList, instead autoboxing will convert int to Integer object

integerList.add(3); //cannot store primitive in ArrayList, instead autoboxing will convert int to Integer object

for (int m : integerList) System.out.println(m);

for (int n=0; n<integerList.size(); n++) System.out.println(integerList.get(n));

Iterator<Integer> itr = integerList.iterator();

while (itr.hasNext()) System.out.println(itr.next());

Difference between Array and ArrayList

  • Resizable
    • Array is static in size that is fixed length data structure, One can not change the length after creating the Array object.
    • ArrayList is dynamic in size. Each ArrayList object has instance variable capacity which indicates the size of the ArrayList. Its capacity grows automatically.
  • Primitives
    • Array can contain both primitive data types (e.g. int, float, double) as well as objects.
    • ArrayList can not contains primitive data types it can only contains objects.
  • Adding elements
    • In array we insert elements using the assignment(=) operator.
    • We can insert elements into the ArrayList using the add() method
  • Length
    • Each array object has the length variable which returns the length of the array.
    • Length of the ArrayList is provided by the size() method.

// Array

int[] integerArray = new int[3];
integerArray[0] = 1;
integerArray[1] = 2;
integerArray[2] = 3;
for (int i : integerArray) System.out.println(i);
for (int j=0; j<integerArray.length; j++) System.out.println(integerArray[ j ]);
int k = 0;
while (k < integerArray.length) System.out.println(integerArray[k++]);

// ArrayList

ArrayList integerList = new ArrayList();
integerList.add(1); //cannot store primitive in ArrayList, instead autoboxing will convert int to Integer object
integerList.add(2); //cannot store primitive in ArrayList, instead autoboxing will convert int to Integer object
integerList.add(3); //cannot store primitive in ArrayList, instead autoboxing will convert int to Integer object
for (int m : integerList) System.out.println(m);
for (int n=0; n<integerList.size(); n++) System.out.println(integerList.get(n));
Iterator itr = integerList.iterator();
while (itr.hasNext()) System.out.println(itr.next());

Lab6

Lab6 – ImageProcessorGUI (Swing) (Due by 5/30)
본인이 작성한 Lab5를 GUI로 구현한다.

Lab6_template (ImageFade updated)

Lab6_0_Image

java1-lab6-ImageProcessorGUI

Lab6_template에 ImageProcessorPanel을 작성한다.

Lab6 프로젝트 디렉토리 안에 모든 파일(src/*.java & bin/*.class)와 보고서(3-4장정도 장수제한없음)를 넣고 Lab6_학번_이름.zip 압축한 후 e-learning (http://lms.dankook.ac.kr/index.jsp)으로 제출

ConvertMode는 TO_JPG, TO_PNG, TO_GIF 열거형 제공
ImageMode는 CONVERT, RESIZE, ROTATE, GRAYSCALE, BLUR, BRIGHTNESS_ADJUST, NEGATIVE, EDGE_DETECT , SHARPEN 열거형 제공
Photo 클래스는 이미지파일을 읽어서 이미지 버퍼(BufferedImage)로 저장
ImageProcessor 추상클래스
ImageBlur, ImageBrightnessAdjust, ImageConvert, ImageGrayscale, ImageResize, ImageRotate, ImageNegative, ImageEdgeDetect, ImageSharpen 클래스
MainFrame에서는 ImageProcessorPanel를 사용하여 각종 영상처리 사용자 인터랙션이 가능한 GUI를 제공한다.
-ImageProcessorPanel에서는 ImageBlur, ImageBrightnessAdjust 등 클래스를 이용하여 각종 이미지 변환을 수행하는 코드를 작성한다.
-본인이 원하는 코드를 추가작성한다

보고서의 내용은 기존 코드 분석과 이해한 내용 그리고 본인이 추가한 코드내용을 적는다.