Advertisement
CaptainSpaceCat

Squares.

Jul 26th, 2016
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.23 KB | None | 0 0
  1. import java.awt.image.*;
  2. import javax.imageio.*;
  3. import java.io.*;
  4. import java.awt.*;
  5. import java.lang.Math;
  6.  
  7. class Main {
  8.  
  9.   static int iter = 10;
  10.  
  11.   public static void main(String[] args) {
  12.     BufferedImage bi = new BufferedImage(500, 500, BufferedImage.TYPE_INT_ARGB);
  13.    
  14.     for (int y = 0; y < 500; y++) {
  15.       for (int x = 0; x < 500; x++) {
  16.         //bi.setRGB(x, y, (int)(Math.random()*-16777217));
  17.         //bi.setRGB(x, y, new Color((int)(Math.random()*256), (int)(Math.random()*256), (int)(Math.random()*256)).getRGB());
  18.         bi.setRGB(x, y, -1);
  19.       }
  20.     }
  21.    
  22.     int sqNum = 100;
  23.     int[][] squares = new int[sqNum][4];
  24.    
  25.     for (int i = 0; i < sqNum; i++) {
  26.       squares[i][0] = (int)(Math.random()*500+.5f); //x
  27.       squares[i][1] = (int)(Math.random()*500+.5f); //y
  28.       squares[i][2] = (int)(Math.random()*100+.5f); //w
  29.       squares[i][3] = (int)(Math.random()*100+.5f); //h
  30.     }
  31.    
  32.     for (int i = 0; i < sqNum; i++) {
  33.      
  34.       for (int y = squares[i][1]-squares[i][3]; y <= squares[i][1]+squares[i][3]; y++) {
  35.         for (int x = squares[i][0]-squares[i][2]; x <= squares[i][0]+squares[i][2]; x++) {
  36.           if (x >= 0 && x < 500 && y >= 0 && y < 500) {
  37.             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]) {
  38.               bi.setRGB(x, y, -16777216);
  39.             } else {
  40.               bi.setRGB(x, y, -1);
  41.             }
  42.           }
  43.         }
  44.       }
  45.      
  46.     }
  47.    
  48.     try {
  49.       ImageIO.write(bi, "png", new File("C:\\Users\\Connor\\Desktop\\pic_0.png"));
  50.     } catch(IOException e) {
  51.     }
  52.    
  53.     /*for (int n = 0; n < 10; n++) {
  54.       for (int i = 0; i < iter; i++) {
  55.         bi = smooth(bi);
  56.       }
  57.      
  58.       try {
  59.         ImageIO.write(bi, "png", new File("C:\\Users\\Connor\\Desktop\\pic_" + (iter*(n+1)) + ".png"));
  60.       } catch(IOException e) {
  61.       }
  62.     }*/
  63.    
  64.   }
  65.  
  66.   public static BufferedImage smooth(BufferedImage bi) {
  67.     BufferedImage img = new BufferedImage(bi.getWidth(), bi.getHeight(), bi.getType());
  68.     for (int y = 0; y < 500; y++) {
  69.       for (int x = 0; x < 500; x++) {
  70.         img.setRGB(x, y, getAverageColor(x, y, bi));
  71.       }
  72.     }
  73.     return img;
  74.   }
  75.  
  76.   public static int getAverageColor(int x, int y, BufferedImage template) {
  77.     int[] n = new int[3];
  78.     int d = 0;
  79.     if (x > 0) {
  80.       Color col = new Color(template.getRGB(x-1, y));
  81.       n[0]+=col.getRed();
  82.       n[1]+=col.getGreen();
  83.       n[2]+=col.getBlue();
  84.       d++;
  85.     }
  86.     if (x < template.getWidth()-1) {
  87.       Color col = new Color(template.getRGB(x+1, y));
  88.       n[0]+=col.getRed();
  89.       n[1]+=col.getGreen();
  90.       n[2]+=col.getBlue();
  91.       d++;
  92.     }
  93.     if (y > 0) {
  94.       Color col = new Color(template.getRGB(x, y-1));
  95.       n[0]+=col.getRed();
  96.       n[1]+=col.getGreen();
  97.       n[2]+=col.getBlue();
  98.       d++;
  99.     }
  100.     if (y < template.getHeight()-1) {
  101.       Color col = new Color(template.getRGB(x, y+1));
  102.       n[0]+=col.getRed();
  103.       n[1]+=col.getGreen();
  104.       n[2]+=col.getBlue();
  105.       d++;
  106.     }
  107.     return new Color((int)(n[0]*1f/d+.5), (int)(n[1]*1f/d+.5), (int)(n[2]*1f/d+.5)).getRGB();
  108.   }
  109.  
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement