// Helene Martin, CSE 142 // Displays a car drawn in different positions and // at different scales. // Comments in parentheses are notes to students. import java.awt.*; // (to use Graphics, Color) public class Car { public static void main(String[] args) { DrawingPanel panel = new DrawingPanel(600, 200); panel.setBackground(Color.LIGHT_GRAY); Graphics g = panel.getGraphics(); drawCar(g, 10, 30, 100); drawCar(g, 150, 10, 50); for(int i = 0; i < 10; i++) { drawCar(g, 10 + 40 * i, 100, 30); } } // Draws a black car with red wheels and a blue windshield // (ALWAYS start with a figure without parameters!) public static void drawCarInitial(Graphics g) { // (When multiple shapes are drawn in sequence, the last // ones drawn are visible) g.setColor(Color.BLACK); g.fillRect(10, 30, 100, 50); g.setColor(Color.RED); g.fillOval(20, 70, 20, 20); g.fillOval(80, 70, 20, 20); g.setColor(Color.CYAN); g.fillRect(80, 40, 30, 20); } // Draws a black car with red wheels and a blue windshield // (x, y) is the coordinate of the top left corner of the black body // (Translation is easier than scaling - start with that) public static void drawCarTranslate(Graphics g, int x, int y) { g.setColor(Color.BLACK); g.fillRect(x, y, 100, 50); // (translation affects x and y coordinates only // and always involves + or -) g.setColor(Color.RED); g.fillOval(x + 10, y + 40, 20, 20); // (the first wheel is 10 pixels in from left edge of the body) g.fillOval(x + 70, y + 40, 20, 20); g.setColor(Color.CYAN); g.fillRect(x + 70, y + 10, 30, 20); } // Draws a black car with red wheels and a blue windshield // (x, y) is the coordinate of the top left corner of the black body // size is the width of the black body // (scaling is hard and affects the translation factor -- do it last) public static void drawCar(Graphics g, int x, int y, int size) { g.setColor(Color.BLACK); g.fillRect(x, y, size, size / 2); // (scaling always involves * or /) g.setColor(Color.RED); // scaling affects width and height AND translation 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); g.setColor(Color.CYAN); g.fillRect(x + size * 7 / 10, y + size / 10, size * 3 / 10, size / 5); } }