HELP! Koch Curve Not Working

hi guys

what im doing is learning java 2d graphics in my spare time to increase my knowledge.

so far im getting on good, but this tutorial i got from a java website is killing me.

my knowledge is small and im currently trying to draew a koch curve.

im using NetBeans 3.6 with a JDK 1.5.

i have two classes:

1) MyDrawingPanel
2) MyKoch

here is my code inside MyDrawingPanel for calling MyKoch:

this code is inside a button method. so when i run the program, i can click the button "draw koch" and my koch curve draws

        ...
   
        private MyKoch myKoch = new MyKoch();          // A object of class MyKoch
   
        MyKoch myKoch = new MyKoch();
        AffineTransform saveAT = g2.getTransform(); //get current transform
        double distance = 100.0;
        int xStart1 = (int) distance;
        int yStart1 = 3 * (int) distance;
        int nSides = 8;
        float rotDeg = 0.0f;
        int level = 4;
        float DegToRad = (float) Math.PI / 180.0f;
        g2.translate(xStart1, yStart1);                  // move to start
        g2.rotate(rotDeg * DegToRad);       // initial rotation in radians
        g2.setPaint(Color.red);
        for(int sides = 1; sides <= nSides; sides++)
        {
            myKoch.drawKoch(level, distance, g2);
            g2.translate(distance, 0);              // move to start
            g2.rotate(-360.0f / nSides * DegToRad); // initial rot in radians
        }
        g2.setTransform(saveAT);             // restore original transform
   
        ...

here is my koch class:

import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.lang.Math;

public class MyKoch {

    private MyKoch myKoch;
   
    /** Creates a new instance of Koch */
    public MyKoch() {
    }
   
    public void drawKoch(int xStart, int yStart, float rotDeg, int level, double distance, java.awt.Graphics2D g2){    
             


       MyKoch(level, distance, g2);     <<<<<< this is where it says i have a error in my code???
    }
}


the error is:

SetDrawingPanel.java [128:1] drawKoch(int,int,float,int,double,java.awt.Graphics2D) in MyKoch cannot be applied to (int,double,java.awt.Graphics2D)
            myKoch.drawKoch(level, distance, g2);
                  ^
MyKoch.java [27:1] cannot find symbol
symbol  : method MyKoch(int,double,java.awt.Graphics2D)
location: class MyKoch
       MyKoch(level, distance, g2);             // Draw the curve
       ^
2 errors
Errors compiling




now the thing is, the tutorial is learn people how to draw thisd koch curve does not give any code for this part in the class??  

ya i know, this is some tutorial to give people....
 
can java expert, guru or master or whatever knowledge you have got in java help me draw this koch curve??

if your still not clear, ask, im online till i get this finished...

thanks      
LVL 16
ellandrdAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
>>MyKoch(level, distance, g2);     <<<<<< this is where it says i have a error in my code???

Get rid of that altogether

>>myKoch.drawKoch(level, distance, g2);

needs to be passed the right number of parameters as given in:


>>drawKoch(int xStart, int yStart, float rotDeg, int level, double distance, java.awt.Graphics2D g2)
0
ellandrdAuthor Commented:
right ok ill delete that line of code and get bk to you...
0
ellandrdAuthor Commented:
right ok, little confused?

in the koch class, you want me to remove

MyKoch(level, distance, g2);

so what about

myKoch.drawKoch(level, distance, g2);

that needs to be passed the right number of parameters?

can you explain?
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

CEHJCommented:
>>
in the koch class, you want me to remove

MyKoch(level, distance, g2);
>>


From the drawKoch method, yes


>>
that needs to be passed the right number of parameters?

can you explain?
>>

drawKoch is defined to take 6 parameters, you only pass it 3
0
ellandrdAuthor Commented:
ok so where do i get the other parameters??

i think this tutorial im reading here is crap...
0
ellandrdAuthor Commented:
ok so in my SetDrawingPanel class i have:

MyKoch myKoch = new MyKoch();
        AffineTransform saveAT = g2.getTransform(); //get current transform
        double distance = 100.0;
        int xStart1 = (int) distance;
        int yStart1 = 3 * (int) distance;
        int nSides = 8;
        float rotDeg = 0.0f;
        int level = 4;
        float DegToRad = (float) Math.PI / 180.0f;
        g2.translate(xStart1, yStart1);                  // move to start
        g2.rotate(rotDeg * DegToRad);       // initial rotation in radians
        g2.setPaint(Color.red);
        for(int sides = 1; sides <= nSides; sides++)
        {
            myKoch.drawKoch(xStart, yStart, rotDeg, level, distance, g2);
     
            // int xStart, int yStart, float rotDeg, int level, double distance, java.awt.Graphics2D g2
            g2.translate(distance, 0);              // move to start
            g2.rotate(-360.0f / nSides * DegToRad); // initial rot in radians
        }
        g2.setTransform(saveAT);      

as you can see i have now added parameters to myKoch.drawKoch.....

but still its not drawing the koch cos the method in koch class is empty...

0
ellandrdAuthor Commented:
in this tutorial, its says futher down the page, translate this procedure in java to draw the koch:

Procedure Koch (Level : Integer;  Distance : Real);
begin
        if (Level = 0) then
            DrawLine(Distance)              // Move forward appropriately
        else begin
            Koch(Level - 1, Distance / 3.0);
            Turn(60);
            Koch(Level - 1, Distance / 3.0);
            Turn(-120);
            Koch(Level - 1, Distance / 3.0);
            Turn(60);
            Koch(Level - 1, Distance / 3.0);
        end;
end;

im taking it, ive to recode this in java inside the drawKoch method?
0
ellandrdAuthor Commented:
ok here is my koch class now after translating the above code in java;

import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.lang.Math;

public class MyKoch {

    private MyKoch myKoch;
    private float DegToRad = (float) Math.PI / 180.0f;
   
    public MyKoch(){
    }
   
    public void drawKoch(int xStart, int yStart, float rotDeg, int level, double distance, java.awt.Graphics2D g2){    
        AffineTransform saveAT = g2.getTransform();     // save current
        g2.translate((double) distance, (double) 0);      // move to start
        g2.rotate(rotDeg * DegToRad);
        MyKoch(level, distance, g2);                     // draw curve
        g2.setTransform(saveAT);
    }
   
    public MyKoch(int level, double distance, java.awt.Graphics2D g2)
    {    
        if (level == 0)
        {
            g2.drawLine(0, 0, (int) Math.max(distance, 1.0), 0);  // draw lines
            g2.translate( (double) distance, (double) 0);   // move to start
        } else {
            MyKoch(level -1, distance / 3.0, g2);    // draw curve    
            g2.rotate(60 * DegToRad);               // initial rotation, radians
            MyKoch(level -1, distance / 3.0, g2);    // draw curve
            g2.rotate(-120 * DegToRad);             // initial rotation, radians
            MyKoch(level -1, distance / 3.0, g2);    // draw curve
            g2.rotate(60 * DegToRad);               // initial rotation, radians
            MyKoch(level -1, distance / 3.0, g2);    // draw curve
        }
    }
}

but it still returns errors??

0
ellandrdAuthor Commented:
ok are you still helping me or not?
0
CEHJCommented:
Yes

This:

>>public MyKoch(int level, double distance, java.awt.Graphics2D g2)

etc. is not right. That's a constructor, you need a method. You can call it 'koch' if you like
0
ellandrdAuthor Commented:
will here is what ive got:

import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.lang.Math;

public class MyKoch {

    private float DegToRad = (float) Math.PI / 180.0f;

    /** Creates a new instance of Koch */
    public MyKoch(){
    }
 
    public void drawKoch(int xStart, int yStart, float rotDeg, int level, double distance, java.awt.Graphics2D g2)
    {    
        AffineTransform saveAT = g2.getTransform();     // save current
        g2.translate(xStart, yStart);            // move to start
        g2.rotate(rotDeg * DegToRad);            // initial rotation, radians
        Koch(level, distance, g2);        // draw curve
        g2.setTransform(saveAT);
    }
   
    public Koch(int level, double distance, java.awt.Graphics2D g2)
    {    
        if (level == 0)
        {
            g2.drawLine(0, 0, (int) Math.max(distance, 1.0), 0);  // draw lines
            g2.translate( (double) distance, (double) 0);      // move to start
        }
        else
        {
            Koch(level -1, distance / 3.0, g2);    // draw curve    
            g2.rotate(60 * DegToRad);               // initial rotation, radians
            Koch(level -1, distance / 3.0, g2);    // draw curve
            g2.rotate(-120 * DegToRad);             // initial rotation, radians
            Koch(level -1, distance / 3.0, g2);    // draw curve
            g2.rotate(60 * DegToRad);               // initial rotation, radians
            Koch(level -1, distance / 3.0, g2);    // draw curve
        } // endif)
    }
     
}

is this right?
0
ellandrdAuthor Commented:
have 1 error:

MyKoch.java [33:1] invalid method declaration; return type required
public Koch(int level, double distance, java.awt.Graphics2D g2)

whats wrong?
0
CEHJCommented:
>>return type required

Means what it says. You have not declared a return type. In this case it's

public void koch(...................
0
ellandrdAuthor Commented:
ok fixed the error, was missing a void, should be:

public void Koch(int level, double distance, java.awt.Graphics2D g2)
    {    
        if (level == 0)
        {
            g2.drawLine(0, 0, (int) Math.max(distance, 1.0), 0);  // draw lines
            g2.translate( (double) distance, (double) 0);     // move to start
        }
        else
        {
            Koch(level -1, distance / 3.0, g2);    // draw curve    
            g2.rotate(60 * DegToRad);               // initial rotation, radians
            Koch(level -1, distance / 3.0, g2);    // draw curve
            g2.rotate(-120 * DegToRad);             // initial rotation, radians
            Koch(level -1, distance / 3.0, g2);    // draw curve
            g2.rotate(60 * DegToRad);               // initial rotation, radians
            Koch(level -1, distance / 3.0, g2);    // draw curve
        } // endif)
    }

ok i ran it then, but nothing draws?

0
CEHJCommented:
>>ok i ran it then, but nothing draws?

What did you pass it as 'g2'?
0
ellandrdAuthor Commented:
here is my code that is in a button method to draw the koch curve...

MyKoch myKoch = new MyKoch();
        AffineTransform saveAT = g2.getTransform(); //get current transform
        double distance = 100.0;
        int xStart1 = (int) distance;
        int yStart1 = 3 * (int) distance;
        int nSides = 8;
        float rotDeg = 0.0f;
        int level = 4;
        float DegToRad = (float) Math.PI / 180.0f;
        g2.translate(xStart1, yStart1);                  // move to start
        g2.rotate(rotDeg * DegToRad);       // initial rotation in radians
        g2.setPaint(Color.red);
        for(int sides = 1; sides <= nSides; sides++)
        {
            myKoch.drawKoch(100, 100, rotDeg, level, distance, g2);
            g2.translate(distance, 0);              // move to start
            g2.rotate(-360.0f / nSides * DegToRad); // initial rot in radians
        }
        g2.setTransform(saveAT);  

can you check if this is working and please check if above code in previous post is correct?

0
objectsCommented:
try this:


import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.lang.Math;

public class MyKoch {

    private float DegToRad = (float) Math.PI / 180.0f;

    /** Creates a new instance of Koch */
    public MyKoch(){
    }
 
    public void drawKoch(int xStart, int yStart, float rotDeg, int level, double distance, java.awt.Graphics2D g2)
    {    
        AffineTransform saveAT = g2.getTransform();     // save current
        g2.translate(xStart, yStart);            // move to start
        g2.rotate(rotDeg * DegToRad);            // initial rotation, radians
        Koch(level, distance, g2);        // draw curve
        g2.setTransform(saveAT);
    }
   
    public void koch(int level, double distance, java.awt.Graphics2D g2)
    {    
        if (level == 0)
        {
            g2.drawLine(0, 0, (int) Math.max(distance, 1.0), 0);  // draw lines
            g2.translate( (double) distance, (double) 0);     // move to start
        }
        else
        {
            koch(level -1, distance / 3.0, g2);    // draw curve    
            g2.rotate(60 * DegToRad);               // initial rotation, radians
            koch(level -1, distance / 3.0, g2);    // draw curve
            g2.rotate(-120 * DegToRad);             // initial rotation, radians
            koch(level -1, distance / 3.0, g2);    // draw curve
            g2.rotate(60 * DegToRad);               // initial rotation, radians
            koch(level -1, distance / 3.0, g2);    // draw curve
        } // endif)
    }
     
}
0
objectsCommented:
> here is my code that is in a button method to draw the koch curve...

probably shouldn't be calling from button code, where are you getting your graphic context?
0
CEHJCommented:
You'll need to pass a valid Graphics reference from a component, cast to Graphics2D if you want it to draw
0
ellandrdAuthor Commented:
? confused, g2 is the graphics2D
0
CEHJCommented:
If you don't want the drawing to erase itself outside your control, that routine will have to be called from the paintComponent method of a component if you're using Swing, or paint if using AWT
0
ashok3sepCommented:
Hi,
This is the method you are calling for drawing from the DrAWING PANEL CLASS
 public void drawKoch(int xStart, int yStart, float rotDeg, int level, double distance, java.awt.Graphics2D g2)

but you pass only 3 arguments

you need to pss 6 arguments for calling this function.

Hope this helps.

so when you press a button then the action to be performed should be carried to the KOCH CLASS

regards
Freedom.
0
ellandrdAuthor Commented:
OK, this is my whole code inside the method to draw the koch on the drawing panel...

paintComponent(this.getGraphics());        
       
        AffineTransform saveAT = g2.getTransform(); //get current transform
        double distance = 100.0;
        int xStart1 = (int) distance;
        int yStart1 = 3 * (int) distance;
        int nSides = 8;
        float rotDeg = 0.0f;
        int level = 4;
        float DegToRad = (float) Math.PI / 180.0f;
        g2.translate(xStart1, yStart1);                  // move to start
        g2.rotate(rotDeg * DegToRad);       // initial rotation in radians
        g2.setPaint(Color.red);
        for(int sides = 1; sides <= nSides; sides++)
        {
            myKoch.drawKoch(xStart, yStart, rotDeg, level, distance, g2);
            g2.translate(distance, 0);              // move to start
            g2.rotate(-360.0f / nSides * DegToRad); // initial rot in radians
        }
        g2.setTransform(saveAT);             // restore original transform

OK objects, thanks for joining

i have a drawing panel that i can draw shapes on etc.. i have a number of buttons that draw different shapes etc etc.

on 1 of my buttons im trying to draw a koch curve...

above is all the code inside the method...

if both of you are still confused, you can download the .java, .class, .form so you can take a look at it.

if you wnat that, ill upload it to my site for downlaod...

0
objectsCommented:
> paintComponent(this.getGraphics());        

you shouldn't do that, you need to do the panels painting from its paintComponent() method.
0
ellandrdAuthor Commented:
do you want all setDrawingPanel.java and MyKoch.java files to look at?
0
objectsCommented:
I posted above the changes I could see that needed to be made to MyKoch
0
CEHJCommented:
It should look something like:

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Koch(level, distance, (Graphics2D) g);
}
0
CEHJCommented:
Or rather

koch.Koch(level, distance, (Graphics2D) g);
0
objectsCommented:
your paint would look like:

public void paintComponent(Graphics g)
{
     koch.koch(level, distance, (Graphics2D) g);
}
0
ellandrdAuthor Commented:
ok will try this..

but here are the .java's for you to look at if you wnat...

http://www.freewebs.com/seandelaney/downloads/classess.zip
0
ellandrdAuthor Commented:
it didnt work, but if i draw a line first, then the koch only draw in pieces...
0
objectsCommented:
you need to move the painting of your curve into the paintComponent() and use the graphic context of your image.
0
ellandrdAuthor Commented:
but i want to draw the curve after i clear the panel i draw on... so in order to do that do i not call it inside the buttons method??

did you look at my classess?
0
CEHJCommented:
You need to actually override paintComponent in a component. See:

http://leepoint.net/notes-java/30GUI/20graphics/40drawingpanel/10drawingpanel.html
0
ellandrdAuthor Commented:
so do you see what im trying to do? - after i clear the panel, draw the curve...
0
objectsCommented:
> but i want to draw the curve after i clear the panel i draw on... so in order to do that
> do i not call it inside the buttons method??

you cannot just paint it once, as it will get deleted when the panel is repainted.
the paintComponent() method is called ewhenever the panel is repainted.

> did you look at my classess?

yes

CEHJ> You need to actually override paintComponent in a component

Already is being overridden.
0
objectsCommented:
> so do you see what im trying to do? - after i clear the panel, draw the curve...

Yes but you need to do *all* your painting from paintComponent(), that is how the swing painting model works
Your paintComponent() method should paint the panel exactly as it should appear at a point in time.
0
ellandrdAuthor Commented:
ok, maybe im wrong, but my understanding off this, is, i draw a few lines, then i decide to clear the panel. when i click clear, the panel clear and the koch curve gets drawn. then i draw more lines on panel. this drawing process gets repeated everytime i clear the panel...

0
CEHJCommented:
As i mentioned earlier, unless you just want to draw it momentarily:

>>
If you don't want the drawing to erase itself outside your control, that routine will have to be called from the paintComponent method of a component if you're using Swing, or paint if using AWT
>>
0
CEHJCommented:
>>when i click clear,

Best to flag that in paintComponent:

if (clear) {
    super.paintComponent(g);
}
else {
    // koch
}
0
ellandrdAuthor Commented:
can you show me what needs changed by posting the code here cos its like 2.40 in the morning here and im wreaked tired and this isnt working for me
0
ellandrdAuthor Commented:
if you look in my code in the classess in allowed for download, you'll see that i have flag set up but dont understand this cos im using a tutorial and it dont explain this flag thing.... but anyways i have this set...
0
objectsCommented:
change your clear() method to clear the offscreen image, and then paint the koch to the offscreen image.
Then when shaps are added paint them also directly to offscreen image.
Then in paintComponent() all you need to do ois paint the offscreen image to screen.
0
ellandrdAuthor Commented:
ok, for forgetting im newbie a java, how do i clear offscreen image part
0
ellandrdAuthor Commented:
heres my code:

public void setMyClear(boolean b) {
               
        myClearFlag = b;

        MyKoch myKoch = new MyKoch();
        AffineTransform saveAT = g2.getTransform(); //get current transform
        double distance = 100.0;
        int xStart1 = (int) distance;
        int yStart1 = 3 * (int) distance;
        int nSides = 8;
        float rotDeg = 0.0f;
        int level = 4;
        float DegToRad = (float) Math.PI / 180.0f;
        g2.translate(xStart1, yStart1);                  // move to start
        g2.rotate(rotDeg * DegToRad);       // initial rotation in radians
        g2.setPaint(Color.red);
        for(int sides = 1; sides <= nSides; sides++)
        {
            myKoch.koch(level, distance, g2);
            g2.translate(distance+10, 0);              // move to start
            g2.rotate(-360.0f / nSides * DegToRad); // initial rot in radians
        }
        g2.setTransform(saveAT);             // restore original transform

        paintComponent(this.getGraphics());
    }
0
ellandrdAuthor Commented:
ok i dont know what i done, but if i claer the screen nothing happens, but then if i draw a line, "pieces" of the koch gets drawing! something or somehwere ive gone wrong...
0
ellandrdAuthor Commented:
what you think?
0
objectsCommented:
> how do i clear offscreen image part

fill the image with background colour

g.setColor(bg);
g.fillRect(0, 0, width, height);
0
ellandrdAuthor Commented:
no koch is drawing now!
0
ellandrdAuthor Commented:
here was my code:

public void setMyClear(boolean b) {
               
        myClearFlag = b;
        g2.setColor(Color.white);
        g2.fillRect(0, 0, d.width, d.height);
           
       
       
        MyKoch myKoch = new MyKoch();
        AffineTransform saveAT = g2.getTransform(); //get current transform
        double distance = 100.0;
        int xStart1 = (int) distance;
        int yStart1 = 3 * (int) distance;
        int nSides = 8;
        float rotDeg = 0.0f;
        int level = 4;
        float DegToRad = (float) Math.PI / 180.0f;
        g2.translate(xStart1, yStart1);                  // move to start
        g2.rotate(rotDeg * DegToRad);       // initial rotation in radians
        g2.setPaint(Color.red);
        for(int sides = 1; sides <= nSides; sides++)
        {
            myKoch.koch(level, distance, g2);
            g2.translate(distance, 0);              // move to start
            g2.rotate(-360.0f / nSides * DegToRad); // initial rot in radians
        }
        g2.setTransform(saveAT);             // restore original transform

        paintComponent(this.getGraphics());
    }
0
objectsCommented:
>      paintComponent(this.getGraphics());

you should *never* do that

that should be replace with:

repaint();
0
ellandrdAuthor Commented:
ok, im just going by tutorial...
0
ellandrdAuthor Commented:
it is still not working? dont understand, made all the changes, copied your koch class... just dont know...

need quick fix - help!
0
ellandrdAuthor Commented:
can you see anywhere in my code to why it isnt working?
0
objectsCommented:
The paint code you are using is not an approach I would follow, so I'd be rewriting that entire class using one of two options being the one I suggested above, or
in paintComponent() you paint the koch, the shapes.

public void paintComponent()
{
   // paint background

   // paint koch

   // paint all shapes
}
0
ellandrdAuthor Commented:
ok im off to bed at half past cos i cant keep my eyes open...

if its not fixed im just going to award you the points for helping and scrap this cos its a p*in in the a**!
0
ellandrdAuthor Commented:
but what im trying to do isnt impossible is it? - drawing the curve after i clear the screen
0
ellandrdAuthor Commented:
all im doing is following this tutorial i found on a java site to learn 2d graphics... everything is been fine up to this curve...
0
objectsCommented:
which tutorial exactly?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ellandrdAuthor Commented:
i just printed it off, have not got a hard copy for you to see unless i post it up here... i got it on google...
0
ellandrdAuthor Commented:
if you know off better tut's, you know what to do - post URL... and ill start again
0
ellandrdAuthor Commented:
right thats me!

its over, im giving up learning this. im off to bed! thanks for all the help!

0
objectsCommented:
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.