/** * Car.java * * A class that represents a simple car, to be drawn on a Canvas. * * @author Jeff Ondich, Dave Musicant, David Liben-Nowell * @version 15 January 2006 */ import java.awt.*; public class Car { // The object variables. private int left; // the x-coordinate of the left edge of the car. private int top; // the y-coordinate of the top edge of the car. // Constructor for class Car. A constructor is a method that is // automatically run whenever a new object is created. public Car() { left = 0; top = 0; } // Methods to set the left and top variables. public void setLeft( int x ) { left = x; } public void setTop( int y ) { top = y; } // Draw this Car object on the provided canvas. public void draw( Canvas canvas ) { // Save the previous pen color so that it can be restored after // we draw the Car. Color savedColor = canvas.getInkColor(); // Draw the body. canvas.setInkColor(Color.red); canvas.drawFilledRectangle(left, top + 40, 100, 50); canvas.drawFilledRectangle(left + 25, top, 50, 90); // Draw the tires. canvas.setInkColor(Color.black); canvas.drawFilledOval(left + 10, top + 80, 20, 20); canvas.drawFilledOval(left + 70, top + 80, 20, 20); // Restore the old color. canvas.setInkColor(savedColor); } }