aboutsummaryrefslogtreecommitdiff
path: root/main.java
blob: 5da42547fb432ca8a5c1713955b86449ecab1859 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
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();
	  }
  }
}