Lab Assignment #6
Overview
- You'll be working in pairs: two people to one machine. You just need to login as ONE of you.
- Login: login to the machine in CentOS.
- Part A: Compound Boolean expressions - ANDs (&&) and ORs (||)
- Part B: Explore || versus && and boundary checking
- Quiz: Each person will individually take a short quiz.
Part A
Copy
- Right click on the Desktop and Select "Open a Terminal"
- Paste the following lines of code into the terminal
- cd ../public/Lab6
- make directories
- make install
- cd
- cd Lab6
- mv Lab6A.java.empty Lab6A.java
- mv Lab6B.java.empty Lab6B.java
- mv Picture.java.empty Picture.java
- ls
- The previous command should print out five filenames: Lab6A.java Lab6B.java Picture.java blur.txt makeConvict.txt
- Open makeConvict.txt, and copy the method into
Read and Understand The Code
- You will be exploring if expressions in modifying a Picture with a compound boolean expression. The goal is to make a bird picture with the bird wearing a "Black and White" convict suit for Halloween.
- Read and understand the makeConvict() method that you already copied into Picture.java. It is not yet complete. What do you predict that it currently does?
Compile, Run and Test
- Run the Lab6A.java application, select bird1.jpg as your picture and see what happens.
- There is an if statement which replaces green pixels with black on the bird – only on rows in the picture whose indexes are even and a certain level of green. In particular, it changes the pixel color to black if its y index is even AND the pixel's level of green is more than 180.
- Copy the 4 lines of that if statement, and paste it
immediately after itself, so there will now be two copies of the same if statement,
one right after the other. Then modify
the second if statement (the one you just pasted) as follows:
Modify the boolean controlling expression of the second if statement to evaluate to true if the y index is odd AND the color of green of the pixel is > 180
Modify the line of code controlled by this second if statement to set the pixel color to white - Compile, Run, choose bird1.jpg from mediasources and check that your bird now wears a convict's horizontally striped black and white outfit.
- The two if expressions are one way to do it, but not the only way (or the best way). Rework them to be one if statement that checks for green > 180, and inside the body of that if statement, have an if – else statement that will either assign black or white stripes based on the value of the y index. Run your code to verify that the new set of if statements does the same thing as the original set.
- But there's still a problem with a our convict bird! The stripes are two thin. Modify the code so that each stripe is two pixels tall instead of one. You completed code should look like the bird below.
- HINT: This can be done by modifying the modulus statements in the conditional.
Part B
Read and Understand the Code
- Switch driver and navigator: if you've been controlling the mouse and keyboard, it's time for your partner to do so.
- Open blur.txt (use text editor so you can copy and paste)
- Before you get started, read and understand the blur() method. This is a simple blur – it is supposed to blur pixels only along the x direction. That is, each the color of each pixel is replaced with the average of three pixels – itself, the one to its left and the one to its right
However, we know that we might have problems "at the edge" of the picture boundary. For all pixels with x coordinate 0, we should not do an average, since there is no pixel to the left to average in. Same with pixels with x offset this.getWidth()-1: there is no pixel to the right to average in. So we've included an if statement in the loop that should only do the averaging as long as it's not a "first column" or "last column" pixel. That is, we'll only calculate averages for the middle part of the picture below:
Compile, Run and Test
- Compile the code, run it and test on a picture (we recommend flower1.jpg).
- AHA! Look in the interactions pane and you will see an error! What is the error? (write the first line of the error message)
- Look through the various lines of the error message for the error reported from the Picture file in your blur method. What line is the error in Picture.java in the blur method? (if you can't find this information in the error message, ask)
- Go to that line in Picture.java. This should be the line assigning a value to redValue. To assign to redValue we need to "look at" three pixels: (x-1,y), (x,y) and (x+1,y). Why do you think we have an index out of bounds error?
- Fix the if statement so that we only calculate an average and update the pixel color when x is bigger than the first column AND smaller than the last column. You will need to make two changes in the controlling boolean expression of that if statement. Compile and Run. Did it work? Ask if you are not sure exactly why your if expression is now keeping you from getting an error.
- Now you are going to write your own method that performs a similar blur.
- Copy and paste the following header into Picture.java: public void blur_y(){}
- This method should blur pixels in the same way as the previous method but in the y direction. In other words, the color of a pixel should be the average of the pixels immediately above and below it. In the same way as before, we do not want to include the top or bottom rows of pixels in our averaging
- HINT: You may reuse most of the same code as from the regular blur method
Can You Do: Do this after the Lab
The bird's head is getting the striped effect too. Use Picture explorer to find the y height of the head. Modify your code so that the head doesn't get changed from green.
Modify this code so that it replaces each pixel with the average of the pixel above and below it. (this will blur on the y-axis)
The bird's head is getting the striped effect too. Use Picture explorer to find the y height of the head. Modify your code so that the head doesn't get changed from green.
Modify this code so that it replaces each pixel with the average of the pixel above and below it. (this will blur on the y-axis)
Log Out and Invite Your Partner for PSA7
Click on System > Log Out cs8wzz... > Log Off
Complete Your Lab Quiz
See the instructor to get your individual lab quiz.
The quizzes are open book & note, but closed Dr Java and partner.
The quizzes are open book & note, but closed Dr Java and partner.
Sample Code
/*
* Lab6A.java -- Use compound if expressions in a Picture method to
* make a picture of a bird have a convict suite (black and white striped).
*/
public class Lab6A
{
public static void main (String[] args)
{
Picture p = new Picture(FileChooser.pickAFile());
p.show();
p.makeConvict();
p.repaint();
}
}
****************************************************************
public void makeConvict()
{
for (int x = 0; x < this.getWidth(); x++)
{
for (int y = 0; y < this.getHeight(); y++)
{
Pixel currentPix = this.getPixel(x,y);
if ( ((y%2) == 0) && (currentPix.getGreen() > 200))
{
currentPix.setColor(Color.BLACK);
}
// Copy above 4 lines here
} // end inner for
} // end outer for
} // end makeConvict()
****************************************************************
/* * Lab6B.java – Blurring a picture.
*/
public class Lab6B
{
public static void main(String[] args)
{
Picture p = new Picture(FileChooser.pickAFile());
Picture copyP = new Picture(p);
copyP.blur();
p.show();
copyP.show();
}
}
****************************************************************
public void blur()
{
int redValue, blueValue, greenValue;
Pixel updatePixel;
Color c;
for (int x = 0; x < this.getWidth(); x++)
{
for (int y = 0; y < this.getHeight(); y++)
{
updatePixel = this.getPixel(x,y);
if ((x > 0) || (x < (this.getWidth()+1)) )
{
redValue = this.getPixel(x-1,y).getRed() +
this.getPixel(x,y).getRed() +
this.getPixel(x+1,y).getRed();
blueValue = this.getPixel(x-1,y).getBlue() +
this.getPixel(x,y).getBlue() +
this.getPixel(x+1,y).getBlue();
greenValue = this.getPixel(x-1,y).getGreen() +
this.getPixel(x,y).getGreen() +
this.getPixel(x+1,y).getGreen();
c = new Color(redValue/3, greenValue/3, blueValue/3);
updatePixel.setColor(c);
}
} // end inner for
} // end outer for
} // end blur()
* Lab6A.java -- Use compound if expressions in a Picture method to
* make a picture of a bird have a convict suite (black and white striped).
*/
public class Lab6A
{
public static void main (String[] args)
{
Picture p = new Picture(FileChooser.pickAFile());
p.show();
p.makeConvict();
p.repaint();
}
}
****************************************************************
public void makeConvict()
{
for (int x = 0; x < this.getWidth(); x++)
{
for (int y = 0; y < this.getHeight(); y++)
{
Pixel currentPix = this.getPixel(x,y);
if ( ((y%2) == 0) && (currentPix.getGreen() > 200))
{
currentPix.setColor(Color.BLACK);
}
// Copy above 4 lines here
} // end inner for
} // end outer for
} // end makeConvict()
****************************************************************
/* * Lab6B.java – Blurring a picture.
*/
public class Lab6B
{
public static void main(String[] args)
{
Picture p = new Picture(FileChooser.pickAFile());
Picture copyP = new Picture(p);
copyP.blur();
p.show();
copyP.show();
}
}
****************************************************************
public void blur()
{
int redValue, blueValue, greenValue;
Pixel updatePixel;
Color c;
for (int x = 0; x < this.getWidth(); x++)
{
for (int y = 0; y < this.getHeight(); y++)
{
updatePixel = this.getPixel(x,y);
if ((x > 0) || (x < (this.getWidth()+1)) )
{
redValue = this.getPixel(x-1,y).getRed() +
this.getPixel(x,y).getRed() +
this.getPixel(x+1,y).getRed();
blueValue = this.getPixel(x-1,y).getBlue() +
this.getPixel(x,y).getBlue() +
this.getPixel(x+1,y).getBlue();
greenValue = this.getPixel(x-1,y).getGreen() +
this.getPixel(x,y).getGreen() +
this.getPixel(x+1,y).getGreen();
c = new Color(redValue/3, greenValue/3, blueValue/3);
updatePixel.setColor(c);
}
} // end inner for
} // end outer for
} // end blur()