0
votes

I am trying to draw the following fractal pattern of circles along a line. It draws a circle with a radius of n, and a center point on an invisible line. It then recursively draws two circles with a radius of n/2 and center points on line where the circles cross the line. Fractal Circles This is very confusing to me as the Java graphics code for drawing circles requires you to use "instance.drawOval(int x1, int x1, int height, int width) - which says nothing about the center of the circle.

I have tried to take a stab at it and this is what I have come up with so far. I have only tried to draw the first circle and then the two to the right and left. I have not done any of the recursion as of yet. I am simply trying to draw two circles to the left and right whose center points collide with the left and right of the largest circle.

 package fractalcircles;

 import java.awt.*;
 import javax.swing.*;

 public class FractalCircles {

 /**
 * @param args the command line arguments
 */
public static void main(String[] args) 
{   //create a MyCanvas object
    MyCanvas canvas1 = new MyCanvas();

    //set up a JFrame to hold the canvas
    JFrame frame = new JFrame();
    frame.setTitle("FractalCircles.java");
    frame.setSize(500,500);
    frame.setLocation(100,100);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //add the canvas to the frame as a content panel
    frame.getContentPane().add(canvas1);
    frame.setVisible(true);
 }//end main
 }//end class

 class MyCanvas extends Canvas
 {
public MyCanvas()
{} //end MyCanvas() constructor

//this method will draw the initial circle and invisible line
public void paint (Graphics graphics)
{
    int n=50; //radius of first circle

    //draw invisible line
    graphics.drawLine(0,250,500,250);

    //draw first circle
    graphics.drawOval(200,200,n*2,n*2);

    //run fractal algorith to draw 2 circles to the left and right
    drawCircles(graphics, n);
}

public void drawCircles (Graphics graphics, int n)
{
    int x1; int y1; //top left corner of left circle to be drawn
    int x2; int y2; //top left corner of right circle to be drawn

    //drawing left circle
    x1=200-((n/2)*2); 
    //***this math was found using the equation in chapter 11
    //***center point of circle = (x+(width/2), y+(height/2))
    y1=200-((n/2)*2);
    graphics.drawOval(x1, y1, ((n/2)*2), ((n/2)*2));

    //drawing right circle
    x2=300-((n/2)*2);
    y2=300-((n/2)*2);
    graphics.drawOval(x1, y1, ((n/2)*2), ((n/2)*2));
}

Any help would be greatly appreciated guys.

1

1 Answers

3
votes

I'm going to throw in a little tip. If the task would be easier and you would rather draw circles from the center, lets make a function that works the way you want it to.

public void circle(Graphics g, int centerX, int centerY, int radius) {
    g.drawOval(centerX-radius, centerY-radius, radius*2+1, radius*2+1) 
}

Now you have a function that can make circles from a center point of your choosing with a radius of your choosing.

Yay!