// NOTE: This code will not run well in Ed. // It is recommended to download and run the code in jGRASP. import java.util.*; import java.awt.*; public class ImageEdit { public static void main(String[] args) throws InterruptedException { Picture pic = new Picture("suzzallo.jpg"); pic.show(); // makeBlue(pic); // invert(pic); // greyscale(pic); blur(pic, 15); } // This method will turn the given picture BLUE! :D public static void makeBlue(Picture pic) throws InterruptedException { Color[][] pixels = pic.getPixels(); for(int i = 0; i < pixels.length; i++) { for(int j = 0; j < pixels[i].length; j++) { // System.out.println(pixels[i][j]); pixels[i][j] = Color.BLUE; } } // Don't need to know about this material, but it stops the program for 3 seconds Thread.sleep(3000); pic.setPixels(pixels); pic.show(); } // This method will turn the given picture into it's negative version public static void invert(Picture pic) throws InterruptedException { Color[][] pixels = pic.getPixels(); for(int i = 0; i < pixels.length; i++) { for(int j = 0; j < pixels[i].length; j++) { Color color = pixels[i][j]; // RGB values are from 0-255 int red = color.getRed(); int green = color.getGreen(); int blue = color.getBlue(); pixels[i][j] = new Color(255 - red, 255 - green, 255 - blue); } } Thread.sleep(3000); pic.setPixels(pixels); pic.show(); } // This method will turn the given picture into a greyscaled version public static void greyscale(Picture pic) throws InterruptedException { Color[][] pixels = pic.getPixels(); for(int i = 0; i < pixels.length; i++) { for(int j = 0; j < pixels[i].length; j++) { Color color = pixels[i][j]; int red = color.getRed(); int green = color.getGreen(); int blue = color.getBlue(); int avg = (red + green + blue) / 3; pixels[i][j] = new Color(avg, avg, avg); } } Thread.sleep(3000); pic.setPixels(pixels); pic.show(); } public static void blur(Picture pic, int radius) throws InterruptedException { Color[][] pixels = pic.getPixels(); // We need a new copy of the pixels because if we modify the original pixels // and use those modified pixels in our blurring algorithm we will not create // the blur we want. Color[][] copyPixels = pic.getPixels(); for(int i = 0; i < pixels.length; i++) { for(int j = 0; j < pixels[i].length; j++) { int red = 0, green = 0, blue = 0, count = 0; for(int dx = -radius; dx <= radius; dx++) { for(int dy = -radius; dy <= radius; dy++) { if(i + dx >= 0 && i + dx < pixels.length && j + dy >= 0 && j + dy < pixels[i + dx].length) { Color color = pixels[i + dx][j + dy]; red += color.getRed(); green += color.getGreen(); blue += color.getBlue(); count++; } } } pixels[i][j] = new Color(red / count, green / count, blue / count); } } Thread.sleep(3000); pic.setPixels(pixels); pic.show(); } }