Computer Science 252
Problem Solving with Java

Spring 2014, The College of Saint Rose

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.


SunAndMoon.java

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
 * Adapted for CSC 202/252, The College of Saint Rose
 *
 * $Id: SunAndMoon.java 2218 2013-10-18 14:06:39Z 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);
    }

}