Reputation: 477
I've been working on this project for last week and can't figure out how to fix it. I feel like I'm so close but can't spot my error! My assignment calls for me to draw circles along an imaginary line using the Java Graphics class. Draw a circle in the center with a radius of n. Then draw two circles with radius of n/2 whose endpoints intersect with the left and right arc of the circle.
I have been able to draw the 2nd step of two circles to the right and left of the first circle. However, my program is supposed to then draw four circles of the same size recursively. One circle to both the right and left side of the left circle AND one circle to both the right and left side of the right circle. Something is suspect with my code.
Any help would be greatly appreciated.
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 x1,x2,y1,y2; //future x and y coordinates
int radius=125; //radius of first circle
int xMid=250, yMid=250; //center point (x,y) of circle
//draw invisible line
graphics.drawLine(0,250,500,250);
//draw first circle
graphics.drawOval(xMid-radius,yMid-radius,radius*2,radius*2);
//run fractal algorithm to draw 2 circles to the left and right
drawCircles(graphics, xMid, yMid, radius);
}
void drawCircles (Graphics graphics, int xMid, int yMid, int radius)
{
//used to position left and right circles
int x1 = xMid-radius-(radius/2);
int y1 = yMid-(radius/2);
int x2 = xMid+radius-(radius/2);
int y2= yMid-(radius/2);
if (radius > 5)
{
//draw circle to the left
graphics.drawOval(x1, y1, (radius/2)*2, (radius/2)*2);
//draw circle to the right
graphics.drawOval(x2, y2, (radius/2)*2, (radius/2)*2);
}
drawCircles (graphics, xMid, yMid, radius/2);
}
Upvotes: 2
Views: 8317
Reputation: 2694
You are only drawing 1 circle from each side of the big circle. You need to change the code. I only write a short fix but don't tested. Good luck
void drawCircles (Graphics graphics, int xMid, int yMid, int radius)
{
//used to position left and right circles
int x1 = xMid-radius-(radius/2);
int x2 = xMid+radius-(radius/2);
int x3 = xMid+radius+(radius/2);
int x4 = xMid-radius+(radius/2);
int y = yMid-(radius/2);
if (radius > 5)
{
//draw 4 circles
graphics.drawOval(x1, y, (radius/2)*2, (radius/2)*2);
graphics.drawOval(x2, y, (radius/2)*2, (radius/2)*2);
graphics.drawOval(x3, y, (radius/2)*2, (radius/2)*2);
graphics.drawOval(x4, y, (radius/2)*2, (radius/2)*2);
}
//left
drawCircles(graphics, xMid-radius, yMid, radius / 2);
//right
drawCircles(graphics, xMid+radius, yMid, radius / 2);
}
Upvotes: 0
Reputation: 34532
Some adapted version using recursion... Draw the circle, then draw its 2 children by calling the same function again.
void drawCircles(Graphics graphics, int xMid, int yMid, int radius) {
// end recursion
if(radius < 5)
return;
// Draw circle
graphics.drawOval(xMid - radius, yMid - radius, radius * 2, radius * 2);
// start recursion
//left
drawCircles(graphics, xMid-radius, yMid, radius / 2);
//right
drawCircles(graphics, xMid+radius, yMid, radius / 2);
}
Upvotes: 1
Reputation: 5647
It looks to be like you only have one drawCircles
call inside your drawCircles
function. This can't be right because each time you draw a circle you need to call the recursive function twice. I see that you are drawing two circles, but that is actually backwards.
To get this to work, you need to draw one circle, and call the recursive function twice.
Here is what you need to change:
That should do it.
Upvotes: 1