4
$\begingroup$

I made a tool for manual creation of raster image.

First cell code (after executing click the grid to paint custom image):

Style[Grid[
  Table[Toggler[
    0, {0 -> Checkbox[1, {1, 2, 3}], 1 -> Checkbox[3, {1, 2, 3}]}], 
   10, 10], Spacings -> {0.1, -0.4}], Selectable -> False, 
 Editable -> False]

Next cell code (execute to retrieve data from previous cell to create graphics):

NotebookRead[PreviousCell[]][[1, 1, 1, 1, 1]] /. 
  TogglerBox[x_, _, _] -> x;
Graphics@Raster@Reverse@%

Is there a better method of retrieving the data from the grid without using NotebookRead?

I tried to set some variable like grid=Dynamic[Style[Grid[... but then the variable grid does not react to the changes made by clicking on the grid.

enter image description here

$\endgroup$

2 Answers 2

4
$\begingroup$

EDIT I have suppressed a useless part of my code EDIT END

You may be interested by this totally different approach :

grAdd = {};

ClickPane[
 Graphics[{Dynamic[Rectangle /@ grAdd]}, 
  PlotRange -> {{0, 50}, {0, 50}}
  , GridLines -> {Range[0, 50], Range[0, 50]}, 
  Frame -> True], (If[MemberQ[grAdd, Floor[#]]
    , grAdd = DeleteCases[grAdd, Floor[#]], 
    AppendTo[grAdd, Floor[#]]]) &]

Dynamic[grAdd]

enter image description here

$\endgroup$
7
  • $\begingroup$ Looks good. Should be 0 instead of -50 in your code. $\endgroup$ Commented Apr 10 at 17:57
  • $\begingroup$ Is there an easy way to adjust it also for mouse dragging for a free hand draw? Also I prefer the output as a raster. $\endgroup$ Commented Apr 10 at 18:02
  • $\begingroup$ Dragging to move a point or add points on the trajectory ? $\endgroup$
    – andre314
    Commented Apr 10 at 18:18
  • $\begingroup$ Adding points on trajectory, do not bother too much, only if you see an easy adjustment, the code is already good as it is. $\endgroup$ Commented Apr 10 at 18:20
  • $\begingroup$ Converting to raster instead of using rectangles: raster = ConstantArray[1, {50, 50}]; (Part[raster, Sequence @@ (# + 1)] = 0) & /@ grAdd; Graphics@Raster@Transpose@raster $\endgroup$ Commented Apr 10 at 18:24
3
$\begingroup$

Just use Dynamic.

Clear[x];

n = 10;
x[_, _] = 0;
Style[Grid[Table[With[{i = i, j = j}, 
    Toggler[Dynamic[x[i, j]], {0 -> Checkbox[1, {1, 2, 3}], 
      1 -> Checkbox[3, {1, 2, 3}]}]], {i, n}, {j, n}], 
  Spacings -> {0.1, -0.4}], Selectable -> False, Editable -> False]

Graphics@Raster@Reverse@Array[x, {n, n}]
$\endgroup$
1
  • 1
    $\begingroup$ That is what I wanted to avoid, using so much dynamic variables slows down response of the grid for larger n, say n=50. Isn't there a way to have one dynamic variable that contains the whole grid? $\endgroup$ Commented Apr 10 at 17:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.