(15 points)
You did a great job in homework 1. But you are not fully happy. The picture is static. Wouldn't be it better to animate the graphics elements within the picture? You could just bring up the picture on your computer, hit a button and have the spiders crawl up and down and the pumpkins flash all through the night. That would really add some zest to your Halloween party!
Overview
In this assignment, you will reuse and modify the four classes that you wrote in homework 1, namely Pumpkin, Spider, Candle and the fourth one that you created. I give you a new version of HalloweenScene that contains the code to do the animation (You don't have to understand the animation part to do this assignment. We might look at some animation later in the quarter).
What you are aked to do is to add functionalities to your classes. For instance you will add a move method to the Spider class to displace the spider in the graphics window.
The details
Download the new version of HalloweenScene.java.Create an Eclipse project that contains HalloweenScene and the four element classes that you created in homework 1.
Start by modifying the code within HalloweenScene. There is very little to
do. At the beginning of the class, create an instance field named myElement
of the type that you created in homework 1 (read through the code and modify
the line "private classname myElement;"). Then scroll
down to the last method named addGraphicsElements. Within that method, the instance
field myElement is instantiated. The statement is not complete. It reads this.myElement
= new ???. Complete it.
This is it for HalloweenScene. Of course, you can also modify the coordinates within the constructor calls if you don't like the positioning of the elements within the window.
What you have left to do is to add methods to the other four classes (these methods are called within HalloweenScene to perform the animation). Here is your contract for each of the class:
Spider class
Add one method with the following signature
public void move()
This method should move the spider up or down by a certain distance (the exact value is up to you). If the spider is moving up past the top of the screen, the spider should reverse its direction the next time move is called. If the spider is moving down past the bottom of the screen, the spider should reverse its direction the next time move is called.
A few hints: keep track in a boolean instance field, e.g. isMovingUp, whether the spider is moving up or down. Whenever the spider is moving (i.e. whenever move is called), check if the spider is past the top or the bottom of the window and modify isMovingUp accordingly (i.e. set it to true or false). To know if the spider is past the window's top and bottom, compare the y of the spider to 0 for the top and to this.window.getWindowHeight() for the bottom. Then, move the spider by changing its y. If isMovingUp is true, subtract the distance moved in one step to y. If isMovingUp is false, add the distance to y.
Of course, changing y doesn't move the spider on the screen. What is left to do is to erase the current spider and draw a new one at the new location.
Oval spiderHead = new Oval(....);).
If you did so, you can't write this.window.remove(spiderHead);
within move, since spiderHead is not visible beyond draw. How to make it visible
beyond draw? The trick is to make it an instance field. You would write Oval
spiderHead; at the top of the class (where the instance fields are
declared). And you would write this.spiderHead = new Oval(...);
within draw (make sure that you don't write Oval spiderHead,
since you would still have spiderHead as a local variable). After that modification
this.window.remove(spiderHead); will work within move. Of course,
you will need to do the modification for all of the graphics elements making
up the spider. this.draw();), since
the location (x,y) of the spider has already been changed.
Pumpkin class
Add two methods with the following signature
public void flipEyeNoseMouthColor()
public void changeSize()
flipEyeNoseMouthColor
An approach is to select two colors (e.g. red and yellow). flipEyeNoseMouthColor
should switch the colors of the eyes, nose and mouth of the pumpkin between
these two colors. That is the first time the method is called, the eyes, nose
and mouth become red and the second time, they go back to yellow and so on.changeSize
changeSize should increase or decrease the size of the pumpkin. Increase
or decrease the size by changing the scale. To increase the size, multiply
the scale by 1.1. To decrease the size, divide the scale by 1.1. Then redraw
the pumpkin with that new scale. The scale should not be greater than 2 or
less than 0.5. This is very similar to the spider moving up or down. Just
follow a similar approach here (use a boolean instance field isIncreasing,
etc...).Candle class
Add one method with the following signature
public void flicker()
flicker should redraw the flame of the candle at a slightly different location. By having an instance of HalloweenScene call flicker over and over (this is what the code in HalloweenScene does), the flame of the candle will appear to move.
To program flicker, you could first introduce an integer instance field flameDirection. Possible values of flameDirection could be 0 (for to the left), 1 (for in the middle), and 2 (for to the right). In flicker, start by adding 1 to flameDirection (make sure that the value stays between 0 and 2. Thus if it becomes 3, reset it to 0). Then, check the value of flameDirection. If 0, draw the flame directed to the left. If 1, draw the flame directed up. If 2, draw the flame directed to the right. As for the pumpkin and the spider, before drawing the new flame, you will need to erase the old one.
Your class (that is the class for the fourth type that you created in homework 1)
Add one method with the following signature
public void doAction()
What this method does is up to you. The only requirement is that it should do something different from what was done with the other elements. For instance, don't just change the color. But, it could spin, or open and close its mouth (if it has one), etc...
Good luck!
Your program has to be your own.