Help with this Processing / Java syntax

Hello, I’m new to Processing and Java. I’m following along with the “Generative Design” book (first edition / Java / Processing version of the book) and I’ve come across some syntax which I’m unsure about. I’m not sure if this is Processing-specific, or a general Java question, hence asking for help (as I’m not sure where to go to help myself :slight_smile:)

int tileCountX = 10;
int tileCountY = 10;

color[] colorsLeft = new color[tileCountY];
color[] colorsRight = new color[tileCountY];
color[] colors;

In this case, what is new color[tileCountY] doing? I’m aware that color is a processing function, but I’m confused by the array syntax in combination with the new keyword. Are we instantiating a new color array, with a length equal to tileCountY?

Here’s the rest of the code for the sketch:

int tileCountX = 10;
int tileCountY = 10;

color[] colorsLeft = new color[tileCountY];
color[] colorsRight = new color[tileCountY];
color[] colors;

void shakeColors() {
  for (int i = 0; i < tileCountY; i++) {
    colorsLeft[i] = color(random(0, 60), random(0, 100), 100);
    colorsRight[i] = color(random(160, 190), 100, random(0, 100));
  }
}

void mouseReleased() {
  shakeColors();
}

void setup() {
  size(800, 800);
  colorMode(HSB, 360, 100, 100, 100);
  noStroke();
  shakeColors();
}

void draw() {
  tileCountX = (int) map(mouseX, 0, width, 2, 100);
  tileCountY = (int) map(mouseY, 0, height, 2, 10);
  float tileWidth = width / (float)tileCountX;
  float tileHeight = height / (float)tileCountY;
  color interCol;
  
  for (int gridY = 0; gridY < tileCountY; gridY++) {
    color col1 = colorsLeft[gridY];
    color col2 = colorsRight[gridY];
    
    for (int gridX = 0; gridX < tileCountX; gridX++) {
      float amount = map(gridX, 0, tileCountX-1, 0, 1);
      
      interCol = lerpColor(col1, col2, amount);
      
      fill(interCol);
      float posX = tileWidth * gridX;
      float posY = tileHeight * gridY;
      rect(posX, posY, tileWidth, tileHeight);
    }
  }
}

If it helps, I’m a JavaScript developer. Just needing a bit of help to think in Java.

Any help appreciated. Thanks!

Yes, color() is a method from classes PApplet & PGraphics:

On that context color is both a Processing’s primitive datatype & keyword:

But when it’s transpiled to Java syntax color becomes the primitive datatype & keyword int:

In short color is an alias to int. There’s no diff. if we use 1 or the other within a “.pde” file. :wink:

Yes, we just make a new array of type color here

Hi @GoToLoop thanks for the links, that really answers my question. I’m aware this is basic stuff, so thanks for taking the time to help me. Cheers!

Hi @Chrisir, yes that’s what I was thinking. It’s good to be able to ask for the help with these things. Cheers :slight_smile:

I’ve also realised I could have used printArray to inspect the value of colorsLeft here. Thanks again.

1 Like

There is also a tutorial about arrays