diff options
| author | Alexander Pickering <alexandermpickering@gmail.com> | 2017-02-06 11:41:36 -0500 |
|---|---|---|
| committer | Alexander Pickering <alexandermpickering@gmail.com> | 2017-02-06 11:41:36 -0500 |
| commit | 89cdf3efb49335e7c07a68a5a64657eeec2288a6 (patch) | |
| tree | cdc0fd8165e65b1637fa54cac11c932acefc8a89 /labs/lab02_simpleRGB | |
| download | coe0445-89cdf3efb49335e7c07a68a5a64657eeec2288a6.tar.gz coe0445-89cdf3efb49335e7c07a68a5a64657eeec2288a6.tar.bz2 coe0445-89cdf3efb49335e7c07a68a5a64657eeec2288a6.zip | |
Diffstat (limited to 'labs/lab02_simpleRGB')
| -rw-r--r-- | labs/lab02_simpleRGB/ImageViewer.java | 85 | ||||
| -rw-r--r-- | labs/lab02_simpleRGB/RGBComponent.java | 46 | ||||
| -rw-r--r-- | labs/lab02_simpleRGB/SimpleRGB.java | 190 | ||||
| -rw-r--r-- | labs/lab02_simpleRGB/SimpleRGBTester.java | 228 | ||||
| -rw-r--r-- | labs/lab02_simpleRGB/lab02_simpleRGB.pdf | bin | 0 -> 358046 bytes | |||
| -rw-r--r-- | labs/lab02_simpleRGB/pgh_640x480.jpg | bin | 0 -> 306595 bytes |
6 files changed, 549 insertions, 0 deletions
diff --git a/labs/lab02_simpleRGB/ImageViewer.java b/labs/lab02_simpleRGB/ImageViewer.java new file mode 100644 index 0000000..ecab621 --- /dev/null +++ b/labs/lab02_simpleRGB/ImageViewer.java @@ -0,0 +1,85 @@ +import java.io.File; +import java.io.IOException; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.image.BufferedImage; +import java.awt.BorderLayout; +import java.awt.Color; +import java.awt.GridLayout; +import javax.imageio.ImageIO; +import javax.swing.JButton; +import javax.swing.JFrame; +import javax.swing.JPanel; + +public class ImageViewer +{ + public static void main(String[] args) throws IOException + { + File file = new File("pgh_640x480.jpg"); + BufferedImage originalImage = ImageIO.read(file); + + int width = originalImage.getWidth(); + int height = originalImage.getHeight(); + + final SimpleRGB[] rgb = new SimpleRGB[5]; + rgb[0] = new SimpleRGB(width, height); + + for(int i = 0; i < height; i++) + { + for(int j = 0; j < width; j++) + { + Color c = new Color(originalImage.getRGB(j,i)); + rgb[0].setRed(j, i, c.getRed()); + rgb[0].setGreen(j, i, c.getGreen()); + rgb[0].setBlue(j, i, c.getBlue()); + } + } + + rgb[1] = rgb[0].getRedImage(); + rgb[2] = rgb[0].getGreenImage(); + rgb[3] = rgb[0].getBlueImage(); + rgb[4] = rgb[0].getGreyImage(); + + final RGBComponent rgbc = new RGBComponent(rgb[0]); + + JPanel buttonPanel = new JPanel(); + buttonPanel.setLayout(new GridLayout(1,5)); + + JButton[] button = new JButton[5]; + button[0] = new JButton("RGB"); + button[1] = new JButton("Red"); + button[2] = new JButton("Green"); + button[3] = new JButton("Blue"); + button[4] = new JButton("Greyscale"); + + class colorButtonListener implements ActionListener + { + private int index; + + public colorButtonListener(int anIndex) + { + index = anIndex; + } + + public void actionPerformed(ActionEvent arg0) + { + rgbc.setImage(rgb[index]); + } + } + + ActionListener[] cbl = new colorButtonListener[5]; + for(int i = 0; i < 5; i++) + { + cbl[i] = new colorButtonListener(i); + button[i].addActionListener(cbl[i]); + buttonPanel.add(button[i]); + } + + JFrame frame = new JFrame("Image Viewer"); + frame.setSize(642,534); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.add(rgbc); + frame.add(buttonPanel, BorderLayout.SOUTH); + frame.setVisible(true); + } +} diff --git a/labs/lab02_simpleRGB/RGBComponent.java b/labs/lab02_simpleRGB/RGBComponent.java new file mode 100644 index 0000000..8a1576e --- /dev/null +++ b/labs/lab02_simpleRGB/RGBComponent.java @@ -0,0 +1,46 @@ +import java.awt.Color; +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.geom.Line2D; + +import javax.swing.JComponent; + +@SuppressWarnings("serial") +public class RGBComponent extends JComponent +{ + private SimpleRGB image; + private int width; + private int height; + + public RGBComponent(SimpleRGB anImage) + { + image = anImage; + width = image.getWidth(); + height = image.getHeight(); + } + + public void paintComponent(Graphics g) + { + Graphics2D g2 = (Graphics2D) g; + + Line2D.Double line = new Line2D.Double(0,0,0,0); + + // Draw the Board + + for(int w = 0; w < width; w++) + { + for(int h = 0; h < height; h++) + { + g2.setColor(new Color(image.getRed(w, h), image.getGreen(w, h), image.getBlue(w, h))); + line.setLine(w, h, w, h); + g2.draw(line); + } + } + } + + public void setImage(SimpleRGB anImage) + { + image = anImage; + repaint(); + } +} diff --git a/labs/lab02_simpleRGB/SimpleRGB.java b/labs/lab02_simpleRGB/SimpleRGB.java new file mode 100644 index 0000000..5efc0bd --- /dev/null +++ b/labs/lab02_simpleRGB/SimpleRGB.java @@ -0,0 +1,190 @@ + +public class SimpleRGB +{ + // TO DO: Instant Variables + private int width; + private int height; + private int[] reds; + private int[] blues; + private int[] greens; + + public SimpleRGB(int aWidth, int aHeight) + { + this.width = aWidth; + this.height = aHeight; + reds = new int[width*height]; + blues = new int[width*height]; + greens = new int[width*height]; + } + + /** + * Gets the width of this image. + * @return the width of this image. + */ + public int getWidth() + { + return this.width; + } + + /** + * Gets the height of this image. + * @return the height of this image. + */ + public int getHeight() + { + return this.height; + } + + /** + * Sets the red value at coordinate (x,y) to aRed. + * @param x the x coordinate of this image. + * @param y the y coordinate of this image. + * @param aRed the red value (0 - 255) + */ + public void setRed(int x, int y, int aRed) + { + reds[(y*this.width) + x] = aRed; + } + + /** + * Sets the green value at coordinate (x,y) to aGreen. + * @param x the x coordinate of this image. + * @param y the y coordinate of this image. + * @param aGreen the green value (0 - 255) + */ + public void setGreen(int x, int y, int aGreen) + { + greens[(y*this.width)+x] = aGreen; + } + + /** + * Sets the blue value at coordinate (x,y) to aBlue. + * @param x the x coordinate of this image. + * @param y the y coordinate of this image. + * @param aBlue the blue value (0 - 255) + */ + public void setBlue(int x, int y, int aBlue) + { + blues[(y*this.width)+x] = aBlue; + } + + /** + * Gets the red value at coordinate (x,y). + * @param x the x coordinate of this image. + * @param y the y coordinate of this image. + * @return the value of red at coordinate (x,y). + */ + public int getRed(int x, int y) + { + return reds[(y*this.width) + x]; + } + + /** + * Gets the green value at coordinate (x,y). + * @param x the x coordinate of this image. + * @param y the y coordinate of this image. + * @return the value of green at coordinate (x,y). + */ + public int getGreen(int x, int y) + { + return greens[(y*this.width)+x]; + } + + /** + * Gets the blue value at coordinate (x,y). + * @param x the x coordinate of this image. + * @param y the y coordinate of this image. + * @return the value of blue at coordinate (x,y). + */ + public int getBlue(int x, int y) + { + return blues[(y*this.width) + x]; + } + + /** + * Get the NEW image containing only the red color. + * The red values of this new image should be exactly + * the same as red value of this image. The green and + * blue values of this new image should be 0s. + * @return the NEW image (SimpleRGB) containing only + * the red color of this image. + */ + public SimpleRGB getRedImage() + { + SimpleRGB output = new SimpleRGB(this.width,this.height); + for(int i = 0; i < reds.length; i++) + { + output.setRed(i%this.width,(i/this.width),reds[i]); + } + return output; + } + + /** + * Get the NEW image containing only the green color. + * The green values of this new image should be exactly + * the same as green value of this image. The red and + * blue values of this new image should be 0s. + * @return the NEW image (SimpleRGB) containing only + * the green color of this image. + */ + public SimpleRGB getGreenImage() + { + SimpleRGB output = new SimpleRGB(this.width,this.height); + for(int i = 0; i < greens.length; i++) + { + output.setGreen(i%this.width,(i/this.width),greens[i]); + } + return output; + } + + /** + * Get the NEW image containing only the blue color. + * The blue values of this new image should be exactly + * the same as blue value of this image. The red and + * green values of this new image should be 0s. + * @return the NEW image (SimpleRGB) containing only + * the blue color of this image. + */ + public SimpleRGB getBlueImage() + { + SimpleRGB output = new SimpleRGB(this.width,this.height); + for(int i = 0; i < blues.length; i++) + { + output.setBlue(i%this.width,(i/this.width),blues[i]); + } + return output; + } + + /** + * Get the NEW image representing the greyscale of this + * image. The grey colors are colors that the red, green + * and blue value are exactly the same. To convert an RGB + * image into a greyscale image, use the following formula + * to calculate the new value. + * (0.21 * red) + (0.72 * green) + (0.07 * blue) + * For example, suppose the (R,G,B) value of this image at + * coordinate (10,20) are (10,100,200), since + * (0.21 * 10) + (0.72 * 100) + (0.07 * 200) = 88 + * the (R,G,B) value of the new greyscale image at (10,20) + * should be (88,88,88). + * @return the NEW image representing the greyscale of this + * image. + */ + /** + *We're not british! + *It's grEy in England, + *and grAy in America! + */ + public SimpleRGB getGreyImage() + { + SimpleRGB output = new SimpleRGB(this.width,this.height); + for(int i = 0; i < reds.length; i++) + { + int gray = (int) ((reds[i]*0.21) + (greens[i]*0.72) + (blues[i]*0.07)); + output.setRed(i%this.width,(i/this.width),gray); + output.setGreen(i%this.width,(i/this.width),gray); + output.setBlue(i%this.width,(i/this.width),gray); + } + return output; + } +} diff --git a/labs/lab02_simpleRGB/SimpleRGBTester.java b/labs/lab02_simpleRGB/SimpleRGBTester.java new file mode 100644 index 0000000..2357f52 --- /dev/null +++ b/labs/lab02_simpleRGB/SimpleRGBTester.java @@ -0,0 +1,228 @@ + +public class SimpleRGBTester +{ + public static void main(String[] args) + { + int width = 500; + int height = 650; + int x = 123; + int y = 321; + int red = 1; + int green = 11; + int blue = 111; + int point = 0; + SimpleRGB rgb = new SimpleRGB(width, height); + + System.out.println("Constructing an SimpleRGB object using the following statement:"); + System.out.println(" SimpleRGB rgb = new SimpleRGB(" + width + "," + height + ");"); + + // Testing the method getWidth() + + System.out.print("Testing the method getWidth(): "); + + if(rgb.getWidth() != width) + { + System.out.println("FAIL"); + System.out.println("You method getWidth() should return " + width + "."); + System.out.println("But your method getWidth() return " + rgb.getWidth() + ".\n"); + } + else + { + point++; + System.out.println("PASS"); + } + System.out.println("You current point is " + point + ".\n"); + + // Testing the method getHeight() + + System.out.print("Testing the method getHeight(): "); + + if(rgb.getHeight() != height) + { + System.out.println("FAIL"); + System.out.println("You method getHeight() should return " + height + "."); + System.out.println("But your method getHeight() return " + rgb.getHeight() + ".\n"); + } + else + { + point++; + System.out.println("PASS"); + } + System.out.println("You current point is " + point + ".\n"); + + // Testing methods setRed() and getRed() + + System.out.print("Testing method setRed() and getRed(): "); + + rgb.setRed(x, y, red); + + if(rgb.getRed(x,y) != red) + { + System.out.println("FAIL"); + System.out.println("After calling rgb.setRed(" + x + "," + y + "," + red + ")"); + System.out.println("The method rgb.getRed(" + x + "," + y + ") should return " + red + "."); + System.out.println("But your method rgb.getRed(" + x + "," + y + ") return " + rgb.getRed(x, y) + ".\n"); + } + else + { + point += 1; + System.out.println("PASS"); + } + System.out.println("You current point is " + point + ".\n"); + + // Testing methods setGreen() and getGreen() + + System.out.print("Testing method setGreen() and getGreen(): "); + + rgb.setGreen(x, y, green); + + if(rgb.getGreen(x,y) != green) + { + System.out.println("FAIL"); + System.out.println("After calling rgb.setGreen(" + x + "," + y + "," + green + ")"); + System.out.println("The method rgb.getGreen(" + x + "," + y + ") should return " + green + "."); + System.out.println("But your method rgb.getGreen(" + x + "," + y + ") return " + rgb.getGreen(x, y) + ".\n"); + } + else + { + point += 1; + System.out.println("PASS"); + } + System.out.println("You current point is " + point + ".\n"); + + // Testing methods setBlue() and getBlue() + + System.out.print("Testing method setBlue() and getBlue(): "); + + rgb.setBlue(x, y, blue); + + if(rgb.getBlue(x,y) != blue) + { + System.out.println("FAIL"); + System.out.println("After calling rgb.setBlue(" + x + "," + y + "," + blue + ")"); + System.out.println("The method rgb.getBlue(" + x + "," + y + ") should return " + blue + "."); + System.out.println("But your method rgb.getBlue(" + x + "," + y + ") return " + rgb.getBlue(x, y) + ".\n"); + } + else + { + point += 1; + System.out.println("PASS"); + } + System.out.println("You current point is " + point + ".\n"); + + // getRedImage(); + + System.out.print("Testing method getRedImage(): "); + + SimpleRGB redImage = rgb.getRedImage(); + + if(redImage.getRed(x, y) != red || redImage.getGreen(x,y) != 0 || redImage.getBlue(x,y) != 0) + { + System.out.println("FAIL"); + System.out.println("The variable redImage of type SimpleRGB was initialized using the following statement:"); + System.out.println(" SimpleRGB redImage = rgb.getRedImage();"); + System.out.println("The (R,G,B) values of the rgb image at (" + x + "," + y + ") are (" + red + "," + green + "," + blue + ")."); + System.out.println("The (R,G,B) values of the redImage at (" + x + "," + y + ") should be (" + red + ",0,0)."); + System.out.println("But your method"); + System.out.println(" redImage.getRed(" + x + "," + y + ") returns " + redImage.getRed(x,y) + "."); + System.out.println(" redImage.getGreen(" + x + "," + y + ") returns " + redImage.getGreen(x,y) + "."); + System.out.println(" redImage.getBlue(" + x + "," + y + ") returns " + redImage.getBlue(x,y) + "."); + } + else + { + point += 1; + System.out.println("PASS"); + } + System.out.println("You current point is " + point + ".\n"); + + // getGreenImage(); + + System.out.print("Testing method getGreenImage(): "); + + SimpleRGB greenImage = rgb.getGreenImage(); + + if(greenImage.getRed(x, y) != 0 || greenImage.getGreen(x,y) != green || greenImage.getBlue(x,y) != 0) + { + System.out.println("FAIL"); + System.out.println("The variable greenImage of type SimpleRGB was initialized using the following statement:"); + System.out.println(" SimpleRGB greenImage = rgb.getGreenImage();"); + System.out.println("The (R,G,B) values of the rgb image at (" + x + "," + y + ") are (" + red + "," + green + "," + blue + ")."); + System.out.println("The (R,G,B) values of the greenImage at (" + x + "," + y + ") should be (0," + green + ",0)."); + System.out.println("But your method"); + System.out.println(" greenImage.getRed(" + x + "," + y + ") returns " + greenImage.getRed(x,y) + "."); + System.out.println(" greenImage.getGreen(" + x + "," + y + ") returns " + greenImage.getGreen(x,y) + "."); + System.out.println(" greenImage.getBlue(" + x + "," + y + ") returns " + greenImage.getBlue(x,y) + "."); + } + else + { + point += 1; + System.out.println("PASS"); + } + System.out.println("You current point is " + point + ".\n"); + + // getBlueImage(); + + System.out.print("Testing method getBlueImage(): "); + + SimpleRGB blueImage = rgb.getBlueImage(); + + if(blueImage.getRed(x, y) != 0 || blueImage.getGreen(x,y) != 0 || blueImage.getBlue(x,y) != blue) + { + System.out.println("FAIL"); + System.out.println("The variable blueImage of type SimpleRGB was initialized using the following statement:"); + System.out.println(" SimpleRGB blueImage = rgb.getBlueImage();"); + System.out.println("The (R,G,B) values of the rgb image at (" + x + "," + y + ") are (" + red + "," + green + "," + blue + ")."); + System.out.println("The (R,G,B) values of the blueImage at (" + x + "," + y + ") should be (0,0," + blue + ")."); + System.out.println("But your method"); + System.out.println(" blueImage.getRed(" + x + "," + y + ") returns " + blueImage.getRed(x,y) + "."); + System.out.println(" blueImage.getGreen(" + x + "," + y + ") returns " + blueImage.getGreen(x,y) + "."); + System.out.println(" blueImage.getBlue(" + x + "," + y + ") returns " + blueImage.getBlue(x,y) + "."); + } + else + { + point += 1; + System.out.println("PASS"); + } + System.out.println("You current point is " + point + ".\n"); + + // getGreyImage(); + + System.out.print("Testing method getGreyImage(): "); + + SimpleRGB greyImage = rgb.getGreyImage(); + + int greyValue = (int) ((0.21 * rgb.getRed(x, y)) + (0.72 * rgb.getGreen(x,y)) + (0.07 * rgb.getBlue(x,y))); + + if(greyImage.getRed(x, y) != greyValue || greyImage.getGreen(x,y) != greyValue || greyImage.getBlue(x,y) != greyValue) + { + System.out.println("FAIL"); + System.out.println("The variable greyImage of type SimpleRGB was initialized using the following statement:"); + System.out.println(" SimpleRGB greyImage = rgb.getGreyImage();"); + System.out.println("The (R,G,B) values of the rgb image at (" + x + "," + y + ") is (" + red + "," + green + "," + blue + ")."); + System.out.println("The (R,G,B) values of the greyImage at (" + x + "," + y + ") should be (" + greyValue + "," + greyValue + "," + greyValue + ")."); + System.out.println("But your method"); + System.out.println(" greyImage.getRed(" + x + "," + y + ") returns " + greyImage.getRed(x,y) + "."); + System.out.println(" greyImage.getGreen(" + x + "," + y + ") returns " + greyImage.getGreen(x,y) + "."); + System.out.println(" greyImage.getBlue(" + x + "," + y + ") returns " + greyImage.getBlue(x,y) + "."); + } + else + { + point += 2; + System.out.println("PASS"); + } + System.out.println("You current point is " + point + ".\n"); + + System.out.println("Your final point is " + point + "."); + + if(point == 10) + { + System.out.println("Contratulation! Your class SimpleRGB works perfectly (I guess)."); + System.out.println("You can run ImageViewer to see how SimpleRGB can be used in a program."); + } + else + { + System.out.println("There is one or more errors in your class."); + System.out.println("Fix your bugs to get more points."); + } + } +} diff --git a/labs/lab02_simpleRGB/lab02_simpleRGB.pdf b/labs/lab02_simpleRGB/lab02_simpleRGB.pdf Binary files differnew file mode 100644 index 0000000..8558e16 --- /dev/null +++ b/labs/lab02_simpleRGB/lab02_simpleRGB.pdf diff --git a/labs/lab02_simpleRGB/pgh_640x480.jpg b/labs/lab02_simpleRGB/pgh_640x480.jpg Binary files differnew file mode 100644 index 0000000..12894c8 --- /dev/null +++ b/labs/lab02_simpleRGB/pgh_640x480.jpg |
