some java help

megadave

I am a FH squatter
Joined
Apr 3, 2006
Messages
11,911
Ok i have some java bluej work i am stuck on, i am complete noob at it but need to get it out of the way so i can do some more important exam revision. anyone able to help?

All i've got left to do is;

*Add a random element to the construction so that the length of each branch and/or the angle between branches
may vary at each iteration.

*Adapt the applet so that it can draw more than one tree on the screen.

Code:
/*
* OneFractalTree.java
*/

import java.awt.*;
import javax.swing.*;
import java.util.Random;

public class OneFractalTree extends JApplet {

    final double ONE_DEGREE = Math.PI/180;              
    final double BRANCHANGLE = ONE_DEGREE*30;           //sets the angle the branches extend at
    final double SHRINKFACTOR = .65;                    //sets the value at which the branches shrinks by 
    final int START_LENGTH = 75;                        //sets the length of the branches

    public void drawbranch( Graphics g,
                            double startx,
                            double starty,
                            double length,
                            double angle )
                            
{
            double endx = startx + Math.sin(angle) * length;
            double endy = starty + Math.cos(angle) * length;
            
            if( 1 < length ) {
                g.setColor( length < 5 ? Color.green : Color.black );
                g.drawLine( (int)startx, (int)starty, (int)endx, (int)endy );
                drawbranch( g, endx, endy, length * SHRINKFACTOR, angle -
BRANCHANGLE );
                drawbranch( g, endx, endy, length * SHRINKFACTOR, angle +
BRANCHANGLE );
                drawbranch( g, endx, endy, length * SHRINKFACTOR, angle
            );
            


    }
}



public void paint( Graphics g ) {
Rectangle r = getBounds();
drawbranch( g, r.width/2, r.height, START_LENGTH, Math.PI );
}
}
Thanks a lot to anyone who can help :|
 

JingleBells

FH is my second home
Joined
Mar 25, 2004
Messages
2,224
Ok i have some java bluej work i am stuck on, i am complete noob at it but need to get it out of the way so i can do some more important exam revision. anyone able to help?

All i've got left to do is;

*Add a random element to the construction so that the length of each branch and/or the angle between branches
may vary at each iteration.

Math.rand() gives you a random number between 0 and 1, use this to generate random angles & lengths

*Adapt the applet so that it can draw more than one tree on the screen.
Thanks a lot to anyone who can help :|

Call
drawbranch( g, r.width/2, r.height, START_LENGTH, Math.PI );

more than once with different start x & start y values to draw 2 ;)
 

Aoami

I am a FH squatter
Joined
Dec 22, 2003
Messages
11,223
see i told you...

i just didnt know how to do it
 

megadave

I am a FH squatter
Joined
Apr 3, 2006
Messages
11,911
thanks, now to figure out how to implement that stuff :p
 

megadave

I am a FH squatter
Joined
Apr 3, 2006
Messages
11,911
think im almost done (hope atleast) thanks to jingles advice and some help from overdriven. just getting an error at the end that i'm unsure how to fix.

Code:
/*
* OneFractalTree.java
*/

import java.awt.*;
import javax.swing.*;


public class OneFractalTree extends JApplet {

double rNum = (Math.random()*200);                      //creates a random number generator up to 200

    final double ONE_DEGREE = Math.PI/180;              
    final double BRANCHANGLE = ONE_DEGREE*30;           //sets the angle the branches extend at
    final double SHRINKFACTOR = .65;                    //sets the value at which the branches shrinks by 
    final double START_LENGTH = rNum;                   //assigns the branch length a random number up to 200




public void drawbranch( Graphics g,                    //assigns the values that the drawbranch command will implement
                            double startx,              
                            double starty,
                            double length,
                            double angle );
                            
    
public void drawbranch2( Graphics h,                   //assigns the values that the second drawbranch command will implement
                            double starta,
                            double startb,
                            double length2,
                            double angle2 );                          
   
{
            double endx = startx + Math.sin(angle) * length;
            double endy = starty + Math.cos(angle) * length;
            
            if( 1 < length ) {
                g.setColor( length < 5 ? Color.green : Color.black );
                g.drawLine( (int)startx, (int)starty, (int)endx, (int)endy );
                drawbranch( g, endx, endy, length * SHRINKFACTOR, angle -
BRANCHANGLE );
                drawbranch( g, endx, endy, length * SHRINKFACTOR, angle +
BRANCHANGLE );
                drawbranch( g, endx, endy, length * SHRINKFACTOR, angle
            );
            


    }
}

{
            double endx = startx + Math.sin(angle) * length;
            double endy = starty + Math.cos(angle) * length;
           
            if( 1 < length ) {
                h.setColor( length < 5 ? Color.green : Color.black );
                h.drawLine( (int)starta, (int)startb, (int)endx, (int)endy );
                drawbranch2( h, endx, endy, length2 * SHRINKFACTOR, angle2 -
BRANCHANGLE );
                drawbranch2( h, endx, endy, length2 * SHRINKFACTOR, angle2 +
BRANCHANGLE );
                drawbranch2( h, endx, endy, length2 * SHRINKFACTOR, angle2
            );
           
 
 
    }
}


public void paint( Graphics g ) {
Rectangle r = getBounds();
drawbranch( g, r.width/2, r.height, START_LENGTH, Math.PI );

public void paint( Graphics h ) {
Rectangle r = getBounds();
drawbranch( g, r.width/2, r.height, START_LENGTH, Math.PI );
}
}

Error is on this line at the end:

public void paint( Graphics h ) {

"Illegal start of expression"

any fixers? :|
 

kirennia

Part of the furniture
Joined
Dec 26, 2003
Messages
3,857
1)Creating two methods with the same name and same attributes passed into them cannot be done therefore in other instances, the second method would need to be called somethingelse or have different variables passed into it.

2)Placing a method inside another method will not work in this case...in fact, I'm not sure where it'll work but your '{}' tags don't line up for the appropriate methods. Always start with a { and end with a } before starting a new method.

3)The paint method itself is a system method so cannot be replicated by creating more then one. If you're trying to create more then one of the same object within the program, simply write the 'drawbranch' method to be accessed twice and pass different parameters into it.

drawbranch( g, r.width/2, r.height, START_LENGTH, Math.PI );

I'm presuming the 2nd and third values are for the x/y coordinatesfor the tree to be drawn. By calling this a second time with the same value, you'd end up with them drawn on top of each other so you'll need to mix them up. For example, in the paint method, try something like:

public void paint( Graphics g ) {
Rectangle r = getBounds();
drawbranch( g, 100, r.height, START_LENGTH, Math.PI );
drawbranch( g, 200, r.height, START_LENGTH, Math.PI );
}

Be sure to understand how the method is working however before moving on. I made a complete pigs ear of one of my projects after not completely understanding the paint method and bodge jobbing it together.

e.g. Painting in AWT and Swing

:)
 

Users who are viewing this thread

Top Bottom