In this challenge, you will do a part of the Picture Lab to modify the pixels of a digital photo. Scroll down to the bottom of the following code and take a look at the zeroBlue() method. Run the code and watch what it does. It uses nested loops to visit each pixel in a photo which has a color with red, green, and blue values, and it sets all the blue values to 0.
Now, write a similar method called keepOnlyBlue() that visits every pixel and sets the red and green values to zero but does not change the blue ones. Then, write a method called switchColors() that swaps the red pixels with green pixels or blue pixels to change the colors around. You will need to use the getRed(), getGreen(), getBlue() to get the RGB values of the pixel and then swap them around by using the setRed, setGreen, setBlue methods and giving them different color values from the get methods as arguments.
You can test the methods in the active code below or your own IDE to see what it does.
Picture Lab: 1) write a method called keepOnlyBlue() that keeps only the blue values by setting the red and green values to zero. Uncomment the code in main to test it. 2) write a method called switchColors() that replaces red values (using p.setRed) with green or blue values (using p.getGreen(), etc.) to change the colors around. Uncomment the code in main to test i..;@a..n@cc.gatech.edu
*/
public class Picture extends SimplePicture
{
///////////////////// constructors //////////////////////////////////
/**
* Constructor that takes no arguments
*/
public Picture ()
{
/* not needed but use it to show students the implicit call to super()
* child constructors always call a parent constructor
*/
super();
}
/**
* Constructor that takes a file name and creates the picture
* @param fileName the name of the file to create the picture from
*/
public Picture(String fileName)
{
// let the parent class handle this fileName
super(fileName);
}
/**
* Constructor that takes the height and width
* @param height the height of the desired picture
* @param width the width of the desired picture
*/
public Picture(int height, int width)
{
// let the parent class handle this width and height
super(width,height);
}
/**
* Constructor that takes a picture and creates a
* copy of that picture
* @param copyPicture the picture to copy
*/
public Picture(Picture copyPicture)
{
// let the parent class do the copy
super(copyPicture);
}
/**
* Constructor that takes a buffered image
* @param image the buffered image to use
*/
public Picture(BufferedImage image)
{
super(image);
}
////////////////////// methods ///////////////////////////////////////
/..;@return a string with information about the picture such as fileName,
* height and width.
*/
public String toString()
{
String output = "Picture, filename " + getFileName() +
" height " + getHeight()
+ " width " + getWidth();
return output;
}
/**
zeroBlue() method sets the blue values at all pixels to zero
*/
public void zeroBlue()
{
Pixel[][] pixels = this.getPixels2D();
for (Pixel[] rowArray : pixels)
{
for (Pixel p: rowArray)
{
p.setBlue(0);
}
}
}
/* Add new methods here.
keepOnlyBlue() method sets the blue values at all pixels to zero.
switchColors() method swaps the color values of pixels.
*/
/* Main method for testing
*/
public static void main(String[] args)
{
Picture arch = new Picture("arch.jpg");
arch.show();
arch.zeroBlue();
arch.show();
//Uncomment the follow code to test your keepOnlyBlue method.
/*
Picture arch2 = new Picture("arch.jpg");
System.out.println("Keep only blue: ");
arch2.keepOnlyBlue();// using new method
arch2.show();
*/
System.out.println();
//Uncomment the follow code to test your swithColors method.
/*
Picture arch3 = new Picture("arch.jpg");
System.out.println("Switch colors: ");
arch3.switchColors();// using new method
arch3.show();
*/
}
}
sectetur adipiscing elit. Na
sec
sectetur adipiscing elit. Nam lacinia pulvi
sectetur adipiscing elit. Nam lacini
sectet
sectetur adipiscing elit. Nam l
sectetur
sectetur adipiscing eli
sectetur adipiscing elit.
sectetur
sectet
sec
Unlock access to this and over
10,000 step-by-step explanations
Have an account? Log In
sectetur adipiscing elit. N
se
sectetur adipiscing elit. Nam lacinia pulv
sectetur adipiscing elit. Nam lacin
secte
sectetur adipiscing elit. Nam
sectetur
sectetur adipiscing elit. Nam lacini
sectetur adipiscing elit. Nam lacinia
sectetur adipiscing elit. Nam lacin
sectetur adipiscing elit. N
sectetur adipiscing elit. Nam l
sectetur adipiscing elit. Nam
sectetur
secte
se
se
sec
sectetur adipiscing elit. Nam lacinia pulvinar tortor nec facilisis. Pellentesque dapibus efficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magna. Fusce dui lectus, congue vel laoreet ac, dictum vitae odio. Donec
se