--- /dev/null
+import java.awt.*;
+import java.awt.event.*;
+import javax.swing.*;
+import javax.swing.event.*;
+import java.io.*;
+import javax.imageio.ImageIO;
+import java.awt.image.BufferedImage;
+import java.util.*;
+
+public class ColorBlackWhiteConversion
+{
+ String Filename;
+ static BufferedImage input_image;
+ static BufferedImage output_image;
+
+ //for GUI
+ private JPanel Output;
+ private JPanel Input;
+ private JFrame frame;
+ private String[] name;
+ private int att[][];
+ private ArrayList<JComponent> GUIComponent;
+ private String userinput = "";
+ //private String result = "";
+
+ ColorBlackWhiteConversion()
+ {
+ int fill[] = { GridBagConstraints.BOTH,
+ GridBagConstraints.VERTICAL,
+ GridBagConstraints.HORIZONTAL,
+ GridBagConstraints.NONE};
+
+ int anchor[] = {GridBagConstraints.CENTER,
+ GridBagConstraints.EAST,
+ GridBagConstraints.SOUTHEAST,
+ GridBagConstraints.SOUTH,
+ GridBagConstraints.SOUTHWEST,
+ GridBagConstraints.WEST,
+ GridBagConstraints.NORTHWEST,
+ GridBagConstraints.NORTH,
+ GridBagConstraints.NORTHEAST};
+
+ String n[] = { "Input:",
+ "Output:",
+ "Load",
+ "BW",
+ "Gray",
+ "Save",};
+
+ name = n;
+
+ //x, y, width, height, weight-x, weight-y, GridBagConstraints.fill, GridBagConstraints.anchor
+ int a[][] = {{0, 0, 1, 1, 0, 0, fill[3], anchor[5]}, //input (word)
+ {9, 0, 1, 1, 0, 0, fill[3], anchor[5]}, //output (word)
+
+ //Text field
+ {0, 1, 4, 8, 0, 0, fill[3], anchor[5]}, //input image
+ {9, 1, 4, 8, 0, 0, fill[3], anchor[1]}, //output image
+
+ //Buttons
+ {0, 10, 1, 1, 0, 0, fill[3], anchor[0]}, //Load file
+ {6, 6, 1, 1, 0, 0, fill[3], anchor[0]}, //Black and White
+ {6, 7, 1, 1, 0, 0, fill[3], anchor[0]}, //Gray Scale
+ {9, 10, 1, 1, 0, 0, fill[3], anchor[0]}, //Save file
+ {1, 10, 3, 1, 0, 0, fill[2], anchor[0]}, //Load file text
+ {10, 10, 3, 1, 0, 0, fill[2], anchor[0]}}; //Save file text
+
+ att = a;
+
+ frame = new JFrame();
+ frame.setTitle("Color Black White Conversion");
+ frame.setSize(1200, 500);
+ frame.setLayout(new GridBagLayout());
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ Output = new JPanel();
+ Input = new JPanel();
+ Input.setBackground(Color.RED);
+ Output.setBackground(Color.WHITE);
+
+ GUIComponent = new ArrayList<JComponent>(10);
+ }
+
+ public void run()
+ {
+ for (int i=0; i<2; i++)
+ {
+ JLabel nLabel = new JLabel(name[i]);
+ GUIComponent.add(nLabel);
+ }
+
+
+ JScrollPane scrollPane = new JScrollPane(new JLabel(new ImageIcon(input_image)));//把Image放進label裡
+ Input.add(scrollPane);
+ GUIComponent.add(Input);
+
+ JScrollPane scrollPane2 = new JScrollPane(new JLabel(new ImageIcon(output_image)));//把Image放進label裡
+ Output.add(scrollPane2);
+ GUIComponent.add(Output);
+
+ //add Buttons
+ for (int k=2; k<6; k++)
+ {
+ JButton nButton = new JButton(name[k]);
+ GUIComponent.add(nButton);
+ }
+
+ JTextField Load_text = new JTextField("");
+ GUIComponent.add(Load_text);
+
+ JTextField Save_text = new JTextField("");
+ GUIComponent.add(Save_text);
+
+ for (int l=0; l<GUIComponent.size(); l++)
+ {
+ addComponent(l);
+ }
+
+ JButton
+ button = (JButton) GUIComponent.get(4);
+ button.addActionListener(new LoadListener());
+
+ button = (JButton) GUIComponent.get(5);
+ button.addActionListener(new BlackWhiteListener());
+
+ button = (JButton) GUIComponent.get(6);
+ button.addActionListener(new GrayScaleListener());
+
+ button = (JButton) GUIComponent.get(7);
+ button.addActionListener(new SaveListener());
+
+ frame.setVisible(true);
+ }
+
+ private void addComponent(int i)
+ {
+ GridBagConstraints c = new GridBagConstraints();
+ int a[] = att[i];
+
+ c.gridx = a[0];
+ c.gridy = a[1];
+ c.gridwidth = a[2];
+ c.gridheight = a[3];
+ c.weightx = a[4];
+ c.weighty = a[5];
+ c.fill = a[6];
+ c.anchor = a[7];
+ frame.add(GUIComponent.get(i), c);
+ }
+
+ class LoadListener implements ActionListener
+ {
+ public void actionPerformed(ActionEvent event)
+ {
+ JTextField inputText = (JTextField) GUIComponent.get(8);
+ userinput = inputText.getText();
+ LoadFile l_file = new LoadFile();
+ input_image = l_file.Load_File(userinput);
+
+ inputText.setText("");
+ }
+ }
+
+ class BlackWhiteListener implements ActionListener
+ {
+ public void actionPerformed(ActionEvent event)
+ {
+ BlackandWhite bw_image = new BlackandWhite();
+ output_image = bw_image.Black_and_White(input_image);
+ }
+ }
+
+ class GrayScaleListener implements ActionListener
+ {
+ public void actionPerformed(ActionEvent event)
+ {
+ GrayScale g_image = new GrayScale();
+ output_image = g_image.Gray_Scale(input_image);
+ }
+ }
+
+ class SaveListener implements ActionListener
+ {
+ public void actionPerformed(ActionEvent event)
+ {
+ JTextField inputText = (JTextField) GUIComponent.get(9);
+ userinput = inputText.getText();
+
+ SaveFile s_file = new SaveFile();
+ s_file.Save_File(output_image, userinput);
+
+ inputText.setText("");
+ }
+ }
+
+ public static void main(String[] args)
+ {
+ // TODO Auto-generated method stub
+ //BlackandWhite obj = new BlackandWhite();
+ //GrayScale obj_bw = new GrayScale();
+
+ LoadFile file = new LoadFile();
+ input_image = file.Load_File("Blank.jpg");
+ output_image = file.Load_File("Blank.jpg");
+
+ ColorBlackWhiteConversion gui = new ColorBlackWhiteConversion();
+ gui.run();
+ }
+
+
+}