SuF
Legend
- Joined
- Jan 21, 2007
- Sythe Gold
- 1,234
- Messages
- 14,211
Thought I might as well post my code and stoof here. I'm slowly turning my shitty fractal creator (the fractal is just squares that split into smaller squares depending on the color) into one that you can create your own fractal very easily. The code is quite bad as I'm not done yet and I just want it to work at this point.
Just post any suggestions, questions, comments, etc.
What the GUI will look like eventually:
FractalCreator.java
FractalWindow.java
JFractal.java
square.java
EnhancedColor.java
FractalCanvas.java
SquareCanvas.java
Just post any suggestions, questions, comments, etc.
What the GUI will look like eventually:
FractalCreator.java
Code:
import java.awt.Color;
public class FracalCreator
{
private static final EnhancedColor RED = new EnhancedColor(Color.RED);
private static final EnhancedColor BLUE = new EnhancedColor(Color.BLUE);
private static final EnhancedColor GREEN = new EnhancedColor(Color.GREEN);
private static final EnhancedColor YELLOW = new EnhancedColor(Color.YELLOW);
private static final EnhancedColor BLACK = new EnhancedColor(Color.BLACK);
private static final EnhancedColor CYAN = new EnhancedColor(Color.CYAN);
private static final EnhancedColor PINK = new EnhancedColor(Color.PINK);
private static final EnhancedColor MAGENTA = new EnhancedColor(Color.MAGENTA);
private static final EnhancedColor ORANGE = new EnhancedColor(Color.ORANGE);
public static EnhancedColor[] COLORS = { RED, BLUE, GREEN, YELLOW, BLACK, CYAN, PINK, MAGENTA, ORANGE };
public static void main(String[] args) throws InterruptedException
{
//Old Green Fractal
RED.setItterate(GREEN);
BLUE.setItterate(CYAN, BLUE, BLUE, CYAN);
GREEN.setItterate(GREEN, GREEN, BLUE, GREEN);
YELLOW.setItterate(GREEN, ORANGE, ORANGE, BLACK);
BLACK.setItterate(GREEN, BLUE, BLUE, GREEN);
CYAN.setItterate(YELLOW, CYAN, RED, YELLOW);
PINK.setItterate(CYAN, YELLOW, BLUE, GREEN);
MAGENTA.setItterate(CYAN, YELLOW, BLUE, GREEN);
ORANGE.setItterate(ORANGE, CYAN, CYAN, ORANGE);
// FILL NUM IN WITH THE NUMBER OF ITTERATION YOU WANT
int num = 9;
// FILL IN WITH COLOR YOU WANT TO SEED WITH
EnhancedColor seed = RED;
Square b = new Square(0, 0, (int) Math.pow(2, num), seed);
JFractal a = new JFractal(b);
new FractalWindow(a);
}
}
FractalWindow.java
Code:
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
@SuppressWarnings("serial")
public class FractalWindow extends JFrame implements ChangeListener
{
private SquareCanvas square;
private FractalCanvas fractal;
private JFractal frac;
private JSlider slider;
public FractalWindow(JFractal frac)
{
this.frac = frac;
this.setLayout(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setResizable(false);
slider = new JSlider(0, 9);
slider.setPaintLabels(true);
slider.setPaintTicks(true);
slider.setSnapToTicks(true);
slider.setMinorTickSpacing(1);
slider.setLocation(560, 50);
slider.setSize(200, 50);
slider.addChangeListener(this);
this.add(slider);
fractal = new FractalCanvas(frac);
fractal.setSize(512, 512);
fractal.setLocation(0, 0);
this.add(fractal);
square = new SquareCanvas();
square.setSize(256, 256);
square.setLocation(512, 256);
this.add(square);
this.setVisible(true);
Insets insets = this.getInsets();
this.setSize(insets.left + insets.right + 768, insets.top + insets.bottom + 512);
slider.setValue(9);
}
public void paint(Graphics g)
{
EnhancedColor RED = new EnhancedColor(Color.RED);
EnhancedColor BLUE = new EnhancedColor(Color.BLUE);
RED.setItterate(RED, BLUE, BLUE, RED);
BLUE.setItterate(BLUE);
super.paint(g);
fractal.repaint();
square.showSquare(RED);
}
public void stateChanged(ChangeEvent e)
{
frac.reset();
for(int i = 0; i < slider.getValue(); i++)
{
frac.itterate();
}
repaint();
}
}
JFractal.java
Code:
import java.util.ArrayList;
public class JFractal
{
private ArrayList<Square> squares = new ArrayList<Square>();
private final Square seed;
public JFractal(Square seed)
{
this.seed = seed;
squares.add(seed);
}
public void itterate()
{
ArrayList<Square> temp = new ArrayList<Square>();
for (int a = squares.size() - 1; a >= 0; a--)
{
Square[] b = squares.get(a).itterate();
for(Square c: b)
{
temp.add(c);
}
squares.remove(a);
}
squares = temp;
}
public ArrayList<Square> getSquares()
{
return squares;
}
public void reset()
{
squares.clear();
squares.add(seed);
}
}
square.java
Code:
import java.awt.Color;
import java.awt.geom.Rectangle2D;
public class Square
{
private Rectangle2D.Float rectangle;
private EnhancedColor color;
public Square(float x, float y, float size, EnhancedColor color)
{
rectangle = new Rectangle2D.Float(x, y, size, size);
this.color = color;
}
public Square[] itterate()
{
EnhancedColor[] rule = color.getRule();
if (color.split())
{
Square[] temp = new Square[4];
float half = getSize() / 2;
temp[0] = new Square(getX(), getY(), half, rule[0]);
temp[1] = new Square(getX() + half, getY(), half, rule[1]);
temp[2] = new Square(getX(), getY() + half, half, rule[2]);
temp[3] = new Square(getX() + half, getY() + half, half, rule[3]);
return temp;
}
Square[] temp = new Square[1];
temp[0] = new Square(getX(), getY(), getSize(), rule[0]);
return temp;
}
public float getX()
{
return rectangle.x;
}
public float getY()
{
return rectangle.y;
}
public float getSize()
{
return rectangle.width;
}
public Rectangle2D.Float getRect()
{
return rectangle;
}
public Color getColor()
{
return color.getBaseColor();
}
}
EnhancedColor.java
Code:
import java.awt.Color;
public class EnhancedColor
{
private Color baseColor;
private EnhancedColor[] itt;
private boolean split;
public EnhancedColor(Color baseColor)
{
this.baseColor = baseColor;
}
public EnhancedColor[] getRule()
{
return itt;
}
public boolean split()
{
return split;
}
public void setItterate(EnhancedColor... itt)
{
if(itt.length >= 4)
{
this.itt = new EnhancedColor[4];
this.itt = itt;
split = true;
}
else
{
this.itt = new EnhancedColor[1];
this.itt = itt;
split = false;
}
}
public Color getBaseColor()
{
return baseColor;
}
}
FractalCanvas.java
Code:
import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.ArrayList;
@SuppressWarnings("serial")
public class FractalCanvas extends Canvas
{
private JFractal frac;
public FractalCanvas(JFractal frac)
{
this.frac = frac;
}
public void paint(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
ArrayList<Square> temp = frac.getSquares();
for (int i = 0; i < temp.size(); i++)
{
g2.setColor(temp.get(i).getColor());
g2.fill(temp.get(i).getRect());
}
}
}
SquareCanvas.java
Code:
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
public class SquareCanvas extends Canvas
{
EnhancedColor square = null;
public void paint(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
if (square != null)
{
EnhancedColor[] temp = square.getRule();
if (temp.length > 1)
{
g2.setColor(temp[0].getBaseColor());
g2.fillRect(0, 0, getWidth() / 2, getHeight() / 2);
g2.setColor(temp[1].getBaseColor());
g2.fillRect(getWidth() / 2, 0, getWidth() / 2, getHeight() / 2);
g2.setColor(temp[2].getBaseColor());
g2.fillRect(0, getHeight() / 2, getWidth() / 2, getHeight() / 2);
g2.setColor(temp[3].getBaseColor());
g2.fillRect(getWidth() / 2, getHeight() / 2, getWidth() / 2, getHeight() / 2);
}
else
{
g2.setColor(temp[0].getBaseColor());
g2.fillRect(0, 0, getWidth(), getHeight());
}
}
else
{
g2.setColor(Color.WHITE);
g2.fillRect(0, 0, this.getWidth(), this.getHeight());
}
}
public void showSquare(EnhancedColor color)
{
this.square = color;
repaint();
}
}


