Computer Science 252
Problem Solving with Java
Fall 2013, The College of Saint Rose
CarsExample Demo
A working demo of CarsExample will appear below. Click inside the applet to interact with it.
CarsExample BlueJ Project
Click here to download a BlueJ project for CarsExample.
CarsExample Source Code
The Java source code for CarsExample is below. Click on a file name to download it.
/** * Write a description of class CarsExample here. Submissions * that do not replace this paragraph with your own description * that includes all required items in the class submission * guidelines are subject to penalties. * * @author (your name) */ import objectdraw.*; import java.awt.*; public class CarsExample extends WindowController { private Car newestCar = null; public void begin() { } public void onMousePress(Location point) { newestCar = new Car(point.getX(), point.getY(), Color.red, canvas); } public void onMouseExit(Location point) { if (newestCar != null) { newestCar.setColor(Color.blue); } } }
import objectdraw.*; import java.awt.*; /** * Class to define a simple car object. * * @author Jim Teresco and the CSC 252 class, Fall 2013 */ public class Car { // named constants for car dimensions private static final double WIDTH = 150; private static final double WINDOW_HEIGHT = 30; private static final double BODY_HEIGHT = 40; private static final double WHEEL_SIZE = 30; private static final double WINDOW_X_OFFSET = 25; private static final double WINDOW_WIDTH = 75; private static final double WHEEL_X_OFFSET = 20; private static final double WHEEL_Y_OFFSET = WINDOW_HEIGHT + BODY_HEIGHT - (WHEEL_SIZE/2); private static final double FRONT_WHEEL_X_OFFSET = WIDTH - WHEEL_X_OFFSET - WHEEL_SIZE; private static final Color WHEEL_COLOR = new Color(25, 25, 25); // instance variables for the components private FilledRect body; private FilledRect window; private FilledOval frontWheel; private FilledOval rearWheel; // constructor for our car object public Car(double x, double y, Color color, DrawingCanvas c) { rearWheel = new FilledOval(x+WHEEL_X_OFFSET, y+WHEEL_Y_OFFSET, WHEEL_SIZE, WHEEL_SIZE,c); rearWheel.setColor(WHEEL_COLOR); frontWheel = new FilledOval(x+FRONT_WHEEL_X_OFFSET, y+WHEEL_Y_OFFSET, WHEEL_SIZE, WHEEL_SIZE,c); frontWheel.setColor(WHEEL_COLOR); body = new FilledRect(x, y+WINDOW_HEIGHT, WIDTH, BODY_HEIGHT, c); body.setColor(color); window = new FilledRect(x+WINDOW_X_OFFSET, y, WINDOW_WIDTH, WINDOW_HEIGHT, c); window.setColor(color); } // get the car painted! public void setColor(Color c) { body.setColor(c); window.setColor(c); } }