--- /dev/null
+import java.awt.*;
+import java.awt.event.*;
+import javax.swing.*;
+import javax.swing.event.*;
+import java.awt.image.BufferedImage;
+import java.util.*;
+
+public class ColorBlackWhiteConversionGUI
+{
+ static BufferedImage input_image;
+ static BufferedImage output_image;
+
+ //for GUI
+ private JFrame frame;
+ private JPanel Input;
+ private JPanel Output;
+ private String[] name;
+ private int att[][];
+ private ArrayList<JComponent> GUIComponent;
+ private String userinput = "";
+
+ boolean occupied = false;
+ boolean saved = false;
+
+ ColorBlackWhiteConversionGUI()
+ {
+ 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:",
+ "hint...",
+ "Load",
+ "BW",
+ "Gray",
+ "Save",
+ "Quit"};
+
+ 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)
+ {0, 11, 7, 1, 0, 0, fill[3], anchor[5]}, //hint... (word)
+
+ //Image 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 button
+ {6, 6, 1, 1, 0, 0, fill[3], anchor[0]}, //Black and White button
+ {6, 7, 1, 1, 0, 0, fill[3], anchor[0]}, //Gray Scale button
+ {9, 10, 1, 1, 0, 0, fill[3], anchor[0]}, //Save file button
+ {9, 11, 1, 1, 0, 0, fill[2], anchor[0]}, //Quit button
+
+ //Text field
+ {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>(15);
+ }
+
+ public void run()
+ {
+ for (int i=0; i<3; i++)
+ {
+ JLabel nLabel = new JLabel(name[i]);
+ GUIComponent.add(nLabel);
+ }
+
+ JScrollPane scrollPane = new JScrollPane(new JLabel(new ImageIcon(input_image))); // put input_Image into label
+ Input.add(scrollPane);
+ GUIComponent.add(Input);
+
+ JScrollPane scrollPane2 = new JScrollPane(new JLabel(new ImageIcon(output_image))); //put output_Image into label
+ Output.add(scrollPane2);
+ GUIComponent.add(Output);
+
+ //add Buttons
+ for (int k=3; k<name.length; 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(5);
+ button.addActionListener(new LoadListener());
+
+ button = (JButton) GUIComponent.get(6);
+ button.addActionListener(new BlackWhiteListener());
+
+ button = (JButton) GUIComponent.get(7);
+ button.addActionListener(new GrayScaleListener());
+
+ button = (JButton) GUIComponent.get(8);
+ button.addActionListener(new SaveListener());
+
+ button = (JButton) GUIComponent.get(9);
+ button.addActionListener(new QuitListener());
+
+ 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(10);
+ userinput = inputText.getText();
+ LoadFile l_file = new LoadFile();
+
+ if(occupied == true)
+ {
+ int reply = JOptionPane.showConfirmDialog(null, "You have loaded an image,\n Do you want to chage?", "Change?", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
+
+ if (reply == JOptionPane.YES_OPTION)
+ {
+ input_image = l_file.Load_File(userinput);
+ }
+ }
+
+ else
+ {
+ input_image = l_file.Load_File(userinput);
+ }
+
+ if(input_image != null)
+ {
+ JLabel t1 = (JLabel) GUIComponent.get(2);
+ t1.setText("You have loaded \'" + userinput + "\'" + " file.");
+
+ occupied = true;
+ saved = false;
+ }
+
+ inputText.setText("");
+ }
+ }
+
+ class BlackWhiteListener implements ActionListener
+ {
+ public void actionPerformed(ActionEvent event)
+ {
+ if(occupied == false)
+ {
+ javax.swing.JOptionPane.showMessageDialog(null, "You haven't loaded any image");
+ }
+
+ else
+ {
+ output_image = null;
+ BlackandWhite bw_image = new BlackandWhite();
+ output_image = bw_image.Black_and_White(input_image);
+
+ JLabel t1 = (JLabel) GUIComponent.get(2);
+ t1.setText("You have changed the image to black and white.");
+
+ saved = false;
+ }
+ }
+ }
+
+ class GrayScaleListener implements ActionListener
+ {
+ public void actionPerformed(ActionEvent event)
+ {
+ if(occupied == false)
+ {
+ javax.swing.JOptionPane.showMessageDialog(null, "You haven't loaded any image");
+ }
+
+ else
+ {
+ output_image = null;
+ GrayScale g_image = new GrayScale();
+ output_image = g_image.Gray_Scale(input_image);
+
+ JLabel t1 = (JLabel) GUIComponent.get(2);
+ t1.setText("You have changed the image to gray scale.");
+
+ saved = false;
+ }
+ }
+ }
+
+ class SaveListener implements ActionListener
+ {
+ public void actionPerformed(ActionEvent event)
+ {
+ JTextField inputText = (JTextField) GUIComponent.get(11);
+ userinput = inputText.getText();
+
+ if(occupied == false)
+ {
+ javax.swing.JOptionPane.showMessageDialog(null, "You haven't loaded any image");
+ }
+
+ SaveFile s_file = new SaveFile();
+ s_file.Save_File(output_image, userinput);
+
+ JLabel t1 = (JLabel) GUIComponent.get(2);
+ t1.setText("You have saved the file and named \'" + userinput + "\'.");
+
+ saved = true;
+
+ inputText.setText("");
+ }
+ }
+
+ //Exits the program.
+ class QuitListener implements ActionListener
+ {
+ public void actionPerformed(ActionEvent event)
+ {
+ if (occupied == true && saved == false)
+ {
+ int reply = JOptionPane.showConfirmDialog(null, "You haven't saved your image,\n are you sure you want to close?", "Close?", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
+
+ if (reply == JOptionPane.YES_OPTION)
+ {
+ System.exit(0);
+ }
+ }
+
+ else
+ {
+ System.exit(0);
+ }
+ }
+ }
+
+ public static void main(String[] args)
+ {
+ // TODO Auto-generated method stub
+ LoadFile file = new LoadFile();
+ input_image = file.Load_File("Blank.jpg");
+ output_image = file.Load_File("Blank.jpg");
+
+ ColorBlackWhiteConversionGUI gui = new ColorBlackWhiteConversionGUI();
+ gui.run();
+ }
+}
--- /dev/null
+import static org.junit.Assert.*;
+
+import java.awt.Color;
+import java.awt.image.BufferedImage;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ *
+ */
+
+/**
+ * @author chingyichang
+ *
+ */
+public class TestColor {
+
+ BufferedImage input_image;
+ BufferedImage output_image;
+ BufferedImage output_image_gray;
+ BufferedImage image;
+ int width;
+ int height;
+
+ @Before
+ public void setUp() throws Exception {
+ LoadFile file = new LoadFile();
+ input_image = file.Load_File("test.jpg");
+
+ }
+
+ /**
+ * @throws java.lang.Exception
+ */
+ @After
+ public void tearDown() throws Exception {
+ }
+
+ @Test
+ public void testBlackandWhite() {
+ BlackandWhite bw_image = new BlackandWhite();
+ output_image = bw_image.Black_and_White(input_image);
+
+ try {
+
+ width = output_image.getWidth();
+ height = output_image.getHeight();
+
+ for(int i=0; i<height; i++){
+
+ for(int j=0; j<width; j++){
+
+ Color c = new Color(output_image.getRGB(j, i));
+
+ int red = (int)c.getRed();
+ int green = (int)c.getGreen();
+ int blue = (int)c.getBlue();
+ int average = (red+green+blue) / 3;
+
+ assertTrue(average==0||average==255);
+
+ }
+ }
+ } catch (Exception e) {}
+
+ }
+ @Test
+ public void testGray() {
+ GrayScale g_image = new GrayScale();
+ output_image_gray = g_image.Gray_Scale(input_image);
+ try {
+
+ width = output_image_gray.getWidth();
+ height = output_image_gray.getHeight();
+
+ for(int i=0; i<height; i++){
+
+ for(int j=0; j<width; j++){
+
+ Color c = new Color(output_image_gray.getRGB(j, i));
+
+ int red = (int)c.getRed();
+ int green = (int)c.getGreen();
+ int blue = (int)c.getBlue();
+
+ assertTrue((red==green)&&(red==blue));
+
+ }
+ }
+ } catch (Exception e) {}
+
+ }
+}