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 등 클래스를 이용하여 각종 이미지 변환을 수행하는 코드를 작성한다.
-본인이 원하는 코드를 추가작성한다

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

BMICALCULATOR

표준체중계산기 (BMI Calculator)
BMIFrame
-label, button, radiobutton, textfield

– 표준체중
// 남자표준체중 = 신장(m) x 신장(m) x 22
// 여자표준체중 = 신장(m) x 신장(m) x 21

– BMI
// BMI = 체중(kg)/신장^2(m^2)
// 20 미만 저체중 (Underweight)
// 20~24 정상 (Normal)
// 25~29 과체중 (Overweight)
// 30 이상 비만 (Obesity)

– 하루 권장 섭취량
// 한국인 하루권장섭취량 남성 2600 kcal 여성 2100 kcal
// 기초대사량산출공식 (Herris Benedict Equation)
// 기초대사량(남성) = 66.47 + (13.75*weight) + (5*height) – (6.76*age)
// 기초대사량(여성) = 655.1 + (9.05*weight) + (1.85*height) – (4.68*age)
// 활동대사량 = 저활동적(1.3)/중간활동적(1.5)/활동적(1.75)
// 개인별 하루권장섭취량(kcal) = 기초대사량 * 활동대사량


enum BMI {
    UNDERWEIGHT, NORMAL, OVERWEIGHT, OBESITY
}

enum Gender {
    FEMALE, MALE
}

enum Activity {
    LOW, MEDIUM, HIGH;

    public static String[] names() {
        String[] names = new String[Activity.values().length];
        for (Activity t : Activity.values())
            names[t.ordinal()] = t.toString();
        return names;
    }

    public static Activity valueOf(int value) {
        switch(value) {
        case 0: return LOW;
        case 1: return MEDIUM;
        case 2: return HIGH;
        default: return null;
        }
    }
}

public class Person {
    protected String name;
    protected int age;
    protected Gender gender;

    public Person() { 
        this("", 0, Gender.FEMALE); 
    }

    public Person(String name, int age, Gender gender) {
        this.name = name;
        this.age = age;
        this.gender = gender;
    }

    public void setName(String name) { 
        this.name = name; 
    }
    
    public void setAge(int age) { 
	this.age = age; 
    }
    
    public void setGender(Gender gender) { 
	this.gender = gender; 
    }

    public String getName() { 
	return name; 
    }

    public int getAge() { 
 	return age; 
    }

    public Gender getGender() { 
 	return gender; 
    }

    public void print() { 
	System.out.println(this); 
    }

    @Override 
    public String toString() { 
	return "Person Name=" + name + " Age=" + age + " Gender=" + gender; 
    }
}

public class BMICalculator extends Person {
    protected double height;
    protected double weight;
    protected Activity activity;

    public BMICalculator() {
        this("J", 20, Gender.FEMALE, 165.0, 55.0, Activity.MEDIUM);
    }

    public BMICalculator(String name, int age, Gender gender, double height, double weight, Activity activity) {
        super(name, age, gender);
        this.height = height;
        this.weight = weight;
        this.activity = activity;
    }

    public void setHeight(double height) {
	this.height = height;	
    }
    
    public void setWeight(double weight) {
	this.weight = weight;
    }

    public void setActivity(Activity activity) {
        this.activity = activity;	
    }
    
    public double getHeight() {
	return height;
    }

    public double getWeight() {
	return weight;
    }

    public Activity getActivity() {
	return activity;
    }

    public BMI calculateBMI() {
        double bmi = weight / (height * 0.01) / (height * 0.01);
        if (bmi >= 0.0 && bmi < 20.0) return BMI.UNDERWEIGHT;
        else if (bmi >= 20.0 && bmi < 24.0) return BMI.NORMAL;
        else if (bmi >= 24.0 && bmi < 30.0) return BMI.OVERWEIGHT;
        else return BMI.OBESITY;
    }

    public double calculateStandardWeight() {
        if (this.gender == Gender.MALE)
            return height * height * 22 * 0.0001;
        return height * height * 21 * 0.0001;
    }

    // Herris Benedict Equation
    public double calculateDailyCaloryIntake() {
        double activityCalory = 1.0;
        switch (this.activity) {
        case LOW:
            activityCalory = 1.3;
            break;
        case MEDIUM:
            activityCalory = 1.5;
            break;
        case HIGH:
            activityCalory = 1.75;
            break;
        default:
            break;
        }

        if (this.gender == Gender.MALE)
            return 66.47 + (13.75 * this.weight) + (5.0 * this.height) - (6.76 * (double)(this.age)) * activityCalory;
        return 655.1 + (9.05 * this.weight) + (1.85 * this.height) - (4.68 * (double)(this.age)) * activityCalory;
    }

    @Override 
    public String toString() { 
	return "BMICalculator Name=" + name + " Age=" + age + " Gender=" + gender + " Height=" + height + " Weight=" + weight + " BMI=" + calculateBMI(); 
    }
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class BMICalculatorFrame extends JFrame implements ActionListener, KeyListener {
    JLabel label1 = new JLabel("나이"); // 레이블 초기화
    JLabel label2 = new JLabel("키(cm)"); // 레이블 초기화
    JLabel label3 = new JLabel("몸무게(kg)"); // 레이블 초기화
    JLabel label4 = new JLabel("성별"); // 레이블 초기화
    JLabel label5 = new JLabel("활동량"); // 레이블 초기화
    JTextField textfield1 = new JTextField(10); // age 텍스트필드 초기화
    JTextField textfield2 = new JTextField(10); // height 텍스트필드 초기화
    JTextField textfield3 = new JTextField(10); // weight 텍스트필드 초기화
    JRadioButton rbutton1 = new JRadioButton("여자", true); // gender 라디오버튼
    JRadioButton rbutton2 = new JRadioButton("남자"); // gender 라디오버튼
    JComboBox<String> combobox1 = new JComboBox<String>(Activity.names()); // acitivity 콤보박스

    JButton button1 = new JButton("계산"); // bmi calculator 버튼 초기화

    JLabel label6 = new JLabel("BMI 지수"); // 레이블 초기화
    JLabel label7 = new JLabel("정상 몸무게"); // 레이블 초기화
    JLabel label8 = new JLabel("일일 칼로리섭취량"); // 레이블 초기화
    JTextField textfield4 = new JTextField(10); // bmi 텍스트필드 초기화
    JTextField textfield5 = new JTextField(10); // standard weight 텍스트필드 초기화
    JTextField textfield6 = new JTextField(10); // dailry calory intake 텍스트필드 초기화

    JPanel panel = new JPanel(); // 패널 초기화
    JPanel panel1 = new JPanel(new GridLayout(0, 2)); // 패널 초기화
    JPanel panel2 = new JPanel(new GridLayout(0, 2)); // 패널 초기화
    JPanel panel3 = new JPanel(new GridLayout(0, 2)); // 패널 초기화
    JPanel panel4 = new JPanel(new GridLayout(0, 2)); // 패널 초기화
    JPanel panel5 = new JPanel(); // 패널 초기화
    JPanel panel6 = new JPanel(new GridLayout(0, 2)); // 패널 초기화
    JPanel panel7 = new JPanel(); // 패널 초기화
    JPanel panel8 = new JPanel(new GridLayout(0, 2)); // 패널 초기화
    JPanel panel9 = new JPanel(new GridLayout(0, 2)); // 패널 초기화
    JPanel panel10 = new JPanel(new GridLayout(0, 2)); // 패널 초기화

    public BMICalculatorFrame() {
        super("BMICalculatorFrame");

        // frame
        panel1.add(label1);
        panel1.add(textfield1);

        panel2.add(label2);
        panel2.add(textfield2);

        panel3.add(label3);
        panel3.add(textfield3);

        panel5.add(rbutton1);
        panel5.add(rbutton2);
        panel4.add(label4);
        panel4.add(panel5);

        panel6.add(label5);
        panel6.add(combobox1);

        panel7.add(button1);

        panel8.add(label6);
        panel8.add(textfield4);

        panel9.add(label7);
        panel9.add(textfield5);

        panel10.add(label8);
        panel10.add(textfield6);

        panel.add(panel1);
        panel.add(panel2);
        panel.add(panel3);
        panel.add(panel4);
        panel.add(panel6);
        panel.add(panel7);
        panel.add(panel8);
        panel.add(panel9);
        panel.add(panel10);
        add(panel);

        // event
        textfield1.addKeyListener(this);
        textfield2.addKeyListener(this);
        textfield3.addKeyListener(this);
        button1.addActionListener(this);

        setSize(350, 300);
        setResizable(false);
        setVisible(true);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    private Gender selectGender() {
        if (rbutton1.isSelected() return Gender.FEMALE;
        return Gender.MALE;
    }

    private Activity selectActivity() {
        return Activity.valueOf(combobox1.getSelectedIndex());
    }

    private void calculateStandardWeightAndBMI() {
        calculator.age = Integer.parseInt(textfield1.getText());
        calculator.height = Double.parseDouble(textfield2.getText());
        calculator.weight = Double.parseDouble(textfield3.getText());
        calculator.gender = selectGender();
        calculator.activity = selectActivity();
        System.out.println("age=" + calculator.age);
        System.out.println("height=" + calculator.height);
        System.out.println("weight=" + calculator.weight);
        System.out.println("gender=" + calculator.gender);
        System.out.println("activity=" + calculator.activity);
        BMI bmi = calculator.calculateBMI();
        double sw = calculator.calculateStandardWeight();
        double dailyIntake = calculator.calculateDailyCaloryIntake();
        textfield4.setText("" + bmi);
        textfield5.setText("" + sw);
        textfield6.setText("" + dailyIntake);
    }

    public void actionPerformed(ActionEvent e) {
        JButton button = (JButton) e.getSource();
        if (button == button1) {
            calculateStandardWeightAndBMI(); // 계산
        }
    }
    
    public void keyTyped(KeyEvent e) {}
    public void keyReleased(KeyEvent e) {}
    public void keyPressed(KeyEvent e) {
        int key = e.getKeyCode();
        if (key == KeyEvent.VK_ENTER) {
            Toolkit.getDefaultToolkit().beep(); 
            System.out.println("ENTER pressed");
            calculateStandardWeightAndBMI(); // 계산
        }
    }

    public static void main(String[] args) {
        new BMICalculatorFrame();
    }
}

ARIDITY INDEX

건조지수(Aridity Index)를 구하고, 기후의 상태를 알려주는 프로그램을 작성한다.
AridityIndexFrame

(1) 사용자에게 연강수량(Precipitation)과 연평균기온(Temperature)를 입력 받는다. 연강수량의 단위는 mm이고, 연평균기온의 단위는 C이다.

(2) 건조지수를 다음 공식으로 계산하고 결과를 화면에 출력한다.
건조지수 De Martonne Aridity Index = Precipitation/ (Temperature + 10)

(3) 다음 기준에 따라 기후의 상태를 판단해서 화면에 출력한다.
AI >= 60 => Perhumid
30 <= AI < 60 => Humid
20 <= AI < 30 => SubHumid
15 <= AI < 20 => SemiArid
5 <= AI < 15 => Arid
AI < 5 => ExtremelyArid

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

//Class: AridityIndexCalculator
public class AridityIndexFrame extends JFrame implements ActionListener, KeyListener {
    JLabel label1 = new JLabel("연평균 온도 (C)"); // 레이블 초기화
    JLabel label2 = new JLabel("강수량 (mm)"); // 레이블 초기화
    JLabel label3 = new JLabel("건조지수 (LA)"); // 레이블 초기화

    JTextField textfield1 = new JTextField("0", 20); // 텍스트필드 초기화
    JTextField textfield2 = new JTextField("0", 20); // 텍스트필드 초기화
    JTextField textfield3 = new JTextField(20); // 텍스트필드 초기화

    JButton button1 = new JButton("확인"); // 버튼 초기화

    JPanel panel = new JPanel(); // 패널 초기화
    JPanel panel1 = new JPanel(); // 패널 초기화
    JPanel panel2 = new JPanel(); // 패널 초기화
    JPanel panel3 = new JPanel(); // 패널 초기화

    public AridityIndexFrame() {
        super("Aridity Index Frame");

        // frame
        panel1.add(label1);
        panel1.add(textfield1);

        panel2.add(label2);
        panel2.add(textfield2);

        panel3.add(label3);
        panel3.add(textfield3);

        panel.add(panel1);
        panel.add(panel2);
        panel.add(panel3);
        panel.add(button1);
        add(panel);

        // event
        textfield1.addKeyListener(this);
        textfield2.addKeyListener(this);
        button1.addActionListener(this);

        setSize(350, 200);
        setResizable(false);
        setVisible(true);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    private double ToDouble(String str) {
        return Double.parseDouble(str);
    }

    public void actionPerformed(ActionEvent e) {
        JButton button = (JButton) e.getSource();
        if (button == button1) {
            this.dispose();
        }
    }

    public void keyTyped(KeyEvent e) {}
    public void keyReleased(KeyEvent e) {}
    public void keyPressed(KeyEvent e) {
        int key = e.getKeyCode();
        if (key == KeyEvent.VK_ENTER) {
            Toolkit.getDefaultToolkit().beep();
            System.out.println("ENTER pressed");
            JTextField textfield = (JTextField) e.getSource();
            double T = ToDouble(textfield1.getText());
            double P = ToDouble(textfield2.getText());
            textfield3.setText("" + AridityIndexCalculator.Martonne(T, P));
        }
    }

    public static void main(String[] args) {
        new AridityIndexFrame();
    }
}