add GUI version
authorHsuan-Hao <perng781202@gmail.com>
Wed, 28 Jun 2017 20:11:28 +0000 (22:11 +0200)
committerHsuan-Hao <perng781202@gmail.com>
Wed, 28 Jun 2017 20:11:28 +0000 (22:11 +0200)
finalproject_Hao/BlackandWhite.java [new file with mode: 0644]
finalproject_Hao/Blank.jpg [new file with mode: 0644]
finalproject_Hao/ColorBlackWhiteConversion.java [new file with mode: 0644]
finalproject_Hao/GrayScale.java [new file with mode: 0644]
finalproject_Hao/LoadFile.java [new file with mode: 0644]
finalproject_Hao/SaveFile.java [new file with mode: 0644]
finalproject_Hao/test.jpg [new file with mode: 0644]

diff --git a/finalproject_Hao/BlackandWhite.java b/finalproject_Hao/BlackandWhite.java
new file mode 100644 (file)
index 0000000..299225c
--- /dev/null
@@ -0,0 +1,42 @@
+import java.awt.*;
+import java.awt.image.BufferedImage;
+
+public class BlackandWhite {
+
+   BufferedImage  image;
+   int width;
+   int height;
+   
+   public BufferedImage Black_and_White(BufferedImage image) {
+        
+      try {
+         this.image = 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/finalproject_Hao/Blank.jpg b/finalproject_Hao/Blank.jpg
new file mode 100644 (file)
index 0000000..2590666
Binary files /dev/null and b/finalproject_Hao/Blank.jpg differ
diff --git a/finalproject_Hao/ColorBlackWhiteConversion.java b/finalproject_Hao/ColorBlackWhiteConversion.java
new file mode 100644 (file)
index 0000000..6cc3dfd
--- /dev/null
@@ -0,0 +1,212 @@
+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();
+       }
+       
+       
+}
diff --git a/finalproject_Hao/GrayScale.java b/finalproject_Hao/GrayScale.java
new file mode 100644 (file)
index 0000000..ab48562
--- /dev/null
@@ -0,0 +1,40 @@
+import java.awt.*;
+import java.awt.image.BufferedImage;
+
+public class GrayScale {
+
+   BufferedImage  image;
+   int width;
+   int height;
+   
+   public BufferedImage Gray_Scale(BufferedImage image) {
+   
+      try {
+         this.image = 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/finalproject_Hao/LoadFile.java b/finalproject_Hao/LoadFile.java
new file mode 100644 (file)
index 0000000..13b8ffb
--- /dev/null
@@ -0,0 +1,26 @@
+import java.io.*;
+import javax.imageio.ImageIO;
+import java.awt.image.BufferedImage;
+
+public class LoadFile 
+{
+       String Filename;
+    BufferedImage image;
+       
+       public BufferedImage Load_File(String name)
+    {
+               Filename = name; //設定檔名
+            
+               try
+        {
+                       image = ImageIO.read(new File(Filename)); //讀取檔案
+        }
+        catch(Exception e)
+        {
+               javax.swing.JOptionPane.showMessageDialog(null, "Wrong File: " + Filename);
+               image = null; //如果錯誤的話顯示錯誤訊息
+        }
+               
+               return image;
+    }
+}
diff --git a/finalproject_Hao/SaveFile.java b/finalproject_Hao/SaveFile.java
new file mode 100644 (file)
index 0000000..314caa9
--- /dev/null
@@ -0,0 +1,21 @@
+import java.awt.image.BufferedImage;
+import java.io.*;
+import javax.imageio.ImageIO;
+
+public class SaveFile 
+{
+       public void Save_File(BufferedImage image, String name)
+       {
+               File ouptut = new File(name);
+        
+               try 
+               {
+                       ImageIO.write(image, "jpg", ouptut);
+               } 
+               catch (IOException e) 
+               {
+                       // TODO Auto-generated catch block
+                       e.printStackTrace();
+               }
+       }
+}
diff --git a/finalproject_Hao/test.jpg b/finalproject_Hao/test.jpg
new file mode 100644 (file)
index 0000000..be810ad
Binary files /dev/null and b/finalproject_Hao/test.jpg differ