aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDominic Matarese <dominicmatarese@gmail.com>2021-07-12 15:58:35 +0000
committerDominic Matarese <dominicmatarese@gmail.com>2021-07-12 15:58:35 +0000
commit44e035afae63978d020fbc4dedc63456def66787 (patch)
tree0ede1d2a0dcc528ab7f3ba684cad42529c2f16fe
required first commit
-rwxr-xr-xRandom.pngbin0 -> 1038035 bytes
-rwxr-xr-xfractalSquare.pngbin0 -> 1502465 bytes
-rwxr-xr-xinput.jpgbin0 -> 33877 bytes
-rwxr-xr-xmain.classbin0 -> 3983 bytes
-rwxr-xr-xmain.java162
-rwxr-xr-xoutput.jpgbin0 -> 31924 bytes
6 files changed, 162 insertions, 0 deletions
diff --git a/Random.png b/Random.png
new file mode 100755
index 0000000..5891a67
--- /dev/null
+++ b/Random.png
Binary files differ
diff --git a/fractalSquare.png b/fractalSquare.png
new file mode 100755
index 0000000..0f57664
--- /dev/null
+++ b/fractalSquare.png
Binary files differ
diff --git a/input.jpg b/input.jpg
new file mode 100755
index 0000000..8a81c3a
--- /dev/null
+++ b/input.jpg
Binary files differ
diff --git a/main.class b/main.class
new file mode 100755
index 0000000..722a5ce
--- /dev/null
+++ b/main.class
Binary files 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
--- /dev/null
+++ b/output.jpg
Binary files differ