Jodo1992 Jodo1992 - 1 month ago 6
Java Question

Processing 3.x not recognizing random() function

I'm trying to create a basic "tree" shape in processing. I have an initial constructor that takes my arguments and draws them in fixed locations on the background, but I also have a secondary constructor that is supposed to assign random values that I specify so that each time it is drawn the trees are in different locations. However, I am having an issue with Processing where it says

The function random(int) does not exist
and I can't seem to find a solution to the issue.

I realize this is a naive approach to graphics, but I am just trying to get my feet wet with Processing.

My Tree.java class:

import processing.core.PApplet;
import java.util.Random;

public class Tree{

// Instance variables
private int centerX, centerY;
private float scale;
private int trunkR, trunkG, trunkB, leavesR, leavesG, leavesB;
private static PApplet sketch;

public Tree(int theCenterX, int theCenterY, float theScale,
int theTrunkR, int theTrunkG, int theTrunkB,
int theLeavesR, int theLeavesG, int theLeavesB)
{
centerX = theCenterX;
centerY = theCenterY;
scale = theScale;
trunkR = theTrunkR;
trunkG = theTrunkG;
trunkB = theTrunkB;
leavesR = theLeavesR;
leavesG = theLeavesG;
leavesB = theLeavesB;
}

public Tree(){
centerX = random(960.0);
centerY = random(700.0);
scale = random(2.0);
trunkR = random(255.0);
trunkG = random(255.0);
trunkB = random(255.0);
leavesR = random(255.0);
leavesG = random(255.0);
leavesB = random(255.0);
}

public void draw(){
sketch.noStroke();
sketch.fill(trunkR, trunkG, trunkB);
sketch.rect(centerX, centerY, 80*scale, 300*scale);

sketch.fill(leavesR, leavesG, leavesB);
sketch.triangle(centerX - 40*scale, centerY + 40*scale, centerX + 40*scale, centerY - 80*scale, centerX + 120*scale, centerY + 40*scale);
}

public static void setup(PApplet theSketch){
sketch = theSketch;
}
}


And here is my main class that calls the tree class to create the objects:

Tree tree, tree2, tree3, tree4, randomTree;

void settings(){
size(1000, 1000);
}

void setup(){
setupGraphicClasses();

tree = new Tree(width/2 - 400, height/2 - 100, 1.0, 67, 12, 12, 27, 129, 28);
tree2 = new Tree(width/2 + 200, height/2 + 150, 1.5, 67, 12, 12, 27, 129, 28);
tree3 = new Tree(width/2, height/2 - 80, 0.5, 67, 12, 12, 27, 129, 28);
tree4 = new Tree(width/2 + 320, height/2 - 170, 0.9, 67, 12, 12, 27, 129, 28);
randomTree = new Tree();
}

void draw() {
background(127);

noStroke();
fill(16, 85, 17);
rect(0, 500, 1000, 500);
fill(70, 195, 255);
rect(0, 0, 1000, 500);
tree.draw();
tree4.draw();
tree2.draw();
tree3.draw();
randomTree.draw();
}

public void setupGraphicClasses() {
Tree.setup(this);
}


Why would I be getting this error? I have tried casting the instance variables as
float
since those are the parameters the
random()
function accepts as parameters, but then I get a different error message.

Answer

You're calling the random() function from your Tree class, not your sketch class. That won't work, because only the sketch class knows about the random() function.

One approach to fix this is to pass an instance of your sketch into the Tree class, then use that to get to the random function. Something like this:

void setup(){
   Tree tree = new Tree(this);
}

class Tree{

   public Tree(PApplet sketch){
      float x = sketch.random(100);
   }
}

If all you need is the random() function, this might be overkill though. You could just use the Math.random() function instead. Of course, this locks you into deploying as Java, which might be overly restrictive.

Comments