diff --git a/classes/IntArrayWorker.java b/classes/IntArrayWorker.java index 489e13e..32b6f5e 100755 --- a/classes/IntArrayWorker.java +++ b/classes/IntArrayWorker.java @@ -1,149 +1,137 @@ public class IntArrayWorker { - /** two dimensional matrix */ - private int[][] matrix = null; - - /** set the matrix to the passed one - * @param theMatrix the one to use - */ - public void setMatrix(int[][] theMatrix) - { - matrix = theMatrix; - } - - /** - * Method to return the total - * @return the total of the values in the array - */ - public int getTotal() - { - int total = 0; - for (int row = 0; row < matrix.length; row++) + /** two dimensional matrix */ + private int[][] matrix = null; + + /** set the matrix to the passed one + * @param theMatrix the one to use + */ + public void setMatrix(int[][] theMatrix) { - for (int col = 0; col < matrix[0].length; col++) - { - total = total + matrix[row][col]; - } + matrix = theMatrix; } - return total; - } - - /** - * Method to return the total using a nested for-each loop - * @return the total of the values in the array - */ - public int getTotalNested() - { - int total = 0; - for (int[] rowArray : matrix) + + public int getCount(int lookingfor){ + int timesfound = 0; + for(int row = 0; row < matrix.length; row++){ + for(int col = 0; col < matrix[row].length; col++){ + if(matrix[row][col]==(lookingfor)){ + timesfound++; + } + } + } + return timesfound; + } + + public int getLargest(){ + int largest = -1; + for(int[] row : matrix){ + for(int i : row){ + if(i>largest){ + largest = i; + } + } + } + return largest; + } + + public int getColTotal(int colLook){ + int total = 0; + for(int row = 0; row < matrix.length; row++){ + for(int col = 0; col < matrix[row].length; col++){ + if(col == colLook){ + total+=matrix[row][col]; + } + } + } + return total; + } + + /** + * Method to return the total + * @return the total of the values in the array + */ + public int getTotal() + { + int total = 0; + for (int row = 0; row < matrix.length; row++) + { + for (int col = 0; col < matrix[0].length; col++) + { + total = total + matrix[row][col]; + } + } + return total; + } + + /** + * Method to return the total using a nested for-each loop + * @return the total of the values in the array + */ + public int getTotalNested() { - for (int item : rowArray) - { - total = total + item; - } + int total = 0; + for (int[] rowArray : matrix) + { + for (int item : rowArray) + { + total = total + item; + } + } + return total; } - return total; - } - - /** - * Method to fill with an increasing count - */ - public void fillCount() - { - int numCols = matrix[0].length; - int count = 1; - for (int row = 0; row < matrix.length; row++) + + /** + * Method to fill with an increasing count + */ + public void fillCount() { - for (int col = 0; col < numCols; col++) - { - matrix[row][col] = count; - count++; - } + int numCols = matrix[0].length; + int count = 1; + for (int row = 0; row < matrix.length; row++) + { + for (int col = 0; col < numCols; col++) + { + matrix[row][col] = count; + count++; + } + } } - } - - /** - * print the values in the array in rows and columns - */ - public void print() - { - for (int row = 0; row < matrix.length; row++) + + /** + * print the values in the array in rows and columns + */ + public void print() { - for (int col = 0; col < matrix[0].length; col++) - { - System.out.print( matrix[row][col] + " " ); - } - System.out.println(); + for (int row = 0; row < matrix.length; row++) + { + for (int col = 0; col < matrix[0].length; col++) + { + System.out.print( matrix[row][col] + " " ); + } + System.out.println(); + } + System.out.println(); } - System.out.println(); - } - - - /** - * fill the array with a pattern - */ - public void fillPattern1() - { - for (int row = 0; row < matrix.length; row++) + + /** + * fill the array with a pattern + */ + public void fillPattern1() { - for (int col = 0; col < matrix[0].length; - col++) - { - if (row < col) - matrix[row][col] = 1; - else if (row == col) - matrix[row][col] = 2; - else - matrix[row][col] = 3; - } + for (int row = 0; row < matrix.length; row++) + { + for (int col = 0; col < matrix[0].length; + col++) + { + if (row < col) + matrix[row][col] = 1; + else if (row == col) + matrix[row][col] = 2; + else + matrix[row][col] = 3; + } + } } - } - - public int getCount(int number) - { - int count = 0; - for (int[] rowArray : matrix) - { - for (int i : rowArray) - { - if (i == number) - { - count++; - } - } - } - - return count; - } - - public int getLargest() - { - int largest = Integer.MIN_VALUE; - - for (int[] row : matrix) - { - for (int i : row) - { - if (i > largest) - { - largest = i; - } - } - } - - return largest; - } - - public int getColTotal(int col) - { - int total = 0; - - for (int[] row : matrix) - { - total += row[col]; - } - - return total; - } - + } \ No newline at end of file diff --git a/classes/IntArrayWorkerTester.java b/classes/IntArrayWorkerTester.java index b876cd9..11795ea 100755 --- a/classes/IntArrayWorkerTester.java +++ b/classes/IntArrayWorkerTester.java @@ -1,104 +1,104 @@ public class IntArrayWorkerTester { - /** method to test setMatrix */ - public static void testSetMatrix() - { - IntArrayWorker worker = new IntArrayWorker(); - int[][] nums = {{1, 1, 1} ,{2,2,2}}; - worker.setMatrix(nums); - System.out.println("This should have all 1's in first row and all 2's in second"); - worker.print(); - } - - /** Method to test fillPattern1 */ - public static void testFillPattern1() - { - IntArrayWorker worker = new IntArrayWorker(); - int[][] nums = new int[3][4]; - worker.setMatrix(nums); - worker.fillPattern1(); - System.out.println("fills with 2's on diagonal, 3's to left, and 1's to right"); - worker.print(); - } - - /** Method to test getCount*/ - public static void testGetCount() - { - IntArrayWorker worker = new IntArrayWorker(); - int[][] nums = new int[3][4]; - worker.setMatrix(nums); - worker.fillPattern1(); - int count = worker.getCount(1); - System.out.println("Count should be 6 and count is " + count); - } - - /** Method to test getTotal */ - public static void testGetTotal() - { - IntArrayWorker worker = new IntArrayWorker(); - int [][] nums2 = {{1, 2, 3}, {4, 5, 6}}; - worker.setMatrix(nums2); - int total = worker.getTotal(); - System.out.println("Total should be 21 and is " + total); - } - - /** Method to test getTotalNested */ - public static void testGetTotalNested() - { - IntArrayWorker worker = new IntArrayWorker(); - int [][] nums2 = {{1, 2, 3}, {4, 5, 6}}; - worker.setMatrix(nums2); - int total = worker.getTotalNested(); - System.out.println("Total should be 21 and is " + total); - } - - /** Method to test getLargest */ - public static void testGetLargest() - { // test when largest is last - IntArrayWorker worker = new IntArrayWorker(); - int [][] nums2 = {{1, 2, 3}, {4, 5, 6}}; - worker.setMatrix(nums2); - int largest = worker.getLargest(); - System.out.println("Largest should be 6 and is " + largest); - // test when largest is first - int[][] nums3 = {{6, 2, 3}, {4, 5, 1}}; - worker.setMatrix(nums3); - largest = worker.getLargest(); - System.out.println("Largest should be 6 and is " + largest); - // test when largest is in the middle - int[][] nums4 = {{1, 2, 3}, {6, 5, 1}}; - worker.setMatrix(nums4); - largest = worker.getLargest(); - System.out.println("Largest should be 6 and is " + largest); - // test when duplicate largest - int[][] nums5 = {{6, 2, 6}, {4, 5, 1}}; - worker.setMatrix(nums5); - largest = worker.getLargest(); - System.out.println("Largest should be 6 and is " + largest); - } - - /** Method to test getColTotal */ - public static void testGetColTotal() - { - IntArrayWorker worker = new IntArrayWorker(); - int [][] nums2 = {{1, 2, 3}, {4, 5, 6}}; - worker.setMatrix(nums2); - int total = worker.getColTotal(0); - System.out.println("Total for column 0 should be 5 and is " + total); - total = worker.getColTotal(1); - System.out.println("Total for column 1 should be 7 and is " + total); - total = worker.getColTotal(2); - System.out.println("Total for column 2 should be 9 and is " + total); - } - - public static void main(String[] args) - { - testSetMatrix(); - testFillPattern1(); - testGetCount(); - testGetTotal(); - testGetTotalNested(); - testGetLargest(); - testGetColTotal(); - } + /** method to test setMatrix */ + public static void testSetMatrix() + { + IntArrayWorker worker = new IntArrayWorker(); + int[][] nums = {{1, 1, 1} ,{2,2,2}}; + worker.setMatrix(nums); + System.out.println("This should have all 1's in first row and all 2's in second"); + worker.print(); + } + + /** Method to test fillPattern1 */ + public static void testFillPattern1() + { + IntArrayWorker worker = new IntArrayWorker(); + int[][] nums = new int[3][4]; + worker.setMatrix(nums); + worker.fillPattern1(); + System.out.println("fills with 2's on diagonal, 3's to left, and 1's to right"); + worker.print(); + } + + /** Method to test getCount*/ + public static void testGetCount() + { + IntArrayWorker worker = new IntArrayWorker(); + int[][] nums = new int[3][4]; + worker.setMatrix(nums); + worker.fillPattern1(); + int count = worker.getCount(1); + System.out.println("Count should be 6 and count is " + count); + } + + /** Method to test getTotal */ + public static void testGetTotal() + { + IntArrayWorker worker = new IntArrayWorker(); + int [][] nums2 = {{1, 2, 3}, {4, 5, 6}}; + worker.setMatrix(nums2); + int total = worker.getTotal(); + System.out.println("Total should be 21 and is " + total); + } + + /** Method to test getTotalNested */ + public static void testGetTotalNested() + { + IntArrayWorker worker = new IntArrayWorker(); + int [][] nums2 = {{1, 2, 3}, {4, 5, 6}}; + worker.setMatrix(nums2); + int total = worker.getTotalNested(); + System.out.println("Total should be 21 and is " + total); + } + + /** Method to test getLargest */ + public static void testGetLargest() + { // test when largest is last + IntArrayWorker worker = new IntArrayWorker(); + int [][] nums2 = {{1, 2, 3}, {4, 5, 6}}; + worker.setMatrix(nums2); + int largest = worker.getLargest(); + System.out.println("Largest should be 6 and is " + largest); + // test when largest is first + int[][] nums3 = {{6, 2, 3}, {4, 5, 1}}; + worker.setMatrix(nums3); + largest = worker.getLargest(); + System.out.println("Largest should be 6 and is " + largest); + // test when largest is in the middle + int[][] nums4 = {{1, 2, 3}, {6, 5, 1}}; + worker.setMatrix(nums4); + largest = worker.getLargest(); + System.out.println("Largest should be 6 and is " + largest); + // test when duplicate largest + int[][] nums5 = {{6, 2, 6}, {4, 5, 1}}; + worker.setMatrix(nums5); + largest = worker.getLargest(); + System.out.println("Largest should be 6 and is " + largest); + } + + /** Method to test getColTotal */ + public static void testGetColTotal() + { + IntArrayWorker worker = new IntArrayWorker(); + int [][] nums2 = {{1, 2, 3}, {4, 5, 6}}; + worker.setMatrix(nums2); + int total = worker.getColTotal(0); + System.out.println("Total for column 0 should be 5 and is " + total); + total = worker.getColTotal(1); + System.out.println("Total for column 1 should be 7 and is " + total); + total = worker.getColTotal(2); + System.out.println("Total for column 2 should be 9 and is " + total); + } + + public static void main(String[] args) + { + testSetMatrix(); + testFillPattern1(); + //testGetCount(); + testGetTotal(); + testGetTotalNested(); + //testGetLargest(); + //testGetColTotal(); + } } \ No newline at end of file diff --git a/classes/Picture.java b/classes/Picture.java index 92d845e..4b15bd9 100755 --- a/classes/Picture.java +++ b/classes/Picture.java @@ -15,269 +15,580 @@ */ 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 + ///////////////////// 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 width and height + * @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 /////////////////////////////////////// + + /** + * Method to return a string with information about this picture. + * @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; + + } + + /** Method to set the blue to 0 */ + public void zeroBlue() + { + Pixel[][] pixels = this.getPixels2D(); + for (Pixel[] rowArray : pixels) + { + for (Pixel pixelObj : rowArray) + { + pixelObj.setBlue(0); + } + } + } + + public void keepOnlyBlue() + { + Pixel[][] pixels = this.getPixels2D(); + for (Pixel[] rowArray : pixels) + { + for (Pixel pixelObj : rowArray) + { + pixelObj.setGreen(0); + pixelObj.setRed(0); + } + } + } + + public void negate(){ + Pixel[][] pixels = this.getPixels2D(); + for (Pixel[] rowArray : pixels) + { + for (Pixel pixelObj : rowArray) + { + int red = 255 - pixelObj.getRed(); + int green = 255 -pixelObj.getGreen(); + int blue = 255-pixelObj.getBlue(); + pixelObj.setRed(red); + pixelObj.setGreen(green); + pixelObj.setBlue(blue); + } + } + } + + public void grayScale(){ + Pixel[][] pixels = this.getPixels2D(); + for (Pixel[] rowArray : pixels) + { + for (Pixel pixelObj : rowArray) + { + int value = (pixelObj.getBlue()+pixelObj.getGreen()+pixelObj.getRed())/3; + pixelObj.setGreen(value); + pixelObj.setBlue(value); + pixelObj.setRed(value); + } + } + } + + public void fixUnderwater(){ + Pixel[][] pixels = this.getPixels2D(); + for (Pixel[] rowArray : pixels) + { + for (Pixel pixelObj : rowArray) + { + int red = pixelObj.getRed(); + int blue = pixelObj.getBlue(); + int green = pixelObj.getGreen(); + int diffromblue = Math.abs(blue-(red+green)); + //if(blue>(Math.abs(green+red-blue))){ + pixelObj.setBlue(blue-diffromblue); + pixelObj.setGreen(green+diffromblue); + pixelObj.setRed(red+diffromblue); + //} + } + } + } + + /** Method that mirrors the picture around a + * vertical mirror in the center of the picture + * from left to right */ + public void mirrorVertical() + { + Pixel[][] pixels = this.getPixels2D(); + Pixel leftPixel = null; + Pixel rightPixel = null; + int width = pixels[0].length; + for (int row = 0; row < pixels.length; row++) + { + for (int col = 0; col < width / 2; col++) + { + leftPixel = pixels[row][col]; + rightPixel = pixels[row][width - 1 - col]; + rightPixel.setColor(leftPixel.getColor()); + } + } + } + + public void mirrorVerticalRightToLeft() + { + Pixel[][] pixels = this.getPixels2D(); + Pixel leftPixel = null; + Pixel rightPixel = null; + int width = pixels[0].length; + for (int row = 0; row < pixels.length; row++) + { + for (int col = 0; col < width / 2; col++) + { + leftPixel = pixels[row][col]; + rightPixel = pixels[row][width - 1 - col]; + leftPixel.setColor(rightPixel.getColor()); + } + } + } + + public void mirrorHorrizontal() + { + Pixel[][] pixels = this.getPixels2D(); + Pixel topPixel = null; + Pixel bottomPixel = null; + int height = pixels.length; + for (int row = 0; row < height / 2; row++) + { + for (int col = 0; col < pixels[0].length; col++) + { + topPixel = pixels[row][col]; + bottomPixel = pixels[height - 1 - row][col]; + bottomPixel.setColor(topPixel.getColor()); + } + } + } + + public void mirrorHorrizontalBotToTop() + { + Pixel[][] pixels = this.getPixels2D(); + Pixel topPixel = null; + Pixel bottomPixel = null; + int height = pixels.length; + for (int row = 0; row < height / 2; row++) + { + for (int col = 0; col < pixels[0].length; col++) + { + topPixel = pixels[row][col]; + bottomPixel = pixels[height - 1 - row][col]; + topPixel.setColor(bottomPixel.getColor()); + } + } + } + + public void mirrorDiagonal(){ + Pixel[][] pixels = this.getPixels2D(); + + Pixel one = null; + Pixel two = null; + + for (int row = 0; row < pixels.length; row++) + { + for (int col = 0; col < pixels[0].length; col++) + { + + try{ + one = pixels[row][col]; + two = pixels[col][row]; + one.setColor(two.getColor()); + }catch(Exception e){ + + } + } + } + } + + /** Mirror just part of a picture of a temple */ + public void mirrorTemple() + { + int mirrorPoint = 276; + Pixel leftPixel = null; + Pixel rightPixel = null; + int count = 0; + Pixel[][] pixels = this.getPixels2D(); + + // loop through the rows + for (int row = 27; row < 97; row++) + { + // loop from 13 to just before the mirror point + for (int col = 13; col < mirrorPoint; col++) + { + count++; + leftPixel = pixels[row][col]; + rightPixel = pixels[row] + [mirrorPoint - col + mirrorPoint]; + rightPixel.setColor(leftPixel.getColor()); + } + } + System.out.println(count); + } + + public void mirrorArms() + { + int top = 190; + int bot = 225; + int right = 96; + int left = 311; + + Pixel[][] pixels = this.getPixels2D(); + Pixel lookingatPixel = null; + Pixel copyingPixel = null; + int height = pixels.length; + for (int row = top; row < bot; row++) + { + for (int col = right; col < left; col++) + { + if(col < 171|| col > 233){ + lookingatPixel = pixels[row][col]; + copyingPixel = pixels[row - 2*(row-top)][col]; + lookingatPixel.setColor(copyingPixel.getColor()); + } + } + } + } + + public void mirrorGull() + { + int top = 229; + int bot = 330; + int right = 347; + int left = 225; + + Pixel[][] pixels = this.getPixels2D(); + Pixel lookingatPixel = null; + Pixel copyingPixel = null; + + for (int row = top; row < bot; row++) + { + for (int col = left; col < right; col++) + { + lookingatPixel = pixels[row][col]; + copyingPixel = pixels[row][col-120]; + copyingPixel.setColor(lookingatPixel.getColor()); + } + } + } + + /** copy from the passed fromPic to the + * specified startRow and startCol in the + * current picture + * @param fromPic the picture to copy from + * @param startRow the start row to copy to + * @param startCol the start col to copy to */ - 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 width and height - * @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 /////////////////////////////////////// - - /** - * Method to return a string with information about this picture. - * @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; - - } - - /** Method to set the blue to 0 */ - public void zeroBlue() - { - Pixel[][] pixels = this.getPixels2D(); - for (Pixel[] rowArray : pixels) + public void copy(Picture fromPic, + int startRow, int startCol) { - for (Pixel pixelObj : rowArray) - { - pixelObj.setBlue(0); - } + Pixel fromPixel = null; + Pixel toPixel = null; + Pixel[][] toPixels = this.getPixels2D(); + Pixel[][] fromPixels = fromPic.getPixels2D(); + for (int fromRow = 0, toRow = startRow; + fromRow < fromPixels.length && + toRow < toPixels.length; + fromRow++, toRow++) + { + for (int fromCol = 0, toCol = startCol; + fromCol < fromPixels[0].length && + toCol < toPixels[0].length; + fromCol++, toCol++) + { + fromPixel = fromPixels[fromRow][fromCol]; + toPixel = toPixels[toRow][toCol]; + toPixel.setColor(fromPixel.getColor()); + } + } } - } - - /** Method that mirrors the picture around a - * vertical mirror in the center of the picture - * from left to right */ - public void mirrorVertical() - { - Pixel[][] pixels = this.getPixels2D(); - Pixel leftPixel = null; - Pixel rightPixel = null; - int width = pixels[0].length; - for (int row = 0; row < pixels.length; row++) + + public void copy(Picture fromPic, + int toRow, int toCol, int fromRowStart, int fromRowEnd, int fromColStart, int fromColEnd) { - for (int col = 0; col < width / 2; col++) - { - leftPixel = pixels[row][col]; - rightPixel = pixels[row][width - 1 - col]; - rightPixel.setColor(leftPixel.getColor()); - } + Pixel fromPixel = null; + Pixel toPixel = null; + Pixel[][] toPixels = this.getPixels2D(); + Pixel[][] fromPixels = fromPic.getPixels2D(); + for(int toPixelRow = toRow, fromPixelRow = fromRowStart; + fromPixelRow < fromRowEnd; + toPixelRow++, fromPixelRow++){ + for(int toPixelCol = toCol, fromPixelCol = fromColStart; + fromPixelCol < fromColEnd; + toPixelCol++, fromPixelCol++){ + try{ + fromPixel = fromPixels[fromPixelRow][fromPixelCol]; + toPixel = toPixels[toPixelRow][toPixelCol]; + toPixel.setColor(fromPixel.getColor()); + }catch(Exception e){ + toPixelCol+=1000; + fromPixelCol+=1000; + } + } + } } - } - - public void mirrorVerticalRightToLeft() - { - Pixel[][] pixels = this.getPixels2D(); - Pixel leftPixel = null; - Pixel rightPixel = null; - int width = pixels[0].length; - for (int row = 0; row < pixels.length; row++) + + public void copy(Picture fromPic, + int toRow, int toCol, int fromRowStart, int fromRowEnd, int fromColStart, int fromColEnd, int effect) { - for (int col = 0; col < width / 2; col++) - { - leftPixel = pixels[row][col]; - rightPixel = pixels[row][width - 1 - col]; - leftPixel.setColor(rightPixel.getColor()); - } + Pixel fromPixel = null; + Pixel toPixel = null; + Pixel[][] toPixels = this.getPixels2D(); + switch(effect){ + case 0: break; + case 1: fromPic.zeroBlue(); break; + case 2: fromPic.keepOnlyBlue(); break; + case 3: fromPic.negate(); break; + case 4: fromPic.grayScale(); break; + case 5: fromPic.fixUnderwater(); break; + + } + Pixel[][] fromPixels = fromPic.getPixels2D(); + for(int toPixelRow = toRow, fromPixelRow = fromRowStart; + fromPixelRow < fromRowEnd; + toPixelRow++, fromPixelRow++){ + for(int toPixelCol = toCol, fromPixelCol = fromColStart; + fromPixelCol < fromColEnd; + toPixelCol++, fromPixelCol++){ + try{ + fromPixel = fromPixels[fromPixelRow][fromPixelCol]; + toPixel = toPixels[toPixelRow][toPixelCol]; + toPixel.setColor(fromPixel.getColor()); + }catch(Exception e){ + toPixelCol+=1000; + fromPixelCol+=1000; + } + } + } + } + + public void createMagic(){ + Picture a = new Picture("beach.jpg"); + Picture b = new Picture("seagull.jpg"); + + this.copy(a,0,0,0,479,0,639); + this.copy(b,100,100, 229, 330, 225, 347); + + } + + public void myCollage(){ + Random k = new Random(); + + Pixel[][] collage = this.getPixels2D(); + + Picture a = new Picture("beach.jpg"); + Picture b = new Picture("seagull.jpg"); + Picture c = new Picture("snowman.jpg"); + Picture[] pics = {a,b,c}; + int thisheight = collage.length; + int thislength = collage[0].length; + int firsty = 0; + for(int y = 0; ythislength){ + xadd=thislength-x; + } + int yadd = k.nextInt(100); + if(yadd>thisheight){ + yadd=thisheight-y; + } + int piccstart = k.nextInt(539); + int piccend= piccstart+yadd; + int picrstart = k.nextInt(379); + int picrend=picrstart+xadd; + this.copy(pics[whichpic],y+yadd,x+xadd,picrstart,picrend,piccstart,piccend,effect); + firsty=yadd; + x+=xadd; + } + y+=firsty; + } + } - } - - public void mirrorHorizontal() - { - Pixel[][] pixels = this.getPixels2D(); - Pixel topPixel = null; - Pixel bottomPixel = null; - int height = pixels.length; - for (int row = 0; row < height; row++) - { - for (int col = 0; col < pixels[0].length; col++) - { - topPixel = pixels[row][col]; - bottomPixel = pixels[height - 1 - row][col]; - bottomPixel.setColor(topPixel.getColor()); - } - } - } - - public void mirrorHorizontalBottomToTop() - { - Pixel[][] pixels = this.getPixels2D(); - Pixel topPixel = null; - Pixel bottomPixel = null; - int height = pixels.length; - for (int row = 0; row < height; row++) - { - for (int col = 0; col < pixels[0].length; col++) - { - topPixel = pixels[row][col]; - bottomPixel = pixels[height - 1 - row][col]; - topPixel.setColor(bottomPixel.getColor()); - } - } - } - - /** Mirror just part of a picture of a temple */ - public void mirrorTemple() - { - int mirrorPoint = 276; - Pixel leftPixel = null; - Pixel rightPixel = null; - int count = 0; - Pixel[][] pixels = this.getPixels2D(); - - // loop through the rows - for (int row = 27; row < 97; row++) + /** Method to create a collage of several pictures */ + // public void createCollage() + // { + // Picture flower1 = new Picture("flower1.jpg"); + // Picture flower2 = new Picture("flower2.jpg"); + // this.copy(flower1,0,0); + // this.copy(flower2,100,0); + // this.copy(flower1,200,0); + // Picture flowerNoBlue = new Picture(flower2); + // flowerNoBlue.zeroBlue(); + // this.copy(flowerNoBlue,300,0); + // this.copy(flower1,400,0); + // this.copy(flower2,100,0); + // this.copy(flower1,200,0); + // Picture flowerNoBlue = new Picture(flower2); + // + // this.copy(flowerNoBlue,300,0); + // this.copy(flower1,400,0); + // this.copy(flower2,500,0); + // this.mirrorVertical(); + // this.write("collage.jpg"); + // } + + /** Method to show large changes in color + * @param edgeDist the distance for finding edges + */ + public void edgeDetection(int edgeDist) { - // loop from 13 to just before the mirror point - for (int col = 13; col < mirrorPoint; col++) - { - - leftPixel = pixels[row][col]; - rightPixel = pixels[row] - [mirrorPoint - col + mirrorPoint]; - rightPixel.setColor(leftPixel.getColor()); - } + Pixel curPixel = null; + Pixel rightPixel = null; + + Pixel botPixel = null; + Pixel[][] pixels = this.getPixels2D(); + Color rightColor = null; + Color botColor = null; + for (int row = 0; row < pixels.length-1; row++) + { + for (int col = 0; + col < pixels[0].length-1; col++) + { + curPixel = pixels[row][col]; + rightPixel = pixels[row][col+1]; + botPixel = pixels[row+1][col]; + botColor = botPixel.getColor(); + rightColor = rightPixel.getColor(); + if (curPixel.colorDistance(rightColor) > + edgeDist) + curPixel.setColor(Color.BLACK); + else if(curPixel.colorDistance(botColor) > edgeDist){ + curPixel.setColor(Color.BLACK); + }else{ + curPixel.setColor(Color.WHITE); + } + } + } } - } - - /** copy from the passed fromPic to the - * specified startRow and startCol in the - * current picture - * @param fromPic the picture to copy from - * @param startRow the start row to copy to - * @param startCol the start col to copy to - */ - public void copy(Picture fromPic, - int startRow, int startCol) - { - Pixel fromPixel = null; - Pixel toPixel = null; - Pixel[][] toPixels = this.getPixels2D(); - Pixel[][] fromPixels = fromPic.getPixels2D(); - for (int fromRow = 0, toRow = startRow; - fromRow < fromPixels.length && - toRow < toPixels.length; - fromRow++, toRow++) + + public void edgeDetection2() { - for (int fromCol = 0, toCol = startCol; - fromCol < fromPixels[0].length && - toCol < toPixels[0].length; - fromCol++, toCol++) - { - fromPixel = fromPixels[fromRow][fromCol]; - toPixel = toPixels[toRow][toCol]; - toPixel.setColor(fromPixel.getColor()); - } - } - } - - /** Method to create a collage of several pictures */ - public void createCollage() - { - Picture flower1 = new Picture("flower1.jpg"); - Picture flower2 = new Picture("flower2.jpg"); - this.copy(flower1,0,0); - this.copy(flower2,100,0); - this.copy(flower1,200,0); - Picture flowerNoBlue = new Picture(flower2); - flowerNoBlue.zeroBlue(); - this.copy(flowerNoBlue,300,0); - this.copy(flower1,400,0); - this.copy(flower2,500,0); - this.mirrorVertical(); - this.write("collage.jpg"); - } - - - /** Method to show large changes in color - * @param edgeDist the distance for finding edges - */ - public void edgeDetection(int edgeDist) - { - Pixel leftPixel = null; - Pixel rightPixel = null; - Pixel[][] pixels = this.getPixels2D(); - Color rightColor = null; - for (int row = 0; row < pixels.length; row++) + Pixel curPixel = null; + Pixel rightPixel = null; + Pixel leftPixel = null; + Pixel topPixel = null; + Pixel botPixel = null; + Pixel[][] pixels = this.getPixels2D(); + Pixel[][] pixelsOrg= this.getPixels2D(); + + double averageDif = 0; + int totalpixs = 0; + + for (Pixel[] rowArray : pixels) + { + for (Pixel pixelObj : rowArray) + { + totalpixs++; + averageDif+=pixelObj.getAverage(); + } + } + int edgeDist = ((int)averageDif)/totalpixs; + + //System.out.println(totalpixs+" "+averageDif+" "+edgeDist); + for (int row = 1; row < pixels.length-1; row++) + { + for (int col = 1; + col < pixels[0].length-1; col++) + { + int pointave = 0; + try{ + pointave = 0; + for(int i = -10; i < 10; i++){ + for(int k = -10; k < 10; k++){ + pointave+=(int)pixels[i+row][k+col].getAverage(); + } + } + + pointave=(int)pointave/(10*(20*20)); + }catch(Exception e){ + } + + if(pointave > edgeDist){ + pointave=edgeDist; + } + curPixel = pixels[row][col]; + rightPixel = pixelsOrg[row][col+1]; + botPixel = pixelsOrg[row+1][col]; + + + if(//curPixel.colorDistance(leftPixel.getColor())>5|| + curPixel.colorDistance(rightPixel.getColor())>pointave|| + //curPixel.colorDistance(topPixel.getColor())>5|| + curPixel.colorDistance(botPixel.getColor())>pointave){ + + curPixel.setColor(Color.BLACK); + + }else{ + curPixel.setColor(Color.WHITE); + } + } + } + } + + /* Main method for testing - each class in Java can have a main + * method + */ + public static void main(String[] args) { - for (int col = 0; - col < pixels[0].length-1; col++) - { - leftPixel = pixels[row][col]; - rightPixel = pixels[row][col+1]; - rightColor = rightPixel.getColor(); - if (leftPixel.colorDistance(rightColor) > - edgeDist) - leftPixel.setColor(Color.BLACK); - else - leftPixel.setColor(Color.WHITE); - } + Picture beach = new Picture("beach.jpg"); + beach.explore(); + beach.zeroBlue(); + beach.explore(); } - } - - - /* Main method for testing - each class in Java can have a main - * method - */ - public static void main(String[] args) - { - Picture beach = new Picture("beach.jpg"); - beach.explore(); - beach.zeroBlue(); - beach.explore(); - } - + } // this } is the end of class Picture, put all new methods before this diff --git a/classes/PictureExplorer.class b/classes/PictureExplorer.class index 86e91a8..ff58b2e 100644 Binary files a/classes/PictureExplorer.class and b/classes/PictureExplorer.class differ diff --git a/classes/PictureExplorer.java b/classes/PictureExplorer.java index fc47082..dff95b9 100755 --- a/classes/PictureExplorer.java +++ b/classes/PictureExplorer.java @@ -22,7 +22,7 @@ public class PictureExplorer implements MouseMotionListener, ActionListener, Mou // current indicies /** row index */ - private int rowIndex = 0; + private int rowIndex = 0; /** column index */ private int colIndex = 0; @@ -800,12 +800,8 @@ public Component getFirstComponent(Container focusCycleRoot) { */ public static void main( String args[]) { - Picture pix = new Picture("kai.jpg"); - Picture smallP = pix.scale(0.25, 0.25); - smallP.write("smallkai.jpg"); - //pix.explore(); - - //Picture p = new SimplePicture(); + Picture pix = new Picture("seagull.jpg"); + pix.explore(); } } diff --git a/classes/PictureTester.java b/classes/PictureTester.java index 8c86df0..d49bfef 100755 --- a/classes/PictureTester.java +++ b/classes/PictureTester.java @@ -8,104 +8,179 @@ */ public class PictureTester { - /** Method to test zeroBlue */ - public static void testZeroBlue() - { - Picture beach = new Picture("beach.jpg"); - beach.explore(); - beach.zeroBlue(); - beach.explore(); - } - - /** Method to test mirrorVertical */ - public static void testMirrorVertical() - { - Picture caterpillar = new Picture("caterpillar.jpg"); - caterpillar.explore(); - caterpillar.mirrorVertical(); - caterpillar.explore(); - } - - public static void testMirrorVerticalRightToLeft() - { - Picture caterpillar = new Picture("caterpillar.jpg"); - caterpillar.explore(); - caterpillar.mirrorVerticalRightToLeft(); - caterpillar.explore(); - } - - public static void testMirrorHorizontal() - { - Picture caterpillar = new Picture("caterpillar.jpg"); - caterpillar.explore(); - caterpillar.mirrorHorizontal(); - caterpillar.explore(); - } - - public static void testMirrorHorizontalBottomToTop() - { - Picture caterpillar = new Picture("caterpillar.jpg"); - caterpillar.explore(); - caterpillar.mirrorHorizontalBottomToTop(); - caterpillar.explore(); - } - - /** Method to test mirrorTemple */ - public static void testMirrorTemple() - { - Picture temple = new Picture("temple.jpg"); - temple.explore(); - temple.mirrorTemple(); - temple.explore(); - } - - /** Method to test the collage method */ - public static void testCollage() - { - Picture canvas = new Picture("640x480.jpg"); - canvas.createCollage(); - canvas.explore(); - } - - /** Method to test edgeDetection */ - public static void testEdgeDetection() - { - Picture swan = new Picture("swan.jpg"); - swan.edgeDetection(10); - swan.explore(); - } - - /** Main method for testing. Every class can have a main - * method in Java */ - public static void main(String[] args) - { - // uncomment a call here to run a test - // and comment out the ones you don't want - // to run - //testZeroBlue(); - //testKeepOnlyBlue(); - //testKeepOnlyRed(); - //testKeepOnlyGreen(); - //testNegate(); - //testGrayscale(); - //testFixUnderwater(); - //testMirrorVertical(); - //testMirrorVerticalRightToLeft(); - testMirrorHorizontal(); - testMirrorHorizontalBottomToTop(); - //testMirrorTemple(); - //testMirrorArms(); - //testMirrorGull(); - //testMirrorDiagonal(); - //testCollage(); - //testCopy(); - //testEdgeDetection(); - //testEdgeDetection2(); - //testChromakey(); - //testEncodeAndDecode(); - //testGetCountRedOverValue(250); - //testSetRedToHalfValueInTopHalf(); - //testClearBlueOverValue(200); - //testGetAverageForColumn(0); - } + /** Method to test zeroBlue */ + public static void testZeroBlue() + { + Picture beach = new Picture("beach.jpg"); + beach.explore(); + beach.zeroBlue(); + beach.explore(); + } + + /** Method to test mirrorVertical */ + public static void testMirrorVertical() + { + Picture caterpillar = new Picture("beach.jpg"); + caterpillar.explore(); + caterpillar.mirrorVertical(); + caterpillar.explore(); + } + + /** Method to test mirrorTemple */ + public static void testMirrorTemple() + { + Picture temple = new Picture("temple.jpg"); + temple.explore(); + temple.mirrorTemple(); + temple.explore(); + } + + /** Method to test the collage method */ + // public static void testCollage() + // { + // Picture canvas = new Picture("640x480.jpg"); + // canvas.createCollage(); + // canvas.explore(); + // } + + /** Method to test edgeDetection */ + public static void testEdgeDetection() + { + Picture swan = new Picture("swan.jpg"); + swan.edgeDetection(10); + swan.explore(); + } + + public static void testKeepOnlyBlue(){ + + Picture beach = new Picture("beach.jpg"); + beach.explore(); + beach.keepOnlyBlue(); + beach.explore(); + } + + public static void testNegate(){ + + Picture beach = new Picture("beach.jpg"); + beach.explore(); + beach.negate(); + beach.explore(); + } + + public static void testGrayscale(){ + + Picture beach = new Picture("beach.jpg"); + beach.explore(); + beach.grayScale(); + beach.explore(); + } + + public static void testFixUnderwater(){ + + Picture beach = new Picture("water.jpg"); + beach.explore(); + beach.fixUnderwater(); + beach.explore(); + } + + public static void testMirrorVerticalRightToLeft(){ + + Picture beach = new Picture("beach.jpg"); + beach.explore(); + beach.mirrorVerticalRightToLeft(); + beach.explore(); + } + + public static void testMirrorHorizontal() + { + Picture caterpillar = new Picture("beach.jpg"); + caterpillar.explore(); + caterpillar.mirrorHorrizontal(); + caterpillar.explore(); + } + + public static void testMirrorHorizontalBotToTop(){ + Picture c = new Picture("beach.jpg"); + c.explore(); + c.mirrorHorrizontalBotToTop(); + c.explore(); + } + + public static void testMirrorDiagonal(){ + Picture c = new Picture("beach.jpg"); + c.explore(); + c.mirrorDiagonal(); + c.explore(); + } + + public static void testArms(){ + Picture c = new Picture("snowman.jpg"); + c.explore(); + c.mirrorArms(); + c.explore(); + } + + public static void testMirrorGull(){ + Picture c = new Picture("seagull.jpg"); + c.explore(); + c.mirrorGull(); + c.explore(); + } + + public static void testCopy(){ + + Picture c = new Picture("640x480.jpg"); + c.explore(); + c.createMagic(); + c.explore(); + } + public static void testMyCollage(){ + Picture c = new Picture("640x480.jpg"); + c.explore(); + c.myCollage(); + c.explore(); + } + public static void testEdgeDetection2(){ + Picture c = new Picture("swan.jpg"); + Picture d = new Picture("swan.jpg"); + c.explore(); + c.edgeDetection2(); + d.edgeDetection(10); + c.explore(); + d.explore(); + } + + /** Main method for testing. Every class can have a main + * method in Java */ + public static void main(String[] args) + { + // uncomment a call here to run a test + // and comment out the ones you don't want + // to run + //testZeroBlue(); + //testKeepOnlyBlue(); + //testKeepOnlyRed(); + //testKeepOnlyGreen(); + //testNegate(); + //testGrayscale(); + //testFixUnderwater(); + //testMirrorVertical(); + //testMirrorVerticalRightToLeft(); + //testMirrorTemple(); + //testMirrorDiagonal(); + //testMirrorHorizontal(); + //testMirrorArms(); + //testMirrorGull(); + //testMirrorDiagonal(); + //testCollage(); + //testCopy(); + //testEdgeDetection(); + testEdgeDetection2(); + //testChromakey(); + //testEncodeAndDecode(); + //testGetCountRedOverValue(250); + //testSetRedToHalfValueInTopHalf(); + //testClearBlueOverValue(200); + //testGetAverageForColumn(0); + } } \ No newline at end of file diff --git a/classes/doc/Picture.html b/classes/doc/Picture.html index bc97dc6..1910da0 100755 --- a/classes/doc/Picture.html +++ b/classes/doc/Picture.html @@ -2,19 +2,22 @@ - + + -Picture (classes) +Picture - +