Link to home
Start Free TrialLog in
Avatar of ellandrd
ellandrdFlag for Ireland

asked on

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      
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

>>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)
Avatar of ellandrd

ASKER

right ok ill delete that line of code and get bk to you...
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?
>>
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
ok so where do i get the other parameters??

i think this tutorial im reading here is crap...
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...

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?
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??

ok are you still helping me or not?
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
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?
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?
>>return type required

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

public void koch(...................
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?

>>ok i ran it then, but nothing draws?

What did you pass it as 'g2'?
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?

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)
    }
     
}
> 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?
You'll need to pass a valid Graphics reference from a component, cast to Graphics2D if you want it to draw
? confused, g2 is the graphics2D
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
Avatar of ashok3sep
ashok3sep

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.
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...

> paintComponent(this.getGraphics());        

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

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

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

public void paintComponent(Graphics g)
{
     koch.koch(level, distance, (Graphics2D) g);
}
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
it didnt work, but if i draw a line first, then the koch only draw in pieces...
you need to move the painting of your curve into the paintComponent() and use the graphic context of your image.
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?
You need to actually override paintComponent in a component. See:

http://leepoint.net/notes-java/30GUI/20graphics/40drawingpanel/10drawingpanel.html
so do you see what im trying to do? - after i clear the panel, draw the curve...
> 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.
> 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.
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...

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
>>
>>when i click clear,

Best to flag that in paintComponent:

if (clear) {
    super.paintComponent(g);
}
else {
    // koch
}
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
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...
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.
ok, for forgetting im newbie a java, how do i clear offscreen image part
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());
    }
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...
what you think?
> how do i clear offscreen image part

fill the image with background colour

g.setColor(bg);
g.fillRect(0, 0, width, height);
no koch is drawing now!
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());
    }
>      paintComponent(this.getGraphics());

you should *never* do that

that should be replace with:

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

need quick fix - help!
can you see anywhere in my code to why it isnt working?
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
}
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**!
but what im trying to do isnt impossible is it? - drawing the curve after i clear the screen
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...
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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...
if you know off better tut's, you know what to do - post URL... and ill start again
right thats me!

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