I need help with filling in a grid

Hi,i created a 3x3 grid and i need each cell in the grid to change colour when clicked but it fills the entire screen. I’m, also unfortunately not allowed to use 2D arrays or the 'color function as I’ve seen online. Can someone please help me?

int noofGrids=3;
int Sizelength=500;
float SizeofGrid=Sizelength/noofGrids;



void setup() {
  size(500, 500);
}
void draw() {
  background(127);

  drawGrid();
}
void drawGrid() {
  for (int i=0; i<=noofGrids; i++) {
    for (int j=0; j<=noofGrids; j++) {
      float x=i*SizeofGrid;
      float y=j*SizeofGrid;
      rect(x, y, SizeofGrid, SizeofGrid);
    }
  }
}
void mouseClicked() {
  for (int i=0; i<noofGrids; i++) {
    for (int j=0; j<noofGrids; j++) {
     float x=i*SizeofGrid;
      float y=j*SizeofGrid;
    
      if ((mouseX>x && mouseX<x+SizeofGrid) &&( mouseY>y && mouseY<y+SizeofGrid)) {
        println(x, y);
     fill(0);
      
        
    
      }
     
  }
}}

Without storing any data:
get rid of background in draw() and draw the rect in mouseClicked with the new color

Don’t call drawgrid from draw, call it from setup ().

Other approach

Are you allowed to use a 1D grid? Then you can store the color (or int) in this. Use a counter in the nested for loop to keep track (use it as an index for the array)

1 Like

thank you so much this helped a lot!, i would like to see an alternative to using a 1D grid if you dont mind. so could i use int[] color=new int[xy or ij or gridCol*gridCol]?

In my 2nd approach you use background again

You store the fact that a cell is pressed in mouseclicked

You store it in a 1D array

You evaluate the array in drawgrid

So you start a counter = 0 before the for loop in mouseclicked

When the if clause applies you say color [counter]= 1;

You increase the counter inside the inner for loop


in drawgrid use an if clause for color[] to set fill

im unfortunately not allowed to use the color function, if that’s what you meant ;(. if you’re referring to int []color , So the counter will be put in the array and used to fill in the grid?.

if (mouseClicked){counter++
filll(color[counter)];
rect(x,ysize,size) }?

I didn’t mean the color function just an array that you can name as you want

before setup () :

int[] myColors…

or even type boolean

When I wrote mouseclicked I was referring to your function

Make changes to it as I suggested above

myColors[counter]=1;

in drawgrid function evaluate myColors[counter] with if

or use fill ( myColors[counter] );

1 Like

Did you make progress here? Anything I can help you with?

Please show your entire code then…

oh wow thanks for checking up ! yes the grid is filling in ,I’m still working on other things and trying to figure it out on my own:)

1 Like

Good afternoon/evening, if you’d still be willing to help, could you please tell me if there’s anyway to assign a string array to a variable? for instance if i wanted to select the proper array based on the value given by the user(e.g if user enter 2 point to a certain array), how would i assign that? I hope this is coherent,i cant really post my entire code since someone might copy it :(.

for instance i created a function that has integer input(used to determine the array to be chosen) and a string array as parameters,how to i point the string array(lets say string[] eat) to the proper one it should use because i cant go eat=right array).

//if I understand you correctly:

//You want to receive an integer input and give back an String Array

//You can use if OR a HashMap


int[] arr0 = { 0, 3, 4 };
int[] arr1 = { 3, 5, 4 };
int[] arr2 = { 9, 2, 8 };

int[] selectedArray = null; 

void setup() {
  size(700, 700);
}

void draw() {
  background(0);
  fill(255);
}

void keyPressed() {
  if (key=='0') {
    println("here");
    selectedArray=arr0.clone();
  } else if (key=='1') 
    selectedArray=arr1.clone();
  else if (key=='2') 
    selectedArray=arr2.clone();

  if (selectedArray!=null)
    println(selectedArray);
}

OR



// HashMap

// Note the HashMap's "key" is a int and "value" is an int[] (int array)
HashMap<Integer, int[]> hm = new HashMap<Integer, int[]>();

void setup() {
  size(700, 700);

  int[] arr0 = { 0, 3, 4 };
  int[] arr1 = { 3, 5, 4 };
  int[] arr2 = { 9, 2, 8 };

  hm.put(0, arr0); 
  hm.put(1, arr1);
  hm.put(2, arr2);
}

void draw() {
  background(0);
  fill(255);
}

void keyPressed() {
  if (key=='0'||key=='1'||key=='2') {
    // We can access values by their key
    int[] val = (int[]) hm.get(int(key+""));
    println(key+" gives " );
    printArray(val);
  }
}
//

Thank you for replying,i noticed that in the first answer you sent you essentially equated the two int arrays once a key is pressed, using .clone() which im guessing is a java function?

yeah, clone was new to me too

It really copies the array instead of just giving the reference.

see https://www.baeldung.com/java-deep-copy

yeah, well, the = sign is assigning the value of the right hand variable / array to the left hand variable (left of the equal sign)

oh no i know how to use the = sign lol its just that when i did it i had an error , ive identified the error i was making thank you, Can i ask another?its really short too

1 Like

sure

go ahead

Chrisir



import javax.swing.JOptionPane;

void setup() {
  size(500, 500);
  textSize(20);
}

void draw() {
  background(128); 
  fill(255);
  rect(50, 250, 400, 200);
  fill(0);
  text("Click here for MessageDialog", 90, 350);
  text("mousePressed status: " + (mousePressed), 90, 125);



  //  JOptionPane.showInputDialog("Error", JOptionPane.ERROR_MESSAGE);
}

void mousePressed() {
  if (mouseX > 50 && mouseX < 450 && mouseY > 250 && mouseY < 450) thread("MessageDialog_thread");
} 

void MessageDialog_thread() {
  JOptionPane.showMessageDialog(null, "OK.", "Message", JOptionPane.PLAIN_MESSAGE);
}

thank you for answering all these questions you’ve been a huge help!till next time!

1 Like