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
| public class FractalCircle extends JPanel {
FractalCircle() {
}
Random r1 = new Random();
Random r2 = new Random();
Random r3 = new Random();
private BufferedImage image;
public void dessinerCercle(Graphics g, int x1, int y1, int radius, int level) {
if (level == 0)
return;
int x2 = x1;
int y2 = y1;
radius = radius + 10;
g.setColor(new Color(r1.nextInt(255), r2.nextInt(255), r3.nextInt(255)));
g.drawOval(x1 - radius / 2, y1 - radius / 2, radius, radius);
dessinerCercle(g, x2, y2, radius, level - 1);
}
public void paint(Graphics g) {
super.paint(g);
if ( image==null && (getWidth()>0 && getHeight()>0)) {
image = dessineImage(getWidth(),getHeight());
}
if ( image!=null ) {
g.drawImage(image, 0, 0, null);
}
}
private BufferedImage dessineImage(int width, int height) {
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
try {
g.setColor(getBackground());
g.fillRect(0, 0, width, height);
dessinerCercle(g, width/2, height/2, 5, 200);
g.drawLine(0, 0, width, height);
}
finally {
g.dispose();
}
return image;
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(500,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.add(new FractalCircle());
frame.setVisible(true);
}
} |