Blame view

src/collembola/CollemBuffer2.java 1.52 KB
89f70c1ec   glaville   import current mc...
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
  package collembola;
  
  import java.awt.Color;
  import java.awt.Graphics2D;
  import java.awt.Shape;
  import java.awt.geom.Rectangle2D;
  import java.awt.image.BufferedImage;
  import java.io.File;
  import java.io.IOException;
  import java.util.Random;
  
  import javax.imageio.ImageIO;
  
  public class CollemBuffer2 {
  	
  	private final int height;
  	private final int width;
  	private final int nbPlots;
  	
  	private BufferedImage image;
  	
  	public CollemBuffer2(int height, int width, int nbPlots) {
  		this.height = height;
  		this.width = width;
  		this.nbPlots = nbPlots;
  		
  		this.image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
  		
  		Graphics2D g2d = image.createGraphics();
  		Random rng = new Random();
  		
  		int avgHeight = (int) Math.ceil(height / Math.sqrt(nbPlots));
  		int avgWidth  = (int) Math.ceil(width / Math.sqrt(nbPlots));
  		
  		for (int i = 0; i < nbPlots; i++) {
  			float randomX = rng.nextFloat() * height;
  			float randomY = rng.nextFloat() * width;
  			float h = rng.nextFloat() * 3 * avgHeight;
  			float w = rng.nextFloat() * 3 * avgWidth;
  			
  			//Ellipse2D e2d = new Ellipse2D.Double(randomX - height / 8, randomY - height / 8, height / 4, height / 4);
  			Shape sh = new Rectangle2D.Float(randomX - w / 2, randomY - h / 2, w, 2 * h);
  			g2d.setColor(new Color(rng.nextInt()));
  			g2d.fill(sh);
  		}
  		
  		try {
  			ImageIO.write(image, "png", new File("test.png"));
  		} catch (IOException e) {
  			e.printStackTrace();
  		}
  	}
  	
  	public static void main(String[] args) {
  		System.out.println("toto");
  		new CollemBuffer2(254, 254, 137);
  	}
  
  }