Translating sketch from P5 to Java

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) 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?

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();
  }
}```
1 Like

I don’t think angleMode is available in Processing java. 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().

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. 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

4 Likes

Thanks, I’ll try to find a workaround.