Color grid array problem
in
Programming Questions
•
2 years ago
Hi I'm a beginning processing user and I've been stumped by this problem for a couple of days.
I've been trying to create a grid of squares that the user can select the color of each square within the grid. The problem that I'm having is that as soon as I release my mouse button the color of the square I was trying to change goes back to black.
I know that my problem is being caused by the fact that I'm using c (line 78) as the fill value for all of my squares. I think the solution is to create an array of c values, but arrays are still a bit of a mystery to me and I keep getting errors with the different methods I've tried.
Thank you in advance for your help
- MyRect [] []grid;
- int k, kc;
- int c;
- int r = color(255,0,0);
- int g =color(0,255,0);
- int b =color(0,0,255);
- int cols = 17;
- int rows = 10;
- void setup() {
- size (450,300);
- grid = new MyRect [cols] [rows];
- for (int i =0; i < cols; i++) {
- for ( int j = 0; j< rows; j++) {
- grid[i][j] = new MyRect (i*21+5,j*21+5,20,20,i,j);
- }
- }
- }
- void draw() {
- background(0);
- smooth();
- fill(r);//(0,0,255);
- rect(365,20,40,40);
- fill(g);//(0,255,0);
- rect(365,70,40,40);
- fill(b);//(255,0,0);
- rect(365,120,40,40);
- for (int i = 0; i < cols; i++) {
- for ( int j = 0; j< rows; j++) {
- grid[i][j].draw();
- if(mousePressed == true) k= get (mouseX,mouseY);
- //kc = k;
- //c=kc;
- if (k == r)kc = k;
- if (k == g)kc = k;
- if (k ==b)kc = k;
- fill(kc);
- rect(365,180,40,90);
- }
- }
- }
- class MyRect {
- float x, y, wide, high, bi, bj;
- MyRect(float x, float y, float wide, float high, float bi, float bj) {
- this.x = x;
- this.y = y;
- this.wide = wide;
- this.high = high;
- this.bi = bi;// column number
- this.bj = bj;// row number
- stroke(128);
- }// thinking of using bi & bj later as part of the color transfer process
- void draw() {
- if(over()) {
- stroke(255,0,255);
- }
- else {
- stroke(255);
- }
- if((mousePressed == true)&& (over()==true))c=kc;// k is get, kc is if k equals stored
- else if ((mousePressed == true)&& (over()==false)) c = 0;
- fill(c);// I realize that my problem is that I'm using a single variable for my fill colors, I think the
- // solution is probaly to use an arry for the color but I'm still having trouble using arrays
- rect(x, y, wide, high);
- }
- boolean over() {
- if (mouseX >= x && mouseX <= x+wide &&
- mouseY >= y && mouseY <= y+high) {
- return true;
- }
- return false;
- }
- }
1