updated the revision of b&W bug and display bug
authorgit <wangtsaichang@gmail.com>
Sun, 2 Jul 2017 21:38:10 +0000 (23:38 +0200)
committergit <wangtsaichang@gmail.com>
Sun, 2 Jul 2017 21:38:10 +0000 (23:38 +0200)
Revise_Version/BlackandWhite.java [new file with mode: 0644]
Revise_Version/ColorBlackWhiteConversionGUI.java [new file with mode: 0644]
Revise_Version/GrayScale.java [new file with mode: 0644]
Revise_Version/LoadFile.java [new file with mode: 0644]
Revise_Version/SaveFile.java [new file with mode: 0644]
Revise_Version/TestColor.java [new file with mode: 0644]

diff --git a/Revise_Version/BlackandWhite.java b/Revise_Version/BlackandWhite.java
new file mode 100644 (file)
index 0000000..c94a394
--- /dev/null
@@ -0,0 +1,59 @@
+import java.awt.*;
+import java.awt.image.BufferedImage;
+import java.awt.image.ColorModel;
+import java.awt.image.WritableRaster;
+
+public class BlackandWhite {
+
+   private BufferedImage image;
+   int width;
+   int height;
+   
+   /*public void BlackandWhite()
+   {
+          image = null;
+          width = 0;
+          height = 0;
+   }*/
+   
+   static BufferedImage deepCopy(BufferedImage bi) {
+          ColorModel cm = bi.getColorModel();
+          boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
+          WritableRaster raster = bi.copyData(null);
+          return new BufferedImage(cm, raster, isAlphaPremultiplied, null);
+        }
+   
+   public BufferedImage Black_and_White(BufferedImage input_image) {
+        
+      try {
+         image = deepCopy(input_image);
+         
+         width = image.getWidth();
+         height = image.getHeight();
+         
+         for(int i=0; i<height; i++){
+         
+            for(int j=0; j<width; j++){
+            
+               Color c = new Color(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;
+               Color newColor;
+             
+               if (average > 128){
+                   newColor = new Color(255,255,255);
+               }else{
+                   newColor = new Color(0,0,0);
+               }
+               
+               image.setRGB(j,i,newColor.getRGB());
+            }
+         }  
+      } catch (Exception e) {}
+      
+      return image;
+   }
+}
\ No newline at end of file
diff --git a/Revise_Version/ColorBlackWhiteConversionGUI.java b/Revise_Version/ColorBlackWhiteConversionGUI.java
new file mode 100644 (file)
index 0000000..376998b
--- /dev/null
@@ -0,0 +1,319 @@
+import java.awt.*;
+import java.awt.event.*;
+
+import javax.imageio.ImageIO;
+import javax.swing.*;
+import javax.swing.event.*;
+import java.awt.image.BufferedImage;
+import java.util.*;
+
+public class ColorBlackWhiteConversionGUI 
+{
+       public BufferedImage input_image;
+       //private BufferedImage temp_image;
+       public BufferedImage output_image;
+    
+    //for GUI
+       private JFrame frame;
+       private JPanel Input;
+       
+       private JLabel Jimage_input;
+       private JLabel Jimage_output;
+       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);
+        }
+        
+        Jimage_input = new JLabel(new ImageIcon(input_image)); // put input_Image into label
+        Input.add(Jimage_input);
+        GUIComponent.add(Input);
+        
+        Jimage_output = new JLabel(new ImageIcon(output_image)); //put output_Image into label
+        Output.add(Jimage_output);
+        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);
+        Input.setVisible(true);
+        Output.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);
+    }
+       
+       public 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);
+                                       Jimage_input.setIcon(new ImageIcon(input_image));
+                       }
+                       }
+            
+            else 
+               {
+               input_image     = l_file.Load_File(userinput);
+               Jimage_input.setIcon(new ImageIcon(input_image));
+               
+                       }
+            
+            if(input_image != null)
+            {
+               JLabel t1 = (JLabel) GUIComponent.get(2);
+                t1.setText("You have loaded \'" + userinput + "\'" + " file.");
+                
+               occupied = true;
+                               saved = false;
+            }
+            
+            inputText.setText("");
+        }
+    }
+       
+       public 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;
+                       BufferedImage temp_image = input_image;
+                       BlackandWhite bw_image = new BlackandWhite();
+                       
+               output_image = bw_image.Black_and_White(temp_image);
+               Jimage_output.setIcon(new ImageIcon(output_image));
+               
+               JLabel t1 = (JLabel) GUIComponent.get(2);
+                t1.setText("You have changed the image to black and white.");
+                
+               saved = false;
+               }
+        }
+    }
+       
+       public 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();
+                       BufferedImage temp_image = input_image;
+               output_image = g_image.Gray_Scale(temp_image);
+               Jimage_output.setIcon(new ImageIcon(output_image));
+
+               JLabel t1 = (JLabel) GUIComponent.get(2);
+                t1.setText("You have changed the image to gray scale.");
+               
+               saved = false;
+               }
+        }
+    }
+       
+       public 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.
+       public 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 void initial(){
+               LoadFile file = new LoadFile();
+               input_image     = file.Load_File("Blank.jpg");
+               output_image = file.Load_File("Blank.jpg");
+       }
+       
+       public static void main(String[] args) 
+       {
+               // TODO Auto-generated method stub
+               ColorBlackWhiteConversionGUI gui = new ColorBlackWhiteConversionGUI();
+               gui.initial();
+               gui.run();
+       }
+}
diff --git a/Revise_Version/GrayScale.java b/Revise_Version/GrayScale.java
new file mode 100644 (file)
index 0000000..501c272
--- /dev/null
@@ -0,0 +1,51 @@
+import java.awt.*;
+import java.awt.image.BufferedImage;
+import java.awt.image.ColorModel;
+import java.awt.image.WritableRaster;
+
+public class GrayScale {
+
+   private BufferedImage image;
+   int width;
+   int height;
+   
+   static BufferedImage deepCopy(BufferedImage bi) {
+          ColorModel cm = bi.getColorModel();
+          boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
+          WritableRaster raster = bi.copyData(null);
+          return new BufferedImage(cm, raster, isAlphaPremultiplied, null);
+        }
+   
+   public BufferedImage Gray_Scale(BufferedImage input_image) {
+   
+      try {
+        
+         
+         image = deepCopy(input_image);
+         width = image.getWidth();
+         height = image.getHeight();
+         
+         for(int i=0; i<height; i++){
+         
+            for(int j=0; j<width; j++){
+            
+               Color c = new Color(image.getRGB(j, i));
+               int red = (int)(c.getRed() * 0.299);
+               int green = (int)(c.getGreen() * 0.587);
+               int blue = (int)(c.getBlue() *0.114);
+               Color newColor = new Color(red+green+blue,red+green+blue,red+green+blue);
+               
+               image.setRGB(j,i,newColor.getRGB());
+            }
+         } 
+      } 
+      
+      catch (Exception e) 
+      {
+       // TODO Auto-generated catch block
+       e.printStackTrace();
+      }
+      
+      return image;
+   }
+}
\ No newline at end of file
diff --git a/Revise_Version/LoadFile.java b/Revise_Version/LoadFile.java
new file mode 100644 (file)
index 0000000..7f2f422
--- /dev/null
@@ -0,0 +1,33 @@
+import java.io.*;
+import javax.imageio.ImageIO;
+import java.awt.image.BufferedImage;
+
+public class LoadFile 
+{
+       String Filename;
+    BufferedImage image;
+    
+    public LoadFile()
+    {
+       Filename = "";
+       image = null;
+    }
+       
+       public BufferedImage Load_File(String name)
+    {
+               Filename = name; //set filename
+            
+               try
+        {
+                       image = ImageIO.read(new File(Filename)); //load file
+        }
+        catch(Exception e)
+        {
+               javax.swing.JOptionPane.showMessageDialog(null, "There doesn't have " + Filename + " file.");
+               image = null; //show wrong message
+               e.printStackTrace();
+        }
+               
+               return image;
+    }
+}
diff --git a/Revise_Version/SaveFile.java b/Revise_Version/SaveFile.java
new file mode 100644 (file)
index 0000000..439e910
--- /dev/null
@@ -0,0 +1,28 @@
+import java.awt.image.BufferedImage;
+import java.io.*;
+import java.util.regex.Pattern;
+import javax.imageio.ImageIO;
+
+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, file_type, ouptut); //save file    
+               }
+               
+               catch (IOException e) 
+               {
+                       // TODO Auto-generated catch block
+                       e.printStackTrace();
+               }
+       }
+}
diff --git a/Revise_Version/TestColor.java b/Revise_Version/TestColor.java
new file mode 100644 (file)
index 0000000..7f1fbe6
--- /dev/null
@@ -0,0 +1,95 @@
+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) {}
+               
+       }
+}