I am running Mathematica 7. However, I am new to using Dynamic, DynamicModule, EventHandler, and the like.

I would like to create a Graphics object containing numDisksDisk objects, where numDisks is a positive integer. I would like each of the Disk objects to be Red, Green, or Blue. The default color is Red, but if a disk is clicked, that disk goes to the next color, cyclically (Red-->Green-->Blue-->Red-->...). Is it possible to accomplish this using a DynamicModule and multiple EventHandler declarations?

I have tried the following. This code generates numDisks disks in a row. When the user clicks anywhere within the Graphics box, the color of all three disks changes to the next color in the sequence.

However, what if I would like each of the numDisksDisk objects to have its own color, which can be changed independently of that of all of the other Disk objects? Is this possible? Can this be accomplished somehow using multiple EventHandler declarations? Thanks for your time.

To the person who voted to reopen: please tell me why. I would be happy to reopen this if you can explain to me why it is not a duplicate. By my reading and by comparing the Accepted answers to both questions I think it is.
–
Mr.Wizard♦Jan 19 '13 at 22:59

1 Answer
1

You want to create an independent DynamicModule for each EventHandler. Otherwise, the single variable col will control the color for all of the disks, as you're witnessing. Your code can be rearranged as follows:

Okay...per your request in the comments, here's a variation which uses a single DynamicModule, but makes the col variable a list which is then referenced separately by the individual EventHandler constructs. The only thing that's tricky about this is that you have to inject the value of i from the Table inside of EventHandler and Dynamic using With.

Thank you! This might be a separate question, but is it possible to somehow save the current color state of each Disk to a list? For example, suppose that I have three disks: (1), (2), and (3). Further suppose that I click the disks so that disk (1) becomes Red, (2) becomes Green, and (3) becomes Blue. Is it possible to save -- perhaps dynamically -- the current color states of (1), (2), and (3) to a list?
–
AndrewJan 19 '13 at 18:12

Mathematica is a registered trademark of Wolfram Research, Inc. While the mark is used herein with the limited permission of Wolfram Research, Stack Exchange and this site disclaim all affiliation therewith.