// CSE 142, Winter 2007 // Author: Marty Stepp // // This program animates a car driving across the panel. import java.awt.*; public class DrawMovingCar_SectionA { public static void main(String[] args) { DrawingPanel panel = new DrawingPanel(300, 200); panel.setBackground(Color.LIGHT_GRAY); Graphics g = panel.getGraphics(); for (int i = 0; i < 50; i++) { drawCar(g, i * 10, 10, 50); drawCar(g, i * 9, 100, 50); // pause the screen, to produce animation panel.sleep(100); // clear the window before redrawing the car g.setColor(Color.LIGHT_GRAY); g.fillRect(0, 0, 300, 200); } } // Draws a car figure at the given position and size. public static void drawCar(Graphics g, int x, int y, int size) { g.setColor(Color.BLACK); g.fillRect(x, y, size, size / 2); // wheels g.setColor(Color.RED); g.fillOval(x + size / 10, y + size * 4 / 10, size / 5, size / 5); g.fillOval(x + size * 7 / 10, y + size * 4 / 10, size / 5, size / 5); // windshield g.setColor(Color.CYAN); g.fillRect(x + size * 7 / 10, y + size / 10, size * 3 / 10, size / 5); } }