Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

i want to change the display to read images from a file in my computer which is a png picture of a car and make

i want to change the display to read images from a file in my computer which is a png picture of a car
and make it move just like the shapes
in the orignal file.
so the output should display a png picture of a care that moves by position .
---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");
}
I need a new method or even a class that load and read images instead of the get shape method
so basically if you run the application it will display an image of a car instead of The rectangle shapes, and you can any method you prefer sich as g.drawimage
i don't anything beside that information
just do it as you think and provide it to me
i need it asap

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions