Computer Science 202
Introduction to Programming

Fall 2013, The College of Saint Rose

ColorEvents Demo

A working demo of ColorEvents will appear below. Click inside the applet to interact with it.



ColorEvents BlueJ Project

Click here to download a BlueJ project for ColorEvents.


ColorEvents Source Code

The Java source code for ColorEvents is below. Click on a file name to download it.


ColorEvents.java

import objectdraw.*;
import java.awt.*;

/*
 * An example demonstrating the use of more event handling methods
 * and setting the color of a graphical object.
 *
 * Jim Teresco, CSIS 120, Siena College, Spring 2011
 * CSC 202, The College of Saint Rose, Fall 2013
 *
 * $Id: ColorEvents.java 1487 2011-01-13 21:20:10Z terescoj $
 */

public class ColorEvents extends WindowController {

    /* Define some names (instance variables) to remember
       the objects we're drawing so we can change their colors
       later
    */
    private FilledOval oval;
    private FramedRect rect;
    private Line line;

    /* Draw a few objects on the canvas to play with later */
    public void begin() {

	/* assignment statements: note how we construct the object on
	   the right hand side of the assignment and put the name
	   where we wish to remember the object on the left. */
	oval = new FilledOval(50, 50, 100, 200, canvas);
	rect = new FramedRect(200, 10, 50, 100, canvas);
	line = new Line(20, 300, 300, 20, canvas);
    }

    /* This method will execute when someone clicks the mouse in the window.  
    */
    public void onMouseClick(Location point) {

	oval.setColor(Color.red);
    }

    /* This method will execute when the mouse is moved in the window.
    */
    public void onMouseMove(Location point) {

	rect.setColor(Color.blue);
    }

    /* This method will execute when someone drags the mouse in the window.  
    */
    public void onMouseDrag(Location point) {

	line.setColor(Color.green);
    }

    /* This method will execute when the mouse enters the window.
    */
    public void onMouseEnter(Location point) {

	oval.setColor(Color.black);
	rect.setColor(Color.black);
	line.setColor(Color.black);
    }

    /* This method will execute when the mouse exits the window.
    */
    public void onMouseExit(Location point) {

	oval.setColor(Color.white);
	rect.setColor(Color.white);
	line.setColor(Color.white);
    }
    
}