Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.image.*;
- import javax.imageio.*;
- import java.io.*;
- import java.awt.*;
- import java.lang.Math;
- class Main {
- static int iter = 10;
- public static void main(String[] args) {
- BufferedImage bi = new BufferedImage(500, 500, BufferedImage.TYPE_INT_ARGB);
- for (int y = 0; y < 500; y++) {
- for (int x = 0; x < 500; x++) {
- //bi.setRGB(x, y, (int)(Math.random()*-16777217));
- //bi.setRGB(x, y, new Color((int)(Math.random()*256), (int)(Math.random()*256), (int)(Math.random()*256)).getRGB());
- bi.setRGB(x, y, -1);
- }
- }
- int sqNum = 100;
- int[][] squares = new int[sqNum][4];
- for (int i = 0; i < sqNum; i++) {
- squares[i][0] = (int)(Math.random()*500+.5f); //x
- squares[i][1] = (int)(Math.random()*500+.5f); //y
- squares[i][2] = (int)(Math.random()*100+.5f); //w
- squares[i][3] = (int)(Math.random()*100+.5f); //h
- }
- for (int i = 0; i < sqNum; i++) {
- for (int y = squares[i][1]-squares[i][3]; y <= squares[i][1]+squares[i][3]; y++) {
- for (int x = squares[i][0]-squares[i][2]; x <= squares[i][0]+squares[i][2]; x++) {
- if (x >= 0 && x < 500 && y >= 0 && y < 500) {
- if (x == squares[i][0]-squares[i][2] || x == squares[i][0]+squares[i][2] || y == squares[i][1]-squares[i][3] || y == squares[i][1]+squares[i][3]) {
- bi.setRGB(x, y, -16777216);
- } else {
- bi.setRGB(x, y, -1);
- }
- }
- }
- }
- }
- try {
- ImageIO.write(bi, "png", new File("C:\\Users\\Connor\\Desktop\\pic_0.png"));
- } catch(IOException e) {
- }
- /*for (int n = 0; n < 10; n++) {
- for (int i = 0; i < iter; i++) {
- bi = smooth(bi);
- }
- try {
- ImageIO.write(bi, "png", new File("C:\\Users\\Connor\\Desktop\\pic_" + (iter*(n+1)) + ".png"));
- } catch(IOException e) {
- }
- }*/
- }
- public static BufferedImage smooth(BufferedImage bi) {
- BufferedImage img = new BufferedImage(bi.getWidth(), bi.getHeight(), bi.getType());
- for (int y = 0; y < 500; y++) {
- for (int x = 0; x < 500; x++) {
- img.setRGB(x, y, getAverageColor(x, y, bi));
- }
- }
- return img;
- }
- public static int getAverageColor(int x, int y, BufferedImage template) {
- int[] n = new int[3];
- int d = 0;
- if (x > 0) {
- Color col = new Color(template.getRGB(x-1, y));
- n[0]+=col.getRed();
- n[1]+=col.getGreen();
- n[2]+=col.getBlue();
- d++;
- }
- if (x < template.getWidth()-1) {
- Color col = new Color(template.getRGB(x+1, y));
- n[0]+=col.getRed();
- n[1]+=col.getGreen();
- n[2]+=col.getBlue();
- d++;
- }
- if (y > 0) {
- Color col = new Color(template.getRGB(x, y-1));
- n[0]+=col.getRed();
- n[1]+=col.getGreen();
- n[2]+=col.getBlue();
- d++;
- }
- if (y < template.getHeight()-1) {
- Color col = new Color(template.getRGB(x, y+1));
- n[0]+=col.getRed();
- n[1]+=col.getGreen();
- n[2]+=col.getBlue();
- d++;
- }
- return new Color((int)(n[0]*1f/d+.5), (int)(n[1]*1f/d+.5), (int)(n[2]*1f/d+.5)).getRGB();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement