Instance method not being called using a 2D array
in
Programming Questions
•
7 months ago
I am probably missing something obvious, but I can't figure out where I am making a mistake.
there are 3 issues:
1. first object in the array doesn't set the color after initialization
2. setColor(..) is not called in the void draw() at all
3. getColor() method returns some weird value
- Pixel [][] pixelsArray;
- int numRows = 20;
- float pixelSize = 30;
- int screenSize = (int)pixelSize * numRows;
- void setup()
- {
- size (screenSize, screenSize);
- background(0);
- pixelsArray = new Pixel [numRows][numRows];
- for (int i = 0; i < numRows; i++)
- {
- for (int j = 0; j <numRows; j++)
- {
- pixelsArray[i][j] = new Pixel(i*pixelSize, j*pixelSize, pixelSize);
- pixelsArray[i][j].setColor(color(0,0,255));
- }
- }
- println(pixelsArray[1][0].getColor());
- }
- void draw()
- {
- pixelsArray[1][1].setColor(color(255,0,0));
- }
- class Pixel
- {
- color c;
- float xPos;
- float yPos;
- float pixSize;
- Pixel(float xPos, float yPos, float pixSize)
- {
- this.xPos = xPos;
- this.yPos = yPos;
- this.pixSize = pixSize;
- rect(xPos,yPos,pixSize,pixSize);
- }
- void setColor(color c)
- {
- this.c = c;
- fill(this.c);
- }
- color getColor()
- {
- return c;
- }
- }
1