modify GUI code
authorHsuan-Hao <perng781202@gmail.com>
Wed, 28 Jun 2017 22:02:51 +0000 (00:02 +0200)
committerHsuan-Hao <perng781202@gmail.com>
Wed, 28 Jun 2017 22:02:51 +0000 (00:02 +0200)
finalproject_Hao/BlackandWhite.java
finalproject_Hao/ColorBlackWhiteConversion.java [deleted file]
finalproject_Hao/GrayScale.java
finalproject_Hao/LoadFile.java
finalproject_Hao/SaveFile.java

index 299225c..38f6808 100644 (file)
@@ -3,10 +3,17 @@ import java.awt.image.BufferedImage;
 
 public class BlackandWhite {
 
-   BufferedImage  image;
+   BufferedImage image;
    int width;
    int height;
    
+   public BlackandWhite()
+   {
+          image = null;
+          width = 0;
+          height = 0;
+   }
+   
    public BufferedImage Black_and_White(BufferedImage image) {
         
       try {
diff --git a/finalproject_Hao/ColorBlackWhiteConversion.java b/finalproject_Hao/ColorBlackWhiteConversion.java
deleted file mode 100644 (file)
index 6cc3dfd..0000000
+++ /dev/null
@@ -1,212 +0,0 @@
-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();
-       }
-       
-       
-}
index ab48562..bf20c98 100644 (file)
@@ -3,10 +3,17 @@ import java.awt.image.BufferedImage;
 
 public class GrayScale {
 
-   BufferedImage  image;
+   BufferedImage image;
    int width;
    int height;
    
+   public GrayScale()
+   {
+          image = null;
+          width = 0;
+          height = 0;
+   }
+   
    public BufferedImage Gray_Scale(BufferedImage image) {
    
       try {
index 13b8ffb..80c4e64 100644 (file)
@@ -6,19 +6,26 @@ public class LoadFile
 {
        String Filename;
     BufferedImage image;
+    
+    public LoadFile()
+    {
+       Filename = "";
+       image = null;
+    }
        
        public BufferedImage Load_File(String name)
     {
-               Filename = name; //設定檔名
+               Filename = name; //set filename
             
                try
         {
-                       image = ImageIO.read(new File(Filename)); //讀取檔案
+                       image = ImageIO.read(new File(Filename)); //load file
         }
         catch(Exception e)
         {
-               javax.swing.JOptionPane.showMessageDialog(null, "Wrong File: " + Filename);
-               image = null; //如果錯誤的話顯示錯誤訊息
+               javax.swing.JOptionPane.showMessageDialog(null, "There doesn't have " + Filename + "file.");
+               image = null; //show wrong message
+               e.printStackTrace();
         }
                
                return image;
index 314caa9..61c7224 100644 (file)
@@ -3,15 +3,16 @@ 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);
-               } 
+                       ImageIO.write(image, "jpg", ouptut); //save file
+               }
+               
                catch (IOException e) 
                {
                        // TODO Auto-generated catch block