JAVA   49
ScrollingSun
Guest on 9th February 2023 03:07:07 AM


  1. import objectdraw.*;
  2. import java.awt.*;
  3.  
  4.     // A program that produces an animation of the sun rising and setting.
  5.     // The animation is driven by dragging the mouse.
  6. public class ScrollingSun extends WindowController {
  7.  
  8.     private FilledOval sun;     // Circle that represents the sun
  9.     private Text instructions;  // Display of instructions
  10.  
  11.         // Place the sun and some brief instructions on the screen
  12.     public void begin() {
  13.         sun = new FilledOval( canvas.getWidth()/4,
  14.                               canvas.getHeight() - canvas.getWidth()/4,
  15.                               canvas.getWidth()/2,
  16.                               canvas.getWidth()/2, canvas);
  17.  
  18.         instructions = new Text("Drag the mouse up or down",
  19.                                                 0, 0, canvas);
  20.         instructions.moveTo((canvas.getWidth()-instructions.getWidth() )/2, 20);
  21.     }
  22.  
  23.         // Move the sun to follow the mouse's vertical motion
  24.     public void onMouseDrag( Location mousePosition ) {
  25.         sun.moveTo(canvas.getWidth()/4, mousePosition.getY());
  26.         instructions.hide();
  27.     }
  28.    
  29.         // Move the sun back to its starting position and redisplay
  30.         // the instructions
  31.     public void onMouseExit( Location point ) {
  32.         sun.moveTo(canvas.getWidth()/4, canvas.getHeight() - canvas.getWidth()/4);
  33.         instructions.show();
  34.     }
  35.  
  36. }

Raw Paste

Login or Register to edit or fork this paste. It's free.