From 44e035afae63978d020fbc4dedc63456def66787 Mon Sep 17 00:00:00 2001 From: Dominic Matarese Date: Mon, 12 Jul 2021 15:58:35 +0000 Subject: required first commit --- Random.png | Bin 0 -> 1038035 bytes fractalSquare.png | Bin 0 -> 1502465 bytes input.jpg | Bin 0 -> 33877 bytes main.class | Bin 0 -> 3983 bytes main.java | 162 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ output.jpg | Bin 0 -> 31924 bytes 6 files changed, 162 insertions(+) create mode 100755 Random.png create mode 100755 fractalSquare.png create mode 100755 input.jpg create mode 100755 main.class create mode 100755 main.java create mode 100755 output.jpg diff --git a/Random.png b/Random.png new file mode 100755 index 0000000..5891a67 Binary files /dev/null and b/Random.png differ diff --git a/fractalSquare.png b/fractalSquare.png new file mode 100755 index 0000000..0f57664 Binary files /dev/null and b/fractalSquare.png differ diff --git a/input.jpg b/input.jpg new file mode 100755 index 0000000..8a81c3a Binary files /dev/null and b/input.jpg differ diff --git a/main.class b/main.class new file mode 100755 index 0000000..722a5ce Binary files /dev/null and b/main.class differ diff --git a/main.java b/main.java new file mode 100755 index 0000000..5da4254 --- /dev/null +++ b/main.java @@ -0,0 +1,162 @@ +/*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(); + } + } +} diff --git a/output.jpg b/output.jpg new file mode 100755 index 0000000..6103c14 Binary files /dev/null and b/output.jpg differ -- cgit v1.2.1