JAVA   53
NightSky
Guest on 9th February 2023 03:08:35 AM


  1. import objectdraw.*;
  2. import java.awt.*;
  3.  
  4. // A program that draws the sky at night.
  5. // Clicking causes the moon and the cloud to swap positions.
  6. public class NightSky extends WindowController{
  7.    
  8.     private FilledOval moon, cloud;
  9.     private FilledRect sky;
  10.    
  11.     // draws the cloud over top of the moon
  12.     public void begin(){
  13.         sky = new FilledRect( 0, 0, 300, 300, canvas );
  14.         moon = new FilledOval( 50, 50, 100, 100, canvas );
  15.         cloud = new FilledOval( 70, 110, 160, 40, canvas );
  16.        
  17.         sky.setColor( new Color(60,60,60));
  18.         moon.setColor(Color.white);
  19.         cloud.setColor(Color.gray);
  20.     }
  21.    
  22.     // draws a new scene with the moon over top of the cloud
  23.     public void onMousePress( Location point ){
  24.         canvas.clear();
  25.         sky = new FilledRect( 0, 0, 300, 300, canvas );
  26.         cloud = new FilledOval( 70, 110, 160, 40, canvas );
  27.         moon = new FilledOval( 50, 50, 100, 100, canvas );
  28.        
  29.         sky.setColor( new Color(60,60,60));
  30.         moon.setColor(Color.white);
  31.         cloud.setColor(Color.gray);
  32.     }
  33.    
  34.     // draws the original scene described in the begin method
  35.     public void onMouseRelease( Location point ){
  36.         canvas.clear();
  37.         begin();
  38.     }
  39.    
  40. }

Raw Paste

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