9

I want to make an interface where a user can fill in a square grid by clicking. Some processing will then be done with these pixels to produce a new picture. How can I make this clickable interface? I thought about linking each little square to a set of mouse positions, but this seems unnecessarily complicated.

Grid

gilianzz
  • 235
  • 2
  • 5

3 Answers3

15

just select n for a nxn grid

n = 5;
Grid[Partition[
Table[DynamicModule[{col = White}, 
EventHandler[
 Dynamic[Graphics[{EdgeForm[Thick], col, Rectangle[]}, 
   ImageSize -> 
    Tiny]], {"MouseClicked" :> (col = 
     col /. {Black -> White, White -> Black})}]], {n^2}], n]]

here is the result

enter image description here

shrap
  • 195
  • 1
  • 6
5
n = 10;
m = ConstantArray[0, {n, n}];
Row[{Dynamic[EventHandler[ArrayPlot[m, Mesh -> All, ImageSize -> 300], 
  {"MouseClicked" :> With[{p = Reverse[{1, n} + {1, -1} Floor@MousePosition["Graphics"]]},
   m[[## & @@ p]] = m[[## & @@ p]] /. {0 -> 1, 1 -> 0}]}]], 
 Dynamic[Style[MatrixForm[m], 24]]}, Spacer[20]]

enter image description here

kglr
  • 394,356
  • 18
  • 477
  • 896
1

I think I found a "way to get the information out of the picture"

First Run the code in the first answer

Let's say you get

Out[15]=....(Grid)...   

then you manipulate the grid and when you are done you must "run" (shift+enter) the final grid.

so, you'll get a new output like

 Out[17]=....(Grid)...    

we are interested in the number "17" of the final output.
you take this number and place it in the begining of the function below

out = %17;
Map[ToExpression[(Characters @@ StringCases[ToString@InputForm[
      #], "vel[" ~~ __ ~~ "]}, E"])[[5]]] &, 
First[out], {2}] /. {0 -> 1, 1 -> 0}   

this will give you the result you asked in the comment of the first answer

ZaMoC
  • 6,697
  • 11
  • 31