Computer Science 120
Introduction to Programming
Fall 2011, Siena College
SunAndMoon Demo
A working demo of SunAndMoon will appear below. Click inside the applet to interact with it.
SunAndMoon BlueJ Project
Click here to download a BlueJ project for SunAndMoon.
SunAndMoon Source Code
The Java source code for SunAndMoon is below. Click on a file name to download it.
import objectdraw.*; import java.awt.*; /* * A program to animate the rising and setting of the sun and moon. * * Example from Computer Science 134, Williams College * * $Id: SunAndMoon.java 1487 2011-01-13 21:20:10Z terescoj $ */ public class SunAndMoon extends WindowController { private FilledRect field; // lower 1/3rd of window private FilledRect sky; // top 2/3rds of window private FilledOval heavenlyBody; // the sun or the moon /* * Create the sky, field and orb and set their colors */ public void begin() { sky = new FilledRect(0, 0, 400, 260, canvas); heavenlyBody = new FilledOval(100, 0, 80, 80, canvas); field = new FilledRect(0, 260, 400, 140, canvas); sky.setColor(Color.blue); field.setColor(Color.green); heavenlyBody.setColor(Color.yellow); } /* * Make it look like night by using black, white and gray */ public void onMousePress(Location point) { sky.setColor(Color.black); field.setColor(Color.gray); heavenlyBody.setColor(Color.white); } /* * Make it look like a bright sunny day */ public void onMouseRelease(Location point) { sky.setColor(Color.blue); field.setColor(Color.green); heavenlyBody.setColor(Color.yellow); } /* * Move the sun-like circle down a little bit */ public void onMouseMove(Location point) { heavenlyBody.move(0, 1.5); } /* * Move the moon-like circle up a bit */ public void onMouseDrag(Location point) { heavenlyBody.move(0, -1.5); } }