i. Figure 5.15 is the DropCollector class. Compare the DropCollector2 class (linked to below) to the DropCollector class of page 156 of Williams textbook. ii. Figure 5.16 on page 157 is the FallingBall class. Compare to the FallingBall2 class shown below and linked to below.
/* See pages 156-157 of the textbook (William's College manuscript) 10/09/2002 handout
Wednesday's class
Figure 5.15: Adding a collector to collect raindrops or falling balls.
How is DropCollector2 different from the book's DropCollector class?
How are the differences VERY HELPFUL for your Boxball assignment?
------------
How is THIS very helpful? Do you see "this" in the code below?
----
Figure 5.16: Making a falling ball fill a collector
How is FallingBall2 different from the book's FallingBall class?
How are the differences VERY HELPFUL and how do they apply to your
Boxball assignment?
*/
import objectdraw.*;
import java.awt.*;
import FallingBall2;
public class DropCollector2 extends WindowController
{
private static final int COLLECTOR_TOP = 295; // Dry season before raindrops
private static final int SCREEN_WIDTH = 300;
private static final int SCREEN_HEIGHT = 300;
private static final int CAPACITY_LEVEL = 50;
private static final int DROP_EFFECT = 12; // How much it raises the water level
private FilledRect collector; // The H O or water (lake)
// 2
private int fontSize;
private Text message0, message;
private FilledRect capacityLevelLine;
public void begin()
{
fontSize = 12;
message0 = new Text("Start Sandbagging!", 10, 2, canvas);
message0.hide();
message = new Text("Click repeatedly to make falling raindrops...", 10, 16, canvas);
collector = new FilledRect(0, COLLECTOR_TOP, SCREEN_WIDTH, SCREEN_HEIGHT, canvas);
collector.setColor( Color.blue );
capacityLevelLine = new FilledRect(0, CAPACITY_LEVEL, SCREEN_WIDTH, 1, canvas);
capacityLevelLine.setColor( Color.red );
}
public void onMouseRelease(Location point) // Java's onMouseClick() has problems! :-)
{
new FallingBall2(point, canvas, this);
}
public void onRaindropLanding( FallingBall2 drop )
{
if (collector.getY() > CAPACITY_LEVEL)
{
collector.setHeight( collector.getHeight() + DROP_EFFECT );
collector.move(0, -DROP_EFFECT);
}
else
{
message0.show();
message.setText("H 2 O overflow! Water capacity reached!");
message.setFontSize( ++fontSize );
}
}
public double waterLevel()
{
return collector.getY();
}
public boolean fullOfWater()
{
return collector.getY() <= capacityLevelLine.getY();
}
}
// To be discussed on Wednesday, October 9th in class.
// Compare to Figure 5.16: Making a falling ball fill a collector.
// See page 157 of the Williams textbook (Chapter 5).
/* Notice how "this" Java keyword is used below.
----
1. Why was it important to pass the ball back as a parameter to the
onRainDropLanding() method?
2. Was it a mistake for the onRainDropLanding() method of the
DropCollector2 class to require a FallingBall2 object?
Hint: Is it possible for raindrops or balls to be different sizes?
Would a larger raindrop add more water to the collector
than a smaller raindrop?
*/
import java.awt.*;
import objectdraw.*;
import DropCollector2;
public class FallingBall2 extends ActiveObject
{
private static final int BALLSIZE = 30;
private DropCollector2 whereItIsRaining;
private FilledOval ball;
public FallingBall2(Location ballLocation, DrawingCanvas canvas,
DropCollector2 theCollector)
{
whereItIsRaining = theCollector;
ball = new FilledOval( ballLocation, BALLSIZE, BALLSIZE, canvas );
ball.setColor( Color.magenta );
start();
}
public void run()
{
while (( ball.getY() < whereItIsRaining.waterLevel() ) &&
!whereItIsRaining.fullOfWater() )
{
ball.move(0, 5);
pause(50);
}
whereItIsRaining.onRaindropLanding( this );
ball.hide();
}
}