Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have a car animation java program that makes a car move from left to right, I need help adding multiple cars to the frame

I have a car animation java program that makes a car move from left to right, I need help adding multiple cars to the frame so theres multiple cars moving at the same time. The code is below

image text in transcribed

import java.awt.*; import java.util.*; import javax.swing.*;

/** An icon that contains a moveable shape. */ public class ShapeIcon implements Icon { private int width; private int height; private MoveableShape shape; private ArrayList cars; public ShapeIcon(MoveableShape shape, int width, int height) { this.shape = shape; this.width = width; this.height = height; cars = new ArrayList(); } public int getIconWidth() { return width; }

public int getIconHeight() { return height; }

public void paintIcon(Component c, Graphics g, int x, int y) { Graphics2D g2 = (Graphics2D) g; shape.draw(g2);

} }

import java.awt.*; import java.awt.event.*; import javax.swing.*;

/** This program implements an animation that moves a car shape. */ public class AnimationTester { private static final int ICON_WIDTH = 400; private static final int ICON_HEIGHT = 100; private static final int CAR_WIDTH = 100; public static void main(String[] args) { JFrame frame = new JFrame();

final MoveableShape shape = new CarShape(0, 0, CAR_WIDTH);

ShapeIcon icon = new ShapeIcon(shape, ICON_WIDTH, ICON_HEIGHT);

final JLabel label = new JLabel(icon); frame.setLayout(new FlowLayout()); frame.add(label);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true);

final int DELAY = 10; //100 // Milliseconds between timer ticks Timer t = new Timer(DELAY, new ActionListener() { public void actionPerformed(ActionEvent event) { shape.translate(1, 0); label.repaint(); } }); t.start(); }

}

import java.awt.*; import java.awt.geom.*; import java.util.*;

/** A car that can be moved around. */ public class CarShape implements MoveableShape { private int x; private int y; private int width; /** Constructs a car item. @param x the left of the bounding rectangle @param y the top of the bounding rectangle @param width the width of the bounding rectangle */ public CarShape(int x, int y, int width) { this.x = x; this.y = y; this.width = width; }

public void translate(int dx, int dy) { // x += dx; // y += dy; if (x

public void draw(Graphics2D g2) { Rectangle2D.Double body = new Rectangle2D.Double(x, y + width / 6, width - 1, width / 6); Ellipse2D.Double frontTire = new Ellipse2D.Double(x + width / 6, y + width / 3, width / 6, width / 6); Ellipse2D.Double rearTire = new Ellipse2D.Double(x + width * 2 / 3, y + width / 3, width / 6, width / 6);

// The bottom of the front windshield Point2D.Double r1 = new Point2D.Double(x + width / 6, y + width / 6); // The front of the roof Point2D.Double r2 = new Point2D.Double(x + width / 3, y); // The rear of the roof Point2D.Double r3 = new Point2D.Double(x + width * 2 / 3, y); // The bottom of the rear windshield Point2D.Double r4 = new Point2D.Double(x + width * 5 / 6, y + width / 6); Line2D.Double frontWindshield = new Line2D.Double(r1, r2); Line2D.Double roofTop = new Line2D.Double(r2, r3); Line2D.Double rearWindshield = new Line2D.Double(r3, r4); g2.draw(body); g2.draw(frontTire); g2.draw(rearTire); g2.draw(frontWindshield); g2.draw(roofTop); g2.draw(rearWindshield); } }

1) Modify the car animation program to make the moving shape reappear on the left-hand side after it disappears from the frame. Modify the Shapelcon class so that it displays multiple moveable shapes. Then modify the animation program to show a number of moving cars. Hint: Store all shapes in an array list. 2)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Case Studies In Business Data Bases

Authors: James Bradley

1st Edition

0030141346, 978-0030141348

More Books

Students also viewed these Databases questions