// Zorah Fung, CSE 142 // Draws cars of different sizes and positions // NOTE: // * First, we added x and y parameters and had to "shift" components of // the car to relate to the x and y. Since adding x and y involved // translation, we use addition and subtraction. // * Second, we added a size parameter and scaled the individual components // of the car. For scaling, we use multiplication and division. // * Lastly, we had to scale the shift factors from the first step. import java.awt.*; public class Car { public static void main(String[] args) { DrawingPanel panel = new DrawingPanel(400, 100); panel.setBackground(Color.LIGHT_GRAY); Graphics brush = panel.getGraphics(); drawCar(brush, 10, 30, 100); drawCar(brush, 150, 10, 50); drawCar(brush, 280, 20, 80); } // Draws a black car with its top left corner at the given x and y value // and a width of the given size public static void drawCar(Graphics g, int x, int y, int size) { // car body g.setColor(Color.BLACK); g.fillRect(x, y, size, size / 2); // wheels g.setColor(Color.RED); g.fillOval(x + size / 10, y + 2 * size / 5, size / 5, size / 5); g.fillOval(x + (7 * size / 10), y + 2 * size / 5, size / 5, size / 5); // window g.setColor(Color.CYAN); g.fillRect(x + 7 * size / 10, y + size / 10, 3 * size / 10, size / 5); } }