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)
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]?
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) }?
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).
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?
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