# Translating sketch from P5 to Java

**URL:** <https://discourse.processing.org/t/translating-sketch-from-p5-to-java/10052>\
**Category:** Coding Questions\
**Created:** [April 7, 2019, 6:37pm UTC](https://discourse.processing.org/t/translating-sketch-from-p5-to-java/10052 "2019-04-07T18:37:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![PixelMx](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/pixelmx/32/4478_2.png) [@PixelMx](https://discourse.processing.org/u/PixelMx)\
**Post date:** [April 7, 2019, 6:37pm UTC](https://discourse.processing.org/t/translating-sketch-from-p5-to-java/10052/1 "2019-04-07T18:37:36Z")

</div>

I was beginning to start programming with Processing (Java) but now I see most sketches created in P5.  
I like Java better because it allows me to export frames to png files and create animations from there.

I’m not familiar with P5 but I’m trying to figure out a sketch ([https://www.openprocessing.org/sketch/678744](https://www.openprocessing.org/sketch/678744)) and translate to java.

I’m having some problems so far:

I marked as comments the functions that won’t run and that I’m not sure how to translate.

first is angleMode(DEGREES); won’t work, do I need to import a library?  
shuffle does not work, I changed it to .shuffle but no luck, would it work the same if I use random instead?

````auto
color[][] colorPalettes = {
  {#152A3B, #0D809C, #F5C03E, #D63826, #EBEBD6},
  {#0F4155, #5399A1, #8CA96B, #CB5548, #E7E6F5},
  {#E8614F, #F3F2DB, #79C3A7, #668065, #4B3331},
  {#DBE5EC, #336B87, #2A3132, #E94D35, #EFAC55}
    };
int[] queueNumber = {
0, 1, 2, 3, 4
    };
int size = 500;
int num = 10;
int i = 2;
int j = 1;
int currentPalette, l;

void setup() {
  
  size(1920, 1080);
  background(25);
 // angleMode(DEGREES);
  noLoop();
}

void draw() {
  noStroke();
  l = size / num;
  currentPalette = colorPalettes[i][j];
  for (int x = 0; x < size; x += l) {
    for (int y = 0; y < size; y += l) {
  // queueNumber = .shuffle(queueNumber);
// fill(currentPalette[queueNumber[0]]);
      rect(x, y, l, l);
// fill(currentPalette[queueNumber[1]]);      
      switch(round(random(0.51, 9.49))) {             
          case 1: triangle(x, y, x + l, y, x, y + l); break;
          case 2: triangle(x, y, x + l, y, x + l, y + l); break;
          case 3: triangle(x + l, y + l, x + l, y, x, y + l); break;
          case 4: triangle(x, y, x, y + l, x + l, y + l); break;
          case 5: arc(x + l/2, y + l/2, l, l, 0, 180); break;
          case 6: arc(x + l/2, y + l/2, l, l, 90, 270); break;
          case 7: arc(x + l/2, y + l/2, l, l, 180, 0); break;
          case 8: arc(x + l/2, y + l/2, l, l, 270, 90); break;
          case 9: ellipse(x + l/2, y + l/2, l, l); break;
      }
    }
  }
  paper();
}

void paper() {
  for (int i = 0; i < width; i += 2) {
    for (int j = 0; j < height; j += 2) {
      fill(random(175, 225), 25);
      rect(i, j, 2, 2);
    }
  }
}

void mouseClicked() {
  i = (i + 1) % 4;
  redraw();
}

void keyPressed() {
  if (keyCode >= 49 && keyCode <= 57) {
 // num = Number(key);
    redraw();
  }
}```
````

---

<div class="post-metadata">

**Author:** ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)\
**Post date:** [April 9, 2019, 4:05am UTC](https://discourse.processing.org/t/translating-sketch-from-p5-to-java/10052/3 "2019-04-09T04:05:13Z")

</div>

> [@PixelMx](#):
>
> first is angleMode(DEGREES); won’t work, do I need to import a library?

I don’t think [angleMode](https://p5js.org/reference/#/p5/angleMode) is available in [Processing java](https://processing.org/reference/). Instead, you should either use the `degrees()` or `radians()` to convert between these units. Then, you need to use the right units when working with your core functions. In other words, if the function requires radians units (you can check this type of info in the function’s own docs) then you will use the value in radians or convert it to radians using `radians()`.

> [@PixelMx](#):
>
> shuffle does not work

[Shuffle](https://p5js.org/reference/#/p5/shuffle) is another function that is only available in P5js and not in Processing, or at least it is not readily available. You can find something alike by checking the `FloatList` class [here](https://processing.org/reference/FloatList.html). Usually it is not possible to convert code from java to js or vise-versa line by line. You need to understand the original function and find a similar function in the other language.

Kf

---

<div class="post-metadata">

**Author:** ![PixelMx](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/pixelmx/32/4478_2.png) [@PixelMx](https://discourse.processing.org/u/PixelMx)\
**Post date:** [May 9, 2019, 4:32am UTC](https://discourse.processing.org/t/translating-sketch-from-p5-to-java/10052/4 "2019-05-09T04:32:17Z")

</div>

Thanks, I’ll try to find a workaround.
