# Need help with converting java code to javascript code

**URL:** <https://discourse.processing.org/t/need-help-with-converting-java-code-to-javascript-code/3142>\
**Category:** p5.js\
**Created:** [August 31, 2018, 6:11pm UTC](https://discourse.processing.org/t/need-help-with-converting-java-code-to-javascript-code/3142 "2018-08-31T18:11:09Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![kernel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kernel/32/1299_2.png) [@kernel](https://discourse.processing.org/u/kernel)\
**Post date:** [August 31, 2018, 6:11pm UTC](https://discourse.processing.org/t/need-help-with-converting-java-code-to-javascript-code/3142/1 "2018-08-31T18:11:09Z")

</div>

Here is the code I found and I was trying to convert this into javascript code

```auto

pathfinder[] paths;
void setup() {
  size(800, 600);
  background(0);
  ellipseMode(CENTER);
  fill(255);
  noStroke();
  smooth();
  paths = new pathfinder[1];
  paths[0] = new pathfinder();
}
void draw() {
  for (int i=0; i<paths.length; i++) {
    PVector loc = paths[i].location;
    float diam = paths[i].diameter;
    ellipse(loc.x, loc.y, diam, diam);
    paths[i].update();
  }
}
void mousePressed() {
  background(0);
  paths = new pathfinder[1];
  paths[0] = new pathfinder();
}

class pathfinder {
  PVector location;
  PVector velocity;
  float diameter;
  pathfinder() {
    location = new PVector(width/2, height);
    velocity = new PVector(0, -1);
    diameter = 32;
  }
  pathfinder(pathfinder parent) {
    location = parent.location.get();
    velocity = parent.velocity.get();
    float area = PI*sq(parent.diameter/2);
    float newDiam = sqrt(area/2/PI)*2;
    diameter = newDiam;
    parent.diameter = newDiam;
  }
  void update() {
    if (diameter>0.5) {
      location.add(velocity);
      PVector bump = new PVector(random(-1, 1), random(-1, 1));
      bump.mult(0.1);
      velocity.add(bump);
      velocity.normalize();
      if (random(0, 1)<0.02) {
        paths = (pathfinder[]) append(paths, new pathfinder(this));
      }
    }
  }
}

```

Here is my p5js code

```auto
let branchs = [];  

function setup() {
  createCanvas(600, 400);
  background(0);
  fill(255);
  noStroke();
  let l = createVector(width/2,height);
  branchs.push(new Branch(l,20));
}

function draw() {
  for(let i=0;i<branchs.length;i++){ 
    branchs[i].update();
    ellipse(branchs[i].loc.x,branchs[i].loc.y,branchs[i].dia,branchs[i].dia); 
  } 
}

function Branch(parent){
  this.loc=0;
  this.vel=0;
  this.dia=0;
  if(parent){
    this.loc = parent.loc;
    this.vel = parent.vel;
    let area = PI*sq(parent.dia/2);
    let newDia = sqrt(area/2/PI)*2;  
    this.dia = newDia; 
    parent.dia = newDia; 
    return parent;
  }else{
    this.loc = loc;
    this.dia = dia;
    this.vel = createVector(0,-1);
  }

  this.update = function(){
    if(this.dia>5){ 
      this.loc.add(this.vel) ;
      let bp = createVector(random(-1,1),random(-1,1));
      bp.mult(random(-0.5,0.5));
      this.vel.add(bp);
      this.vel.normalize();  
      this.dia-=0.005;
      if(random(1)<0.002){
        for(let i=0;i<random(1,6);i++){
          append(branchs, new Branch(this))
        }
      }
    }
  }
} 

```

update the code according to the feedback and it throwing error now

**TypeError: branchs[i].update is not a function (sketch: line 14)**

---

<div class="post-metadata">

**Author:** ![Kevin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kevin/32/2297_2.png) [@Kevin](https://discourse.processing.org/u/Kevin)\
**Post date:** [August 31, 2018, 6:16pm UTC](https://discourse.processing.org/t/need-help-with-converting-java-code-to-javascript-code/3142/2 "2018-08-31T18:16:15Z")

</div>

Which part of this are you stuck on? Which line are you confused about?

Note that you shouldn’t try to translate code line-by-line. Instead, you should figure out **what the code is doing** and then implement that behavior in your target language.

---

<div class="post-metadata">

**Author:** ![kernel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kernel/32/1299_2.png) [@kernel](https://discourse.processing.org/u/kernel)\
**Post date:** [August 31, 2018, 6:19pm UTC](https://discourse.processing.org/t/need-help-with-converting-java-code-to-javascript-code/3142/3 "2018-08-31T18:19:13Z")

</div>

Java class has double constructor which is not possible in javascript. How do I do that?

```auto

pathFinder(){
}

pathFinder( pathfinder parent) {
}

```

Here is the live code - [https://editor.p5js.org/black/sketches/HJJ1Tpzv7](https://editor.p5js.org/black/sketches/HJJ1Tpzv7)

---

<div class="post-metadata">

**Author:** ![Kevin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kevin/32/2297_2.png) [@Kevin](https://discourse.processing.org/u/Kevin)\
**Post date:** [August 31, 2018, 6:21pm UTC](https://discourse.processing.org/t/need-help-with-converting-java-code-to-javascript-code/3142/4 "2018-08-31T18:21:15Z")

</div>

Speaking generally, JavaScript supports optional parameters:

```auto
function myFunction(myArg){
  if(myArg){
    // myArg was provided
  }
  else{
    // myArg was not provided
  }
}

myFunction();
myFunction('test');

```

---

<div class="post-metadata">

**Author:** ![kernel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kernel/32/1299_2.png) [@kernel](https://discourse.processing.org/u/kernel)\
**Post date:** [August 31, 2018, 6:24pm UTC](https://discourse.processing.org/t/need-help-with-converting-java-code-to-javascript-code/3142/5 "2018-08-31T18:24:09Z")

</div>

Yes it does but it wont return “this”. Which basically modify and return the parent passed in the second constructor in java class.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [August 31, 2018, 6:24pm UTC](https://discourse.processing.org/t/need-help-with-converting-java-code-to-javascript-code/3142/6 "2018-08-31T18:24:40Z")

</div>

You simply check whether the parameter _parent_ was initialized or not.

```auto
class PathFinder {
  constructor(parent) {
    parent? this.initParent(parent) : this.initEmpty();
  }

  initEmpty() {
    // ...
  }

  initParent(parent) {
    // ...
  }
}

```

---

<div class="post-metadata">

**Author:** ![Kevin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kevin/32/2297_2.png) [@Kevin](https://discourse.processing.org/u/Kevin)\
**Post date:** [August 31, 2018, 6:34pm UTC](https://discourse.processing.org/t/need-help-with-converting-java-code-to-javascript-code/3142/7 "2018-08-31T18:34:27Z")

</div>

> [@kernel](#):
>
> Yes it does but it wont return “this”.

It will if you tell it to:

```auto
function myFunction(){
  // whatever
  return this;
}

```

But again, I don’t think you should be trying to translate code line-by-line. You need to take a step back and write down **in English, not in code** what you want to happen when you run the code. Then take that English description (also called an algorithm) and implement that in your target language.

Trying to match the syntax one-to-one is just going to give you a headache, and will result in poorly written code.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [August 31, 2018, 6:39pm UTC](https://discourse.processing.org/t/need-help-with-converting-java-code-to-javascript-code/3142/8 "2018-08-31T18:39:17Z")

</div>

Some tips to Processing’s Java Mode to p5js conversion:

- [GitHub.com/processing/p5.js/wiki/Processing-transition](http://GitHub.com/processing/p5.js/wiki/Processing-transition)

Some sketches w/ both Java Mode & p5js versions:

> **[r/processing - OpenProcessing: converting code written in Java to JavaScript](https://www.reddit.com/r/processing/comments/7dqqvh/openprocessing_converting_code_written_in_java_to/dq0ecac/)**
>
> 9 votes and 14 comments so far on Reddit

---

<div class="post-metadata">

**Author:** ![kernel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kernel/32/1299_2.png) [@kernel](https://discourse.processing.org/u/kernel)\
**Post date:** [August 31, 2018, 6:43pm UTC](https://discourse.processing.org/t/need-help-with-converting-java-code-to-javascript-code/3142/9 "2018-08-31T18:43:16Z")

</div>

I agree with you on this 🙂 🙂
