Homework 2 

(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, and have the cars move back and forth, the apples change colors and the snow on the mountains melts. That would really add some zest to your program!

Overview

In this assignment, you will reuse and modify the four classes that you wrote in homework 1, namely AppleTree, Car, Mountain, and the fourth one that you created. I give you a new version of CountrysideScene that contains the code to do the animation (You don't have to understand the animation part to do this assignment. The code will become clearer when we learn about inheritance at the end of 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 Car class to move the car back and forth in the graphics window.

 

The details

Download the new version of CountrysideScene.java. Create an Eclipse project that contains CountrysideScene and the four element classes that you created in homework 1.

Start by modifying the code within CountrysideScene. 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.

That's it for CountrysideScene. Of course, you can also modify the coordinates within the constructor calls if you don't like the positioning of some of the elements in the graphics window.

What you have left to do is to add methods to the other four classes (these methods are called within CountrysideScene to perform the animation). Here is your contract for each class:

 

AppleTree class

Add one method with the following signature

public void ripen()

This method should change the colors of the apples on the tree. If the apples are green their color will change to yellow. If the apples are yellow, their color will change to red. And if the apples are red, their color will change to green. To do so, use the setColor method on the shapes that you use to draw the apples. In other words, the color of the apples will cycle through green, yellow and red while the animation runs.

Car class

A car may move to the left or to the right. To keep track of its direction of motion, create two constants LEFT_MOVING and RIGHT_MOVING of type int. Use the syntax public static final int ... when declaring the constants. Add also an instance field direction of type int whose value is set in the constructor to LEFT_MOVING or RIGHT_MOVING.

Modify the constructor so that it now reads

public Car(int x, int y, double scale, int direction, GWindow window)


In other words, add a parameter (in position 4) to specify the direction of motion of the car. The parameter direction can have any of the 2 values, LEFT_MOVING and RIGHT_MOVING which are the public constants mentioned above. If direction is equal to LEFT_MOVING, the car should be drawn facing left and if direction is equal to RIGHT_MOVING, the car should be drawn facing right. Initialize the instance field direction with the value of the direction parameter, i.e. write this.direction = direction;.

 

Add one method with the following signature

public void move()

This method should move the car forward by a certain distance (the exact value is up to you but the displacement should big enough so that the car travels back and forth during the animation). When the car reaches the right edge of the window, the car will turn around and start moving to the left. If the car reaches the left side of the window, the car will turn around and start moving to the right.

A few hints: Whenever the car is moving (i.e. whenever move is called), check if the car is past the right edge or the left edge of the window and modify its this.direction accordingly (i.e. set this.direction to MOVING_RIGHT or MOVING_LEFT). To know if the car is past the window's right or left edge, compare the this.x coordinate of the car to 0 for the left edge and to this.window.getWindowWidth() for the right edge. Then, move the car by changing its this.x coordinate. If this.direction is equal to MOVING_LEFT, subtract the distance moved in one step from this.x. If this.direction is equal to MOVING_RIGHT, add the distance to this.x.

Changing this.x doesn't move the car on the screen. What is left to do is to move the graphics elements making up the car. One approach is to erase the graphics elements making up the car (use this.window.remove...). Then, redraw the car at the new position (with the right orientation) by calling the draw method.

You can also use the moveBy method of the UW library on each one of the graphics elements (rectangles...) of your car. However, this approach is more complicated when the car turns around.

In any case, make sure that you have access to the graphics elements within your car method. You will need to make these graphics elements instance fields, since most likely, you defined them as local variables within draw() for homework 1.

 

Mountain class

Add one method with the following signature

public void meltSnow()

meltSnow should decrease the size of the snow on top of the mountain. For instance, if the snow is represented by a white triangle, erase that triangle and replace it with a smaller triangle. At some point, all of the snow will have melted (e.g. when the size of the white triangle becomes 0). When that is the case, redraw the snow with its original size and restart the melting process. Make sure that all of the snow melts at least two times within the full animation.

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