Interaction

In this lab, you are going to paint the stars.

Downloading the project framework

Download StarPress.zip, and unZIP it into your csci/201 directory. Make your Projects panel look like the following continuing.
Projects panel for StarPress
The only class you'll modify in this lab is StarPress, so go ahead and open it.

The project looks quite a bit like the 50 star union program but when you run it nothing happens.

Listing to the mouse

Notice that about halfway into StarPress.java there is a call to the addMouseListerne method. The single argument to these method is an anonymous class made by extending the MouseAdapter class. This is some really unusual Java, but you only need to pay attention to the three methods of this anonymous class: mouseClicked, mousePressed; mouseReleased. A bit farther down you see a call to addMouseMotionLister which receives another anonymous class that extends MouseMotionAdapter. This class has a single method mouseDragged.

All your work during this lab will be confined to these four methods.

  1. mouseClicked
  2. mousePressed
  3. mouseReleased
  4. mouseDragged

This methods are invoked whenever you click, press, release, or drag the mouse. Each is passed a MouseEvent object. For this lab, the only thing you really must know about a MouseEvent is that it has methods getX() and getY() for returning the x and y co-ordinates where the mouse was located at the time of the event.

StarPress does have calls to another MouseEvent method and uses a couple of MouseEvent fields. But you don't have to modify that part of the code.

Adding a star

Look within the mouseClicked for the following comment line:

                    // Your task #1:  Add a star to the array

Add a single line of Java code to create a new Star5 object and insert it into the array. Remember to use the getX() and getY() methods of the MouseEvent object e. [If you need help there, look down five more lines in your program.] Use the fixed variable starRadius for the size of your star and the random variable starRot for the rotation. You better have “stars[numStars++]” somewhere in your line. The “++” is important.

Compile your code and make sure that new stars appear whenever you click the mouse. Make 51 clicks and see your program crash. Place your Java assignment statement with an if to fix this problem.

Deleting stars

Now go on down to your next task comment:

                            // Your task #2:  Remove a star from the array

Now this is tricky. Your program has identified a star, stars[i] that is very close to the location of your mouse press. We want to delete it. The problem is that we can't just leave a “hole” in the array.

One way to handle this problem would be to move all the array elements from stars[i+1] to stars[numStars-1] up one position and then subtract one from numStars. Perhaps a loop like the following would help out here.

                            // Your task #2:  Remove a star from the array
                            for (int j=i; j<numStars-1; ++j) {
                                // Do something here
                            }  

Remember to adjust numStars. Also call the repaint() method so that you can see that star has been removed. You might also think about what should happen if more than one star is close to the point where the mouse was pressed.

Moving stars

Moving stars require changes as three locations marked with “Your task” comments. Two of these changes (3 and 4) involve adding one line of code. One (5) requires about three. The idea here is that you use the variable taggedStar to tag a star. The method mousePressed is to set the tag. As the star is dragged, mouseDragged is called. Here you have to use the setX and setY methods of Star5 to change the star's position. Then in mouseReleased you have to untag the star.

Show your instructor that you have signed up for CSCI 202 for next semester.