// Zorah Fung, CSE 142 // Draws cars of different sizes and positions // We want to create a parameterize method that can draw a car at any location at any size // given the specific code to draw a single instance of a car at a particular location // and size. In order to write a parameterized method, we followed these steps. // 1. Write the code to produce a car in main (this code was given to us) // 2. Create a method that draws this car. Remember that graphics should be a parameter! // 3. In order to draw a car at any location, we need to add parameters for the x and y // coordinates. We used the x and y to represent the top/left hand corner of the car // so we must determine the relationship between the location of each component // (e.g. the window) and the top left/hand corner. Since we're translating the car, // we use addition and subtraction. // 4. We then added a size parameter and scaled the individual components of the car, // using the values from the original car for reference. For scaling, we use // multiplication. // 5. Lastly, we must scale relationship between components (i.e. shift factors) from // step 3. import java.awt.*; public class CarAnnotated { public static void main(String[] args) { DrawingPanel panel = new DrawingPanel(500, 200); panel.setBackground(Color.LIGHT_GRAY); Graphics g = panel.getGraphics(); drawCar(g, 10, 30, 100); drawCar(g, 150, 10, 100); drawCar(g, 270, 140, 50); drawCar(g, 380, 80, 80); /* // Animates a car that drives across the screen for (int i = 0; i < 75; i++) { drawCar(g, 10 + 10 * i, 30, 50); panel.sleep(30); // Adds a pause before the next thing is drawn on the panel panel.clear(); // clears the graphics on the DrawingPanel } */ } /* // STEP 1 and STEP 2 // Original code which produces a single car at x = 10, y = 30, and width = 100 public static void drawCar(Graphics g) { // car body g.setColor(Color.BLACK); g.fillRect(10, 30, 100, 50); // wheels g.setColor(Color.RED); g.fillOval(20, 70, 20, 20); g.fillOval(80, 70, 20, 20); // window g.setColor(Color.CYAN); g.fillRect(80, 40, 30, 20); } */ /* // STEP 3 // Original Car had its top left hand corner at x = 10, y = 30 // Here we determine the relationship between the x, y values of each component // and the x and y value of the top/left hand corner. We only modify the x and y // parameters in the drawing methods. The size stays the same. public static void drawCar(Graphics g, int x, int y) { // car body g.setColor(Color.BLACK); g.fillRect(x, y, 100, 50); // wheels g.setColor(Color.RED); // The left wheel was at x = 20 in the original car, which is 10 away from // the x value of the top/left hand corner. The right wheel was at 80, which // is 70 away g.fillOval(x + 10, y + 40, 20, 20); g.fillOval(x + 70, y + 40, 20, 20); // window g.setColor(Color.CYAN); g.fillRect(x + 70, y + 10, 30, 20); } */ // STEP 4 and 5 // Original car has a width of 100, which we will call the "size" // In step 4, we scale the individual components (i.e. the width and height of the shapes) // Then in step 5, we need to scale the shift values we calculated in step 3 // Note: I assume that the given size is a multiple of 10 public static void drawCar(Graphics g, int x, int y, int size) { // car body g.setColor(Color.BLACK); // Original height was 50, which is half of the width (or size) g.fillRect(x, y, size, size / 2); // wheels g.setColor(Color.RED); // Our shift value for the left wheel was 10, which is 1/10th of the width, or size 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); // window g.setColor(Color.CYAN); g.fillRect(x + size * 7 / 10, y + size / 10, size * 3 / 10, size / 5); } }