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