/** * Car.java * * A class that represents a simple car, to be drawn on a Canvas. * * @author Jeff Ondich, Dave Musicant, David Liben-Nowell * @version 11 April 2006 */ import java.awt.*; public class Car { // The object variables. private Integer left; // the x-coordinate of the left edge of the car. private Integer 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(Integer x) { left = x; } public void setTop(Integer 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(); Color red = new Color(255,0,0); Color black = new Color(0,0,0); // Draw the body. canvas.setInkColor(red); canvas.drawFilledRectangle(left, top + 40, 100, 50); canvas.drawFilledRectangle(left + 25, top, 50, 90); // Draw the tires. canvas.setInkColor(black); canvas.drawFilledOval(left + 10, top + 80, 20, 20); canvas.drawFilledOval(left + 70, top + 80, 20, 20); // Restore the old color. canvas.setInkColor(savedColor); } }