Make sure review the four homework assignments, all of the labs we've done, and the examples we've written in class. Alos, I may ask some questions from Exam 1.
Solution: This is almost exactly like our gambler's ruin example except yo uneed to generate random numbers between 1 and 6 inclusive.
makeBlurry
that animates filling in a picture with random colors at random locations within the picture.
Have the user pass in as a parameter how many random pixel changes they want to make. Repainting after every pixel change is slow, so have your method repaint every tenth pixel to speed things up (don't use Util.sleep
,
that will really slow it down). For example the call, in my solution, the code
below results int the blurry bug by randomly modifying pixels 150,000 times.
String path = "T:/Harcourt/Fall2008/CS140/gallery/"; Picture pic1 = new Picture(path + "bug.jpg"); pic1.makeBlurry(150000);
public void makeBlurry(int count) { for(int i = 0; i < count; i++) { // generate a random location int x = (int)(Math.random()*getWidth()); int y = (int)(Math.random()*getHeight()); // generate a random color int red = (int) (Math.random()*255); int green = (int) (Math.random()*255); int blue = (int) (Math.random()*255); getPixel(x,y).setColor(new Color(red,green,blue)); // repaint every tenth pixel if ((i / 10.0) - (i/10) == 0) { repaint(); } } }
flipHorizontal
that flips a picture horizontally.
For example, here is the bug flipped horizontally. (Contrast this with the original.)
public Picture flipHorizontal() { int height = getHeight(); int width = getWidth(); Picture temp = new Picture(width, height); for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { Pixel leftPixel = getPixel(x,y); Pixel rightPixel = temp.getPixel(width - 1 - x, y); rightPixel.setColor(leftPixel.getColor()); } } return temp; }
rotateLeft
that rotates a picture
ninety degrees counter clockwise.
For example, here is the bug rotated left. (Contrast this with the original)
public Picture rotateLeft(){ Picture dest = new Picture(getHeight(), getWidth()); for(int col = 0; col < getWidth(); col ++){ for (int row = 0; row< getHeight(); row++){ Pixel src_pixel = getPixel(col,row); Pixel dest_pixel = dest.getPixel(row, getWidth() - col -1); dest_pixel.setColor(src_pixel.getColor());} } return dest; }
morph
that changes a picture into another picture from
the top down. For example, assuming pic1
and pic2
are the same exact dimensions
pic1.morph(pic2);will change
pic1
into pic2
row by row starting at the
top.
public void morph(Picture p) { int height = getHeight(); int width = getWidth(); for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { Pixel dst = getPixel(x,y); Pixel src = p.getPixel(x, y); dst.setColor(src.getColor()); } repaint(); Util.sleep(.02); } }
Picture.java
file and work properly so you can use them.
Here's my sample main program for the study guide methods (except for the first program that simulates rolling a die.
public class Exam2StudyGuide { public static void main(String [] args) { String path = "T:/Harcourt/Fall2008/CS140/gallery/"; Picture pic = new Picture(path + "bug.jpg"); Picture pic1 = new Picture(path + "colors_small.jpg"); Picture pic2 = new Picture(path + "mutant_lemon_small.jpg"); pic.flipHorizontal().repaint(); pic.makeBlurry(100000); pic.flipHorizontal().repaint(); pic.rotateLeft().repaint(); pic1.morph(pic2); } }