- import objectdraw.*;
- import java.awt.*;
- // A program that produces an animation of the sun rising and setting.
- // The animation is driven by dragging the mouse.
- public class ScrollingSun extends WindowController {
- private FilledOval sun; // Circle that represents the sun
- private Text instructions; // Display of instructions
- // Place the sun and some brief instructions on the screen
- public void begin() {
- sun = new FilledOval( canvas.getWidth()/4,
- canvas.getHeight() - canvas.getWidth()/4,
- canvas.getWidth()/2,
- canvas.getWidth()/2, canvas);
- instructions = new Text("Drag the mouse up or down",
- 0, 0, canvas);
- instructions.moveTo((canvas.getWidth()-instructions.getWidth() )/2, 20);
- }
- // Move the sun to follow the mouse's vertical motion
- public void onMouseDrag( Location mousePosition ) {
- sun.moveTo(canvas.getWidth()/4, mousePosition.getY());
- instructions.hide();
- }
- // Move the sun back to its starting position and redisplay
- // the instructions
- public void onMouseExit( Location point ) {
- sun.moveTo(canvas.getWidth()/4, canvas.getHeight() - canvas.getWidth()/4);
- instructions.show();
- }
- }
Raw Paste