From a6ff08fc3b8424c3435b7c5e6feeb35170710233 Mon Sep 17 00:00:00 2001 From: Hsuan-Hao Date: Thu, 29 Jun 2017 02:51:12 +0200 Subject: [PATCH] modify GUI code and add Test code which is from Ching Yi --- finalproject_Hao/BlackandWhite.java | 4 +- finalproject_Hao/ColorBlackWhiteConversionGUI.java | 26 +++++--- finalproject_Hao/GrayScale.java | 4 +- finalproject_Hao/LoadFile.java | 2 +- finalproject_Hao/SaveFile.java | 8 ++- finalproject_Hao/TestColor.java | 69 ++++++++++++++++++++++ 6 files changed, 98 insertions(+), 15 deletions(-) create mode 100644 finalproject_Hao/TestColor.java diff --git a/finalproject_Hao/BlackandWhite.java b/finalproject_Hao/BlackandWhite.java index 38f6808..1a046c7 100644 --- a/finalproject_Hao/BlackandWhite.java +++ b/finalproject_Hao/BlackandWhite.java @@ -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(); diff --git a/finalproject_Hao/ColorBlackWhiteConversionGUI.java b/finalproject_Hao/ColorBlackWhiteConversionGUI.java index 109c87c..2759411 100644 --- a/finalproject_Hao/ColorBlackWhiteConversionGUI.java +++ b/finalproject_Hao/ColorBlackWhiteConversionGUI.java @@ -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(); } diff --git a/finalproject_Hao/GrayScale.java b/finalproject_Hao/GrayScale.java index bf20c98..ac94bb4 100644 --- a/finalproject_Hao/GrayScale.java +++ b/finalproject_Hao/GrayScale.java @@ -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(); diff --git a/finalproject_Hao/LoadFile.java b/finalproject_Hao/LoadFile.java index 80c4e64..7f2f422 100644 --- a/finalproject_Hao/LoadFile.java +++ b/finalproject_Hao/LoadFile.java @@ -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(); } diff --git a/finalproject_Hao/SaveFile.java b/finalproject_Hao/SaveFile.java index 61c7224..439e910 100644 --- a/finalproject_Hao/SaveFile.java +++ b/finalproject_Hao/SaveFile.java @@ -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 index 0000000..23c085f --- /dev/null +++ b/finalproject_Hao/TestColor.java @@ -0,0 +1,69 @@ +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 image; + int width; + int height; + + @Before + public void setUp() throws Exception { + LoadFile file = new LoadFile(); + input_image = file.Load_File("test.jpg"); + + BlackandWhite bw_image = new BlackandWhite(); + output_image = bw_image.Black_and_White(input_image); + } + + /** + * @throws java.lang.Exception + */ + @After + public void tearDown() throws Exception { + } + + @Test + public void testBlackandWhite() { + + try { + + width = output_image.getWidth(); + height = output_image.getHeight(); + + for(int i=0; i