Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
Solved

# HELP! Koch Curve Not Working

Posted on 2005-02-27
Medium Priority
377 Views
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.setPaint(Color.red);
for(int sides = 1; sides <= nSides; sides++)
{
myKoch.drawKoch(level, distance, g2);
g2.translate(distance, 0);              // move to start
}
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
0
Question by:ellandrd
• 36
• 14
• 12
• +1

LVL 86

Expert Comment

ID: 13415664
>>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

LVL 16

Author Comment

ID: 13415673
right ok ill delete that line of code and get bk to you...
0

LVL 16

Author Comment

ID: 13415678
right ok, little confused?

in the koch class, you want me to remove

MyKoch(level, distance, g2);

myKoch.drawKoch(level, distance, g2);

that needs to be passed the right number of parameters?

can you explain?
0

LVL 86

Expert Comment

ID: 13415691
>>
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

LVL 16

Author Comment

ID: 13415702
ok so where do i get the other parameters??

i think this tutorial im reading here is crap...
0

LVL 16

Author Comment

ID: 13415730
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.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.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

LVL 16

Author Comment

ID: 13415747
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

LVL 16

Author Comment

ID: 13415808
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
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
MyKoch(level -1, distance / 3.0, g2);    // draw curve
MyKoch(level -1, distance / 3.0, g2);    // draw curve
MyKoch(level -1, distance / 3.0, g2);    // draw curve
}
}
}

but it still returns errors??

0

LVL 16

Author Comment

ID: 13415852
ok are you still helping me or not?
0

LVL 86

Expert Comment

ID: 13415870
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

LVL 16

Author Comment

ID: 13415896
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
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
Koch(level -1, distance / 3.0, g2);    // draw curve
Koch(level -1, distance / 3.0, g2);    // draw curve
Koch(level -1, distance / 3.0, g2);    // draw curve
} // endif)
}

}

is this right?
0

LVL 16

Author Comment

ID: 13415904
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

LVL 86

Expert Comment

ID: 13415918
>>return type required

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

public void koch(...................
0

LVL 16

Author Comment

ID: 13415921
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
Koch(level -1, distance / 3.0, g2);    // draw curve
Koch(level -1, distance / 3.0, g2);    // draw curve
Koch(level -1, distance / 3.0, g2);    // draw curve
} // endif)
}

ok i ran it then, but nothing draws?

0

LVL 86

Expert Comment

ID: 13415929
>>ok i ran it then, but nothing draws?

What did you pass it as 'g2'?
0

LVL 16

Author Comment

ID: 13415936
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.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.setTransform(saveAT);

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

0

LVL 92

Expert Comment

ID: 13415939
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
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
koch(level -1, distance / 3.0, g2);    // draw curve
koch(level -1, distance / 3.0, g2);    // draw curve
koch(level -1, distance / 3.0, g2);    // draw curve
} // endif)
}

}
0

LVL 92

Expert Comment

ID: 13415945
> 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

LVL 86

Expert Comment

ID: 13415947
You'll need to pass a valid Graphics reference from a component, cast to Graphics2D if you want it to draw
0

LVL 16

Author Comment

ID: 13415948
? confused, g2 is the graphics2D
0

LVL 86

Expert Comment

ID: 13415965
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

LVL 5

Expert Comment

ID: 13415968
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

LVL 16

Author Comment

ID: 13415969
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.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.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

LVL 92

Expert Comment

ID: 13415985
> paintComponent(this.getGraphics());

you shouldn't do that, you need to do the panels painting from its paintComponent() method.
0

LVL 16

Author Comment

ID: 13415997
do you want all setDrawingPanel.java and MyKoch.java files to look at?
0

LVL 92

Expert Comment

ID: 13416002
I posted above the changes I could see that needed to be made to MyKoch
0

LVL 86

Expert Comment

ID: 13416004
It should look something like:

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

LVL 86

Expert Comment

ID: 13416011
Or rather

koch.Koch(level, distance, (Graphics2D) g);
0

LVL 92

Expert Comment

ID: 13416020

public void paintComponent(Graphics g)
{
koch.koch(level, distance, (Graphics2D) g);
}
0

LVL 16

Author Comment

ID: 13416033
ok will try this..

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

0

LVL 16

Author Comment

ID: 13416050
it didnt work, but if i draw a line first, then the koch only draw in pieces...
0

LVL 92

Expert Comment

ID: 13416059
you need to move the painting of your curve into the paintComponent() and use the graphic context of your image.
0

LVL 16

Author Comment

ID: 13416069
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

LVL 86

Expert Comment

ID: 13416071
You need to actually override paintComponent in a component. See:

http://leepoint.net/notes-java/30GUI/20graphics/40drawingpanel/10drawingpanel.html
0

LVL 16

Author Comment

ID: 13416090
so do you see what im trying to do? - after i clear the panel, draw the curve...
0

LVL 92

Expert Comment

ID: 13416098
> 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

0

LVL 92

Expert Comment

ID: 13416106
> 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

LVL 16

Author Comment

ID: 13416124
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

LVL 86

Expert Comment

ID: 13416129
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

LVL 86

Expert Comment

ID: 13416131
>>when i click clear,

Best to flag that in paintComponent:

if (clear) {
super.paintComponent(g);
}
else {
// koch
}
0

LVL 16

Author Comment

ID: 13416135
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

LVL 16

Author Comment

ID: 13416146
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

LVL 92

Expert Comment

ID: 13416158
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

LVL 16

Author Comment

ID: 13416171
ok, for forgetting im newbie a java, how do i clear offscreen image part
0

LVL 16

Author Comment

ID: 13416174
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.setPaint(Color.red);
for(int sides = 1; sides <= nSides; sides++)
{
myKoch.koch(level, distance, g2);
g2.translate(distance+10, 0);              // move to start
}
g2.setTransform(saveAT);             // restore original transform

paintComponent(this.getGraphics());
}
0

LVL 16

Author Comment

ID: 13416186
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

LVL 16

Author Comment

ID: 13416200
what you think?
0

LVL 92

Expert Comment

ID: 13416214
> how do i clear offscreen image part

fill the image with background colour

g.setColor(bg);
g.fillRect(0, 0, width, height);
0

LVL 16

Author Comment

ID: 13416226
no koch is drawing now!
0

LVL 16

Author Comment

ID: 13416233
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.setPaint(Color.red);
for(int sides = 1; sides <= nSides; sides++)
{
myKoch.koch(level, distance, g2);
g2.translate(distance, 0);              // move to start
}
g2.setTransform(saveAT);             // restore original transform

paintComponent(this.getGraphics());
}
0

LVL 92

Expert Comment

ID: 13416246
>      paintComponent(this.getGraphics());

you should *never* do that

that should be replace with:

repaint();
0

LVL 16

Author Comment

ID: 13416248
ok, im just going by tutorial...
0

LVL 16

Author Comment

ID: 13416259
it is still not working? dont understand, made all the changes, copied your koch class... just dont know...

need quick fix - help!
0

LVL 16

Author Comment

ID: 13416287
can you see anywhere in my code to why it isnt working?
0

LVL 92

Expert Comment

ID: 13416300
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

LVL 16

Author Comment

ID: 13416302
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

LVL 16

Author Comment

ID: 13416312
but what im trying to do isnt impossible is it? - drawing the curve after i clear the screen
0

LVL 16

Author Comment

ID: 13416317
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

LVL 92

Accepted Solution

objects earned 2000 total points
ID: 13416320
which tutorial exactly?
0

LVL 16

Author Comment

ID: 13416334
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

LVL 16

Author Comment

ID: 13416340
if you know off better tut's, you know what to do - post URL... and ill start again
0

LVL 16

Author Comment

ID: 13416347
right thats me!

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

0

LVL 92

Expert Comment

ID: 13416365
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilationâ€¦
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different dâ€¦
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Booleâ€¦
The viewer will learn how to implement Singleton Design Pattern in Java.
###### Suggested Courses
Course of the Month15 days, 9 hours left to enroll