I need to do a java code with Threads (implements Runnable()) for a college assigment. I need to draw 4 fractals at the same time ( with thread.sleep()). I already tried almost everything I knew, and this still doesn't working. So I cleaned up the source.
I have four classes ( four fractals). In my JPanel, i call a paint method to draw them ( they are recursive ). Can anyone save me please?
public class MainPanel extends JPanel {
FractalTree tree = new FractalTree();
FractalCircle circle = new FractalCircle();
FractalSquare square = new FractalSquare();
FractalCircle2 circle2 = new FractalCircle2();
@Override
public void paint(Graphics g) {
setBackground(Color.black,g);
Graphics2D g2 = (Graphics2D) g;
g.setColor(Color.WHITE);
DrawBounds(g);
tree.drawTree(g,200,290,-90,9);
circle.drawCircle(g2,675,175,300);
square.drawSquares(g, 200, 525, 100,7);
circle2.drawCircle(g2,675,518,300);
}
public void DrawBounds(Graphics g){
g.drawLine(0,350,900,350);
g.drawLine(450,0,450,700);
}
public void setBackground(Color c,Graphics g){
g.fillRect(0, 0, 900, 700);
}
}
public class FractalSquare{
public void drawSquares(Graphics g,int x, int y, int side ,int size){
g.setColor(Color.BLUE);
if(size >2){
size--;
g.fillRect(x-side/2, y-side/2, side, side);
side = side/2;
x = x-side;
y = y-side;
drawSquares(g,x,y,side,size);
drawSquares(g,x+side*2,y,side,size);
drawSquares(g,x,y+side*2,side,size);
drawSquares(g,x+side*2,y+side*2,side,size);
} else return;
}
}
public class FractalCircle {
public void drawCircle(Graphics2D g, float x, float y, float radius) {
g.setColor(Color.RED);
g.draw(new Ellipse2D.Float(x-radius/2, y-radius/2, radius,radius));
if(radius > 2) {
radius *= 0.75f;
drawCircle(g,x, y, radius);
} else return ;
}
}
public class FractalCircle2 {
public void drawCircle(Graphics2D g, float x, float y, float radius) {
Color color = new Color(255,0,255);
g.setColor(color);
g.draw(new Ellipse2D.Float(x-radius/2, y-radius/2, radius,radius));
if(radius > 1) {
radius *= 0.75f;
drawCircle(g,x + radius/2, y, radius/2);
drawCircle(g,x - radius/2, y, radius/2);
} else return ;
}
}
public class FractalTree {
public void drawTree(Graphics g, int x1, int y1, double angle, int depth) {
g.setColor(Color.GREEN);
if (depth == 0) return;
int x2 = x1 + (int) (Math.cos(Math.toRadians(angle)) * depth * 5.0);
int y2 = y1 + (int) (Math.sin(Math.toRadians(angle)) * depth * 5.0);
g.drawLine(x1, y1, x2, y2);
drawTree(g, x2, y2, angle - 20, depth - 1);
drawTree(g, x2, y2, angle + 20, depth - 1);
}
}