![]() |
Матрица статей Список статей Всячина Контакты | ||||||||||||
|
Фрактальные кластеры
package ru.xaoc.fractalworld.dla;
import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.io.*;
import java.util.Random;
import javax.swing.*;
public class Main {
static final int WIDTH = 512;
static final int HEIGHT = 512;
static final int COUNT = 100000;
static BufferedImage image;
static Graphics2D graphics;
private static void drawDLA() {
Random rnd = new Random();
for (int i = 0; i < COUNT; ++i) {
int x = WIDTH / 2;
int y = HEIGHT / 2;
int dx = 1;
int dy = 1;
while (image.getRGB(x, y) != Color.BLACK.getRGB()) {
dx = rnd.nextInt(3) - 1;
dy = rnd.nextInt(3) - 1;
x += dx;
y += dy;
}
image.setRGB(x - dx, y - dy, Color.BLACK.getRGB());
System.out.println(i);
}
}
public static void main(String[] args) throws IOException {
image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
graphics = image.createGraphics();
graphics.setColor(Color.WHITE);
graphics.fill(new Rectangle2D.Double(0, 0, WIDTH, HEIGHT));
graphics.setColor(Color.BLACK);
graphics.drawRect(1, 1, WIDTH - 2, HEIGHT - 2);
drawDLA();
JFrame frame = new JFrame();
frame.addNotify();
frame.setSize(frame.getInsets().left
+ frame.getInsets().right + WIDTH,
frame.getInsets().top
+ frame.getInsets().bottom + HEIGHT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JPanel() {
@Override
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
if (image != null) {
g2.drawImage(image, 0, 0, null);
}
}
});
frame.setVisible(true);
}
}
Ссылки:
|