/* CMPT 300X Chaos Working with Fractals: Iterated Circle
*
* rewrite of Sierpinski Square code to iterate a circular fractal
Author: Daniel Tanner
*Note you can mess around with the code here a bit to avoid printing circles inside each other to generate a different fractal pattern (which I kinda like better but... eh).
*/
import javax.swing.JFrame; //imports for the frame
import java.awt.*; //imports the colour
import java.awt.Graphics; //imports the graphics
import java.util.Random; // imports the random class for random colour
import javax.swing.JPanel;
public class IteratedCircle extends JPanel
{
public Graphics g;
public Color c =Color.WHITE; // set colour to white to create the blanks
public Random r = new Random();
public static void main (String[]args)
{
JFrame SS = new JFrame("Drawing with Recursion");
SS.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
SS.setVisible(true); //Show the first drawing.
IteratedCircle S = new IteratedCircle(); //call default constructor (not written);
SS.setBackground(Color.BLACK);
SS.add(S);
SS.setSize(500, 500); //(width ,height ) */
//new SFrame(); // call to the frame, program
}
public void paint(Graphics g)
{
this.g = g;
// start recursion with first call to draw square
drawCircle( getWidth() / 2, getHeight() / 2, getWidth() / 4);
}
private void drawCircle ( int x, int y ,int radius)
{
if (radius > 3) //control when to stop: when size is 2x2 pixels
{
g.setColor(new Color((int)(Math.random() * 255), (int)(Math.random() * 255), (int)(Math.random() * 255))); //random colour (default is white)
g.drawRoundRect(x - radius / 2, y - radius / 2, radius, radius, radius * radius, radius / 2);
drawCircle((int)(x + radius * .75), y, radius / 2); //draws and fill the square, bigger one
drawCircle((int)(x - radius * .75), y, radius / 2);
drawCircle(x, (int)(y + radius * .75), radius / 2);
drawCircle(x, (int)(y - radius * .75), radius / 2);
//repaint();
}
}
private void drawUpCircle( int x, int y ,int radius)
{
if (radius > 1) //control when to stop: when size is 2x2 pixels
{
g.setColor(new Color((int)(Math.random() * 255), (int)(Math.random() * 255), (int)(Math.random() * 255))); //random colour (default is white)
g.drawRoundRect(x - radius / 2, y - radius / 2, radius, radius, radius * radius, radius / 2);
drawUpCircle(x, (int)(y + radius * .75), radius / 2);
drawRightCircle((int)(x + radius * .75), y, radius / 2);
drawLeftCircle((int)(x - radius * .75), y, radius / 2);
//repaint();
}
}
private void drawDownCircle( int x, int y ,int radius)
{
if (radius > 1) //control when to stop: when size is 2x2 pixels
{
g.setColor(new Color((int)(Math.random() * 255), (int)(Math.random() * 255), (int)(Math.random() * 255))); //random colour (default is white)
g.drawRoundRect(x - radius / 2, y - radius / 2, radius, radius, radius * radius, radius / 2);
drawDownCircle(x, (int)(y - radius * .75), radius / 2);
drawRightCircle((int)(x + radius * .75), y, radius / 2);
drawLeftCircle((int)(x - radius * .75), y, radius / 2);
//repaint();
}
}
private void drawLeftCircle( int x, int y ,int radius)
{
if (radius > 1) //control when to stop: when size is 2x2 pixels
{
g.setColor(new Color((int)(Math.random() * 255), (int)(Math.random() * 255), (int)(Math.random() * 255))); //random colour (default is white)
g.drawRoundRect(x - radius / 2, y - radius / 2, radius, radius, radius * radius, radius / 2);
drawUpCircle(x, (int)(y + radius * .75), radius / 2);
drawDownCircle(x, (int)(y - radius * .75), radius / 2);
drawLeftCircle((int)(x - radius * .75), y, radius / 2);
//repaint();
}
}
private void drawRightCircle( int x, int y ,int radius)
{
if (radius > 1) //control when to stop: when size is 2x2 pixels
{
g.setColor(new Color((int)(Math.random() * 255), (int)(Math.random() * 255), (int)(Math.random() * 255))); //random colour (default is white)
g.drawRoundRect(x - radius / 2, y - radius / 2, radius, radius, radius * radius, radius / 2);
drawUpCircle(x, (int)(y + radius * .75), radius / 2);
drawRightCircle((int)(x + radius * .75), y, radius / 2);
drawDownCircle(x, (int)(y - radius * .75), radius / 2);
//repaint();
}
}
}//end of class
Friday Night Open Thread: Motivation
6 hours ago
No comments:
Post a Comment