Finding an item in an arrayTopA fancier drawing program

A fancier drawing program

For the next example, we go back to the drawing program that we first used as an example of using GUI components. It has choice buttons to determine the shape of the object to be drawn (square or circle) and its color. Now we would like to enhance the program to allow the user to click on one of the objects on the canvas and drag it to a new position. We do that by adding a new choice button that will allow us to choose whether we wish to draw a new geometric object, or select an old one to drag. The drawing is handled as before, but selecting an element used to be impossible because once we created a new object, the variable holding the old object now only refers to the new object, leaving the old object inaccessible. We will now make up for that by keeping all of the entries on the screen in a list.

Demo: Drawing program

Notice the code to create a new list does not create any items in the array. That is handled by the call of addNew in onMousePress.

The nice thing about our lists is that they keep track of their size. (Use the size method.) Thus if we add an element and then ask for the size, it will return a value one larger than before. The other nice features of lists is that they never (well hardly ever) run out of space. They grow as needed when new elements are added.


Finding an item in an arrayTopA fancier drawing program