0

I wanted to start creating fractals, which I think are fascinating. But I cant get the following code to work.

void drawcircle(int x, int y, int radius) {
    g.drawOval(x,y, radius,radius);   //g is to call the graphic functions
    if(radius>2)
    {
       radius=radius/2;
       drawcircle(x, y, radius);
    }    
}

Its supposed to give concentric circles, but theres just not output at all. Even a wrong output would work, as it'd allow me to fix it, but just nothing comes up in the frame. In the output window, it shows a huge block of red lines, but I dont understand what the error is. Can someone point me to the error? It'd be much appreciated. Edit : The code is used as follows. The code dosen't actually work for these values on my system, but it dosen't work for any other values either.

 int x=0; int y=0;
    drawcircle(x,y,256);

This is the error I'm getting :

Error in the output window

Edit : Thanks to all the comments, I figured out the issue with the code. The correct code is

void conccircle(int x,int y, int radius,Graphics G) {
    G.drawOval(x-radius,y-radius,radius*2,radius*2);
    if(radius>=2)
    {
        radius=radius/2;
        conccircle(x,y,radius,G);
    }
}
Vinayak
  • 1
  • 5
  • What initial values are you calling the `drawCircle` function with? Please add some code to show how you are using it. – Matt Hogan-Jones Jan 17 '18 at 16:08
  • Also, be aware that the 3rd and 4th parameters are NOT the radius but are instead the width and height of a rectangle that will bound the oval. If you wanted a circle of radius `10`, then the 3rd and 4th parameter would have a value of `20`. – Matt Hogan-Jones Jan 17 '18 at 16:11
  • Because of this, your code will **not** draw concentric circles - the centre of the circle will changed with each call. The upper left corner of the bounding rectangle for each circle will be the same. – Matt Hogan-Jones Jan 17 '18 at 16:14
  • @Matt thanks for the advice. And the concentric circles thing, noted. But the problem is that this code yields nothing at all; even a weird collection of circles in the top left corner would be fine, as long the code is functioning. The semantic errors, as you pointed out, I'd try to fix later. – Vinayak Jan 17 '18 at 16:42
  • 1
    The red lines are a stack trace of a NullPointerException, raised after a mouse event. Where does the `g` come from? Also take a look here: https://stackoverflow.com/questions/6118737/how-to-draw-in-jpanel-swing-graphics-java – Matteo T. Jan 17 '18 at 18:05
  • Thanks MattJones @Matteo for the advice. I've corrected the code. the problem turned out to be that the function was missing, as Matteo pointed out, an input "Graphics g" in the method declaring part. I was then able to fix the concentric circles thing once I knew what was happening. Thank you !! – Vinayak Jan 18 '18 at 13:08
  • Why not add that as an answer, and then accept it? Self answered questions are allowable. – Matt Hogan-Jones Jan 18 '18 at 15:37

1 Answers1

0

I got it, thanks to everyone who commented!

void conccircle(int x,int y, int radius,Graphics G) {
G.drawOval(x-radius,y-radius,radius*2,radius*2);
if(radius>=2)
{
    radius=radius/2;
    conccircle(x,y,radius,G);
}}
Vinayak
  • 1
  • 5