modify GUI code and add Test code which is from Ching Yi
authorHsuan-Hao <perng781202@gmail.com>
Thu, 29 Jun 2017 00:51:12 +0000 (02:51 +0200)
committerHsuan-Hao <perng781202@gmail.com>
Thu, 29 Jun 2017 00:51:12 +0000 (02:51 +0200)
finalproject_Hao/BlackandWhite.java
finalproject_Hao/ColorBlackWhiteConversionGUI.java
finalproject_Hao/GrayScale.java
finalproject_Hao/LoadFile.java
finalproject_Hao/SaveFile.java
finalproject_Hao/TestColor.java [new file with mode: 0644]

index 38f6808..1a046c7 100644 (file)
@@ -14,10 +14,10 @@ public class BlackandWhite {
           height = 0;
    }
    
-   public BufferedImage Black_and_White(BufferedImage image) {
+   public BufferedImage Black_and_White(BufferedImage input_image) {
         
       try {
-         this.image = image;
+         this.image = input_image;
          width = image.getWidth();
          height = image.getHeight();
          
index 109c87c..2759411 100644 (file)
@@ -7,8 +7,8 @@ import java.util.*;
 
 public class ColorBlackWhiteConversionGUI 
 {
-       private BufferedImage input_image;
-       private BufferedImage output_image;
+       static BufferedImage input_image;
+       static BufferedImage output_image;
     
     //for GUI
        private JFrame frame;
@@ -96,15 +96,11 @@ public class ColorBlackWhiteConversionGUI
             GUIComponent.add(nLabel);
         }
         
-        LoadFile file = new LoadFile();
-               input_image     = file.Load_File("test.jpg");
-               output_image = file.Load_File("Blank.jpg");
-          
         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
+        JScrollPane scrollPane2 = new JScrollPane(new JLabel(new ImageIcon(output_image))); //put output_Image into label
         Output.add(scrollPane2);
         GUIComponent.add(Output);
         
@@ -187,7 +183,8 @@ public class ColorBlackWhiteConversionGUI
             if(input_image != null)
             {
                JLabel t1 = (JLabel) GUIComponent.get(2);
-                t1.setText("You have loaded \'" + userinput + "\' file.");
+                t1.setText("You have loaded \'" + userinput + "\'" + " file.");
+                
                occupied = true;
                                saved = false;
             }
@@ -207,6 +204,7 @@ public class ColorBlackWhiteConversionGUI
                
                else
                {
+                       output_image = null;
                        BlackandWhite bw_image = new BlackandWhite();
                output_image = bw_image.Black_and_White(input_image);
                
@@ -229,6 +227,7 @@ public class ColorBlackWhiteConversionGUI
                
                else
                {
+                       output_image = null;
                        GrayScale g_image = new GrayScale();
                output_image = g_image.Gray_Scale(input_image);
                
@@ -247,11 +246,16 @@ public class ColorBlackWhiteConversionGUI
                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 nemed \'" + userinput + "\'.");
+            t1.setText("You have saved the file and named \'" + userinput + "\'.");
             
             saved = true;
             
@@ -284,6 +288,10 @@ public class ColorBlackWhiteConversionGUI
        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();
        }
index bf20c98..ac94bb4 100644 (file)
@@ -14,10 +14,10 @@ public class GrayScale {
           height = 0;
    }
    
-   public BufferedImage Gray_Scale(BufferedImage image) {
+   public BufferedImage Gray_Scale(BufferedImage input_image) {
    
       try {
-         this.image = image;
+         this.image = input_image;
          width = image.getWidth();
          height = image.getHeight();
          
index 80c4e64..7f2f422 100644 (file)
@@ -23,7 +23,7 @@ public class LoadFile
         }
         catch(Exception e)
         {
-               javax.swing.JOptionPane.showMessageDialog(null, "There doesn't have " + Filename + "file.");
+               javax.swing.JOptionPane.showMessageDialog(null, "There doesn't have " + Filename + " file.");
                image = null; //show wrong message
                e.printStackTrace();
         }
index 61c7224..439e910 100644 (file)
@@ -1,5 +1,6 @@
 import java.awt.image.BufferedImage;
 import java.io.*;
+import java.util.regex.Pattern;
 import javax.imageio.ImageIO;
 
 public class SaveFile 
@@ -7,10 +8,15 @@ public class SaveFile
        public void Save_File(BufferedImage image, String name)
        {
                File ouptut = new File(name);
+               
+               String[] parts = name.split(Pattern.quote("."));
+               String file_type = parts[1];
+               
+               //System.out.println(part2);
         
                try 
                {
-                       ImageIO.write(image, "jpg", ouptut); //save file
+                       ImageIO.write(image, file_type, ouptut); //save file    
                }
                
                catch (IOException e) 
diff --git a/finalproject_Hao/TestColor.java b/finalproject_Hao/TestColor.java
new file mode 100644 (file)
index 0000000..23c085f
--- /dev/null
@@ -0,0 +1,69 @@
+import static org.junit.Assert.*;\r
+\r
+import java.awt.Color;\r
+import java.awt.image.BufferedImage;\r
+\r
+import org.junit.After;\r
+import org.junit.Before;\r
+import org.junit.Test;\r
+\r
+/**\r
+ * \r
+ */\r
+\r
+/**\r
+ * @author chingyichang\r
+ *\r
+ */\r
+public class TestColor {\r
+\r
+       BufferedImage  input_image;\r
+       BufferedImage  output_image;\r
+       BufferedImage  image;\r
+       int width;\r
+       int height;\r
+       \r
+       @Before\r
+       public void setUp() throws Exception {\r
+               LoadFile file = new LoadFile();\r
+               input_image     = file.Load_File("test.jpg");\r
+       \r
+               BlackandWhite bw_image = new BlackandWhite();\r
+       output_image = bw_image.Black_and_White(input_image);\r
+       }\r
+\r
+       /**\r
+        * @throws java.lang.Exception\r
+        */\r
+       @After\r
+       public void tearDown() throws Exception {\r
+       }\r
+\r
+       @Test\r
+       public void testBlackandWhite() {\r
+               \r
+               try {\r
+        \r
+                width = output_image.getWidth();\r
+                height = output_image.getHeight();\r
+                \r
+                for(int i=0; i<height; i++){\r
+                \r
+                   for(int j=0; j<width; j++){\r
+                   \r
+                      Color c = new Color(output_image.getRGB(j, i));\r
+                      \r
+                      int red = (int)c.getRed();\r
+                      int green = (int)c.getGreen();\r
+                      int blue = (int)c.getBlue();\r
+                      int average = (red+green+blue) / 3;\r
+                  \r
+                      assertTrue(average==0||average==255);\r
+                  \r
+                   }\r
+                }  \r
+             } catch (Exception e) {}\r
+               \r
+       }\r
+\r
+}\r