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();
public class ColorBlackWhiteConversionGUI
{
- private BufferedImage input_image;
- private BufferedImage output_image;
+ static BufferedImage input_image;
+ static BufferedImage output_image;
//for GUI
private JFrame frame;
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);
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;
}
else
{
+ output_image = null;
BlackandWhite bw_image = new BlackandWhite();
output_image = bw_image.Black_and_White(input_image);
else
{
+ output_image = null;
GrayScale g_image = new GrayScale();
output_image = g_image.Gray_Scale(input_image);
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;
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();
}
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();
}
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();
}
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, "jpg", ouptut); //save file
+ ImageIO.write(image, file_type, ouptut); //save file
}
catch (IOException e)
--- /dev/null
+import static org.junit.Assert.*;\r
+\r
+import java.awt.Color;\r
+import java.awt.image.BufferedImage;\r
+\r
+import org.junit.After;\r
+import org.junit.Before;\r
+import org.junit.Test;\r
+\r
+/**\r
+ * \r
+ */\r
+\r
+/**\r
+ * @author chingyichang\r
+ *\r
+ */\r
+public class TestColor {\r
+\r
+ BufferedImage input_image;\r
+ BufferedImage output_image;\r
+ BufferedImage image;\r
+ int width;\r
+ int height;\r
+ \r
+ @Before\r
+ public void setUp() throws Exception {\r
+ LoadFile file = new LoadFile();\r
+ input_image = file.Load_File("test.jpg");\r
+ \r
+ BlackandWhite bw_image = new BlackandWhite();\r
+ output_image = bw_image.Black_and_White(input_image);\r
+ }\r
+\r
+ /**\r
+ * @throws java.lang.Exception\r
+ */\r
+ @After\r
+ public void tearDown() throws Exception {\r
+ }\r
+\r
+ @Test\r
+ public void testBlackandWhite() {\r
+ \r
+ try {\r
+ \r
+ width = output_image.getWidth();\r
+ height = output_image.getHeight();\r
+ \r
+ for(int i=0; i<height; i++){\r
+ \r
+ for(int j=0; j<width; j++){\r
+ \r
+ Color c = new Color(output_image.getRGB(j, i));\r
+ \r
+ int red = (int)c.getRed();\r
+ int green = (int)c.getGreen();\r
+ int blue = (int)c.getBlue();\r
+ int average = (red+green+blue) / 3;\r
+ \r
+ assertTrue(average==0||average==255);\r
+ \r
+ }\r
+ } \r
+ } catch (Exception e) {}\r
+ \r
+ }\r
+\r
+}\r