We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Converting main sketch code into Java mode
Page Index Toggle Pages: 1
Converting main sketch code into Java mode (Read 806 times)
Converting main sketch code into Java mode
Dec 15th, 2009, 1:14pm
 
Hello, I'm on pages 19-20 of Chapter 14 of the Ira Greenberg book, and I am in a world of pain. It says that to convert this Processing code:

Quote:
int birdCount = 300;
Bird[] birds = new Bird[birdCount];
float[] x = new float[birdCount];
float[] y = new float[birdCount];
float[] z = new float[birdCount];
float[] rx = new float[birdCount];
float[] ry = new float[birdCount];
float[] rz = new float[birdCount];
float[] spd = new float[birdCount];
float[] rot = new float[birdCount];

void setup(){
  size(400, 400, P3D);
  noStroke();

  for (int i =0; i < birdCount; i++) {

    birds[i] = new Bird(random(-300, 300), random(-300, 300),
    random(-500, -2500), random(5, 30), random(5, 30));

    birds[i].setColor(color(random(255), random(255), random(255)),
    color(random(255), random(255), random(255)));

    x[i] = random(20, 340);
    y[i] = random(30, 350);
    z[i] = random(1000, 4800);
    rx[i] = random(-160, 160);
    ry[i] = random(-55, 55);
    rz[i] = random(-20, 20);
    spd[i] = random(.1, 3.75);
    rot[i] = random(.025, .15);
    print(this);
  }
}


void draw(){
  background(150, 120, 255);
  lights();
  for (int i = 0; i < birdCount; i++){
    birds[i].setFlight(x[i], y[i], z[i], rx[i], ry[i], rz[i]);
    birds[i].setWingSpeed(spd[i]);
    birds[i].setRotSpeed(rot[i]);
    birds[i].fly();
  }
}



into Java mode, all I need to do is add "public class Flock extends PApplet {" at the top of the code, a "}" at the end of the code, a "this" argument to the Bird instantiation call, and I should be able to run the code. Instead I get the following error message: "Cannot parse error text: File C:\DOCUME~1\cmena\LOCALS~1\Temp\build56498.tmp\Flock.java is missing"

Are there additional steps that are missing? Do I need to put this code into a new tab or rename it? This is driving me insane.
Re: Converting main sketch code into Java mode
Reply #1 - Dec 15th, 2009, 1:14pm
 
PS. Here's the code for the Bird.java class:

Quote:
import processing.core.PApplet;

public class Bird{

  private PApplet p;
  private float offsetX, offsetY, offsetZ;
  private float w, h;
  private int bodyFill;
  private int wingFill;
  private float ang = 0, ang2 = 0, ang3 =0, ang4 = 0;
  private float radiusX = 120, radiusY = 200, radiusZ = 700;
  private float rotX = 15, rotY = 10, rotZ = 5;
  private float flapSpeed = .4f;
  private float rotSpeed = .1f;

  public Bird(PApplet p){
    this(p, 0, 0, 0, 60, 80);
  }

  public Bird(PApplet p, float offsetX, float offsetY,
  float offsetZ, float w, float h){
    this.p = p;
    this.offsetX = offsetX;
    this.offsetY = offsetY;
    this.offsetZ = offsetZ;
    this.h = h;
    this.w = w;
    bodyFill = p.color(200, 100, 10);
    wingFill = p.color(200, 200, 20);
  }

  public void setOffsetX(float offsetX){
    this.offsetX = offsetX;
  }

  public float getOffsetX(float offsetX){
    return offsetX;
  }

  public void setColor(int bodyFill, int wingFill){
    this.bodyFill = bodyFill;
    this.wingFill = wingFill;
  }

  public void setFlight(float radiusX, float radiusY, float radiusZ,
  float rotX, float rotY, float rotZ){
    this.radiusX = radiusX;
    this.radiusY = radiusY;
    this.radiusZ = radiusZ;

    this.rotX = rotX;
    this.rotY = rotY;
    this.rotZ = rotZ;
  }

  public void setWingSpeed(float flapSpeed){
    this.flapSpeed = flapSpeed;
  }

  public void setRotSpeed(float rotSpeed){
    this.rotSpeed = rotSpeed;
  }

  public void fly(){
    p.pushMatrix();
    float px, py, pz;
    p.fill(bodyFill);

    //flight
    px = p.sin(p.radians(ang3))*radiusX;
    py = p.sin(p.radians(ang3))*radiusY;
    pz = p.sin(p.radians(ang4))*radiusZ;
    //

    p.translate(p.width/2+offsetX+px, p.height/2+offsetY+py,
    -500+offsetZ+pz);

    p.rotateX(p.sin(p.radians(ang2))*rotX);
    p.rotateY(p.sin(p.radians(ang2))*rotY);
    p.rotateZ(p.sin(p.radians(ang2))*rotZ);

    //body
    p.box(w/5, h, w/5);

    p.fill(wingFill);
    //left wing
    p.pushMatrix();
    p.rotateY(p.sin(p.radians(ang))*20);
    p.rect(0, -h/2, w, h);
    p.popMatrix();

    //right wing
    p.pushMatrix();
    p.rotateY(p.sin(p.radians(ang))*-20);
    p.rect(-w, -h/2, w, h);
    p.popMatrix();

    //wing flap
    ang += flapSpeed;
    if (ang > 3){
      flapSpeed *= -1;
    }
    if (ang < -3) {
      flapSpeed *= -1;
    }

    // ang's run trig functions
    ang2 += rotSpeed;
    ang3 += .7;
    ang4 += .55;
    p.popMatrix();
  }
}


Re: Converting main sketch code into Java mode
Reply #2 - Dec 15th, 2009, 1:31pm
 
Phattee wrote on Dec 15th, 2009, 1:14pm:
Do I need to put this code into a new tab or rename it

Uh I don't know the book, but "to convert Processing code [...] into Java mode" seems to carry the meaning of trying to make a PApplet outside of the PDE. Eg. in Eclipse or some other IDE or even a simple text editor and the good old javac command.
Re: Converting main sketch code into Java mode
Reply #3 - Dec 15th, 2009, 2:39pm
 
Put Bird.java in its own tab, and...

add the missing PApplet reference argument:
birds[i] = new Bird(random(-300, 300), random(-300, 300),
   random(-500, -2500), random(5, 30), random(5, 30));

birds[i] = new Bird(this, random(-300, 300), random(-300, 300),
   random(-500, -2500), random(5, 30), random(5, 30));
Re: Converting main sketch code into Java mode
Reply #4 - Dec 15th, 2009, 3:20pm
 
Ok, something weird is happening:

I open the original main flock sketch, make the changes specified in the book, and sketch doesn't work

BUT

If I make a new sketch and copy the code from the old main tab into the new main tab and the code from the old Bird.java tab into the new Bird.java tab, then the sketch runs perfectly.

This is the exact same code in both Processing windows, but one works and the other doesn't. Something like this happened earlier with just the single bird sketch: if I try to java-ize the original sketch, it won't work, but if I java-ize it then copy it onto a new sketch, then it does work.

Am I doing something wrong?
Re: Converting main sketch code into Java mode
Reply #5 - Dec 16th, 2009, 1:57am
 
I have seen that in the past.
Important advice: don't give to your sketch the same name as one of your classes, as the name of the sketch is used to make the main class.
Re: Converting main sketch code into Java mode
Reply #6 - Dec 16th, 2009, 10:22am
 
Hey! Thank you all very much for your help and your patience.


public class Flock extends PApplet {



The error was occurring because the name for the class, Flock did not match the name of the main tab. Once I changed the name of the main tab so it matched the name of the class (or vice versa) then it worked fine. I'll be extra careful from now on when it comes to naming tabs and classes.

Page Index Toggle Pages: 1