/*goals: create methods to generate a random image convert an image to grayscale generate a fractal art Dominic Matarese COP3503 */ import java.awt.*; import java.awt.geom.Ellipse2D; import java.awt.geom.Line2D; import java.awt.image.BufferedImage; import java.awt.image.ImageObserver; import java.io.*; import javax.imageio.ImageIO; import javax.swing.JFrame; public class main { public static void main(String[] args) throws IOException { generateRandom(720, 480); generateFractal(2000, 10); convertGrayscale(); System.out.println("Done!"); } public static void generateFractal(int size, int level) throws IOException { BufferedImage image = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB); Graphics2D g2d = image.createGraphics(); Point p1 = new Point(0,0); Point p2 = new Point(0,size); Point p3 = new Point(size,0); Point p4 = new Point(size,size); drawImage(level, g2d, p1, p2, p3, p4); g2d.dispose(); ImageIO.write(image, "png", new File("fractalSquare.png")); } public static void drawImage(int level, Graphics2D g2d, Point p1, Point p2, Point p3, Point p4) { if (level == 1) { //base case, do nothing return; } else { Point p5 = midpoint(p1, p3); Point p6 = midpoint(p3, p4); Point p7 = midpoint(p2, p4); Point p8 = midpoint(p1, p2); Point p9 = midpoint(p1, p4); //generate random colors int r = (int)(Math.random()*256); int g = (int)(Math.random()*256); int b = (int)(Math.random()*256); Color color = new Color(r, g, b); g2d.setColor(color); //g2d.setColor(new Color(p4.x)); // This will set the color to blue, mostly Polygon square = new Polygon(); square.addPoint(p8.x, p8.y); square.addPoint(p2.x, p2.y); square.addPoint(p7.x, p7.y); square.addPoint(p9.x, p9.y); Polygon triangle = new Polygon(); triangle.addPoint(p1.x, p1.y); triangle.addPoint(p9.x, p9.y); triangle.addPoint(p3.x, p3.y); //uncomment the square polygon and comment the triangle for a square fractal //the triangle looks slightly more interesting //g2d.fillPolygon(square); g2d.fillPolygon(triangle); //begin recursion drawImage(level-1, g2d, p1, p8, p5, p9); drawImage(level-1, g2d, p8, p2, p9, p7); drawImage(level-1, g2d, p9, p7, p6, p4); drawImage(level-1, g2d, p5, p9, p3, p6); } } public static Point midpoint(Point p1, Point p2) { Point midpoint = new Point((p1.x + p2.x) / 2, (p1.y + p2.y) / 2); return midpoint; } public static void generateRandom(int width, int height) { BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); //generate random RGB values for each pixel for(int x = 0; x < width; x++) { for(int y = 0; y < height; y++) { int r = (int)(Math.random()*256); int g = (int)(Math.random()*256); int b = (int)(Math.random()*256); Color color = new Color(r, g, b); image.setRGB(x, y, color.getRGB()); } } try { File random = new File("Random.png"); ImageIO.write(image, "png", random); } catch (IOException e){ return; //does nothing if exception occurs } } public static void convertGrayscale() { File f = new File("input.jpg"); BufferedImage image = null; try { image = ImageIO.read(f); int width = image.getWidth(); int height = image.getHeight(); for(int y = 0; y < height; y++){ for(int x = 0; x < width; x++){ int p = image.getRGB(x,y); int a = (p>>24)&0xff; int r = (p>>16)&0xff; int g = (p>>8)&0xff; int b = p&0xff; int avg = (r+g+b)/3; //setting the value of each RGB to the average p = (a<<24) | (avg<<16) | (avg<<8) | avg; image.setRGB(x, y, p); } } } catch (IOException e) { e.printStackTrace(); //Eclipse default catch operation } try { File fOutput = new File("output.jpg"); ImageIO.write(image, "jpg", fOutput); } catch(IOException e) { e.printStackTrace(); } } }