Answered step by step
Verified Expert Solution
Question
1 Approved Answer
please change the output to display a car images instaed of the shapes .. FIRST FILE .. import java.awt.Shape; import java.awt.geom.Ellipse2D; import java.awt.geom.Rectangle2D; public class
please change the output to display a car images instaed of the shapes
..
FIRST FILE
..
import java.awt.Shape;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
public class Vehicle {
String brand;
int model;
String color;
int speed;
boolean on;
float posX;
float posY;
Vehicle (String inBrand, int inModel, String inColor)
{
brand = inBrand;
model = inModel;
color = inColor;
posX = 50.f;
posY = 50.f;
}
void turnOn()
{
on = true;
}
void turnOff()
{
on = false;
}
boolean isOn()
{
return on;
}
String getColor()
{
return color;
}
float moveRight()
{
posX += 30.0f;
return posX;
}
float moveLeft()
{
posX -= 30.0f;
return posX;
}
float moveUp()
{
posY -= 30.0f;
return posY;
}
float moveDown()
{
posY += 30.0f;
return posY;
}
void Accelerate()
{
speed += 5;
}
void Decelerate()
{
speed -= 5;
}
void Stop()
{
speed =0;
}
Shape getShape()
{
Shape shape;
switch (brand)
{
case "A":
shape = new Ellipse2D.Float(posX, posY, 100.0f, 100.0f);
break;
case "B":
shape = new Rectangle2D.Float(posX, posY, 100.0f, 100.0f);
break;
case "C":
shape = new Rectangle2D.Float(posX, posY, 75.0f, 50.0f);
break;
default:
shape = new Rectangle2D.Float(posX, posY, 25.0f, 25.0f);
}
/* if (brand.equals("A"))
{
shape = new Ellipse2D.Float(posX, posY, 100.0f, 100.0f);
}
else
{
shape = new Rectangle2D.Float(posX, posY, 100.0f, 100.0f);
}
*/
return shape;
}
}
...
SECOND FILE
...
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Scanner;
public class TestVehicle extends Frame {
static Vehicle listOfVehicles[];
static int nbVehicles;
public void paint(Graphics g) {
Graphics2D ga = (Graphics2D)g;
for (int i =0; i < nbVehicles; i++)
{
if (listOfVehicles[i].isOn())
{
// Set the color
switch (listOfVehicles[i].getColor())
{
case "green":
ga.setPaint(Color.green);
break;
case "red":
ga.setPaint(Color.red);
break;
case "blue":
ga.setPaint(Color.blue);
break;
default:
ga.setPaint(Color.magenta);
break;
}
// Draw the object
ga.fill(listOfVehicles[i].getShape());
}
}
}
public static void main(String[] args) {
/* Start Code for Graphics */
Frame frame = new TestVehicle();
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
}
});
frame.setSize(600, 500);
frame.setVisible(true);
/* End Code for Graphics */
Scanner scanner = new Scanner(System.in);
listOfVehicles = new Vehicle[10];
nbVehicles = 0;
// Create references t vehicles
Vehicle V1, V2, V3, V4;
// Create the vehicles themselves
V1 = new Vehicle("A", 2017, "green");
V2 = new Vehicle("B", 2013, "red");
V3 = new Vehicle("C", 2019, "blue");
V4 = new Vehicle("Toyota", 2019, "Wardi");
/* Start Code for Graphics */
listOfVehicles[0] = V1;
nbVehicles++;
listOfVehicles[1] = V2;
nbVehicles++;
listOfVehicles[2] = V3;
nbVehicles++;
listOfVehicles[3] = V4;
nbVehicles++;
frame.repaint();
/* End Code for Graphics */
scanner.nextLine();
// Turn On the first Car
V1.turnOn();
frame.repaint();
scanner.nextLine();
// Turn On the second Car
V2.turnOn();
frame.repaint(); // redraw the window
scanner.nextLine(); // Wait for user input
// Turn On the first Car
V1.moveRight();
frame.repaint(); // redraw the window
scanner.nextLine(); // Wait for user input
V1.moveRight();
frame.repaint(); // redraw the window
scanner.nextLine(); // Wait for user input
V1.moveRight();
frame.repaint(); // redraw the window
scanner.nextLine(); // Wait for user input
V2.moveDown();
frame.repaint(); // redraw the window
scanner.nextLine(); // Wait for user input
V2.moveDown();
frame.repaint(); // redraw the window
scanner.nextLine(); // Wait for user input
V2.moveDown();
frame.repaint(); // redraw the window
scanner.nextLine();
V1.moveDown();
frame.repaint(); // redraw the window
scanner.nextLine(); // Wait for user input
V1.turnOff();
frame.repaint(); // redraw the window
scanner.nextLine(); // Wait for user input
V3.turnOn();
frame.repaint(); // redraw the window
scanner.nextLine(); // Wait for user input
V3.moveUp();
frame.repaint(); // redraw the window
scanner.nextLine(); // Wait for user input
V4.turnOn();
frame.repaint(); // redraw the window
scanner.nextLine(); // Wait for user input
V4.moveLeft();
frame.repaint(); // redraw the window
scanner.nextLine(); // Wait for user input
System.out.println("END");
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started