modify GUI code
authorHsuan-Hao <perng781202@gmail.com>
Wed, 28 Jun 2017 22:02:12 +0000 (00:02 +0200)
committerHsuan-Hao <perng781202@gmail.com>
Wed, 28 Jun 2017 22:02:12 +0000 (00:02 +0200)
finalproject_Hao/ColorBlackWhiteConversionGUI.java [new file with mode: 0644]

diff --git a/finalproject_Hao/ColorBlackWhiteConversionGUI.java b/finalproject_Hao/ColorBlackWhiteConversionGUI.java
new file mode 100644 (file)
index 0000000..109c87c
--- /dev/null
@@ -0,0 +1,290 @@
+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 
+{
+       private BufferedImage input_image;
+       private 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);
+        }
+        
+        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
+        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
+               {
+                       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
+               {
+                       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();
+            
+            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 + "\'.");
+            
+            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
+               ColorBlackWhiteConversionGUI gui = new ColorBlackWhiteConversionGUI();
+               gui.run();
+       }
+}