Question

please change the output to display a car images instaed of the shapes .. FIRST FILE...

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");
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1
Take car class  class instead of shape in file 1
public class Car
{
   public Car(double x, double y)
   {
      xLeft = x;
      yTop = y;
   }
   public void draw(Graphics2D g2)
   {
      Rectangle2D.Double body 
         = new Rectangle2D.Double(xLeft, yTop + 10, 60, 10);      
      Ellipse2D.Double frontTire 
         = new Ellipse2D.Double(xLeft + 10, yTop + 20, 10, 10);
      Ellipse2D.Double rearTire
         = new Ellipse2D.Double(xLeft + 40, yTop + 20, 10, 10);


      Point2D.Double r1 = new Point2D.Double(xLeft + 10, yTop + 10);
   
      Point2D.Double r2 = new Point2D.Double(xLeft + 20, yTop);
     
      Point2D.Double r3 = new Point2D.Double(xLeft + 40, yTop);
      Point2D.Double r4  = new Point2D.Double(xLeft + 50, yTop + 10);

      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);      
   }

}

Do modification in file 1 in switch case as given below

Car shape;

switch (brand){ case "A": shape= new car.FLOAT(pos,pos,pos,pos)....}

Add a comment
Know the answer?
Add Answer to:
please change the output to display a car images instaed of the shapes .. FIRST FILE...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA Can you make this return the first letter of  first and last name capitalized? import java.io.File;...

    JAVA Can you make this return the first letter of  first and last name capitalized? import java.io.File; import java.io.IOException; import java.util.*; public class M1 {    public static void main(String[] args) {        //scanner input from user    Scanner scanner = new Scanner(System.in); // System.out.print("Please enter your full name: "); String fullname = scanner.nextLine(); //creating a for loop to call first and last name separately int i,k=0; String first="",last=""; for(i=0;i<fullname.length();i++) { char j=fullname.charAt(i); if(j==' ') { k=i; break; } first=first+j;...

  • When I invoke the forward or backward method the current_number isnt being changed. Not sure if...

    When I invoke the forward or backward method the current_number isnt being changed. Not sure if its something in my switch case or my methods.. package project4; import java.awt.image.BufferedImage; import java.net.URL; import java.util.Scanner; import javax.imageio.ImageIO; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class PictureViewer { final static int MIN_NUMBER = 0; final static int MAX_NUMBER = 8; static int image_number = 1;    public static int forward(int current_number) { if (current_number < MAX_NUMBER) { return current_number ++; }...

  • CONVERT THIS JAVA CODE WITH THE JAVA DOC GUIDELINES WHERE APPROPRIATE. DO NOT CHANGE THE CODE....

    CONVERT THIS JAVA CODE WITH THE JAVA DOC GUIDELINES WHERE APPROPRIATE. DO NOT CHANGE THE CODE. SAME CODE SHOULD BE USED FOR THE DOCUMENTATION PURPOSES. ONLY INSERT THE JAVA DOC. //Vehicle.java public class Vehicle {    private String manufacturer;    private int seat;    private String drivetrain;    private float enginesize;    private float weight; //create getters for the attributes    public String getmanufacturer() {        return manufacturer;    }    public String getdrivetrain() {        return drivetrain;...

  • Help! Not sure how to create this java program to run efficiently. Current code and assignment...

    Help! Not sure how to create this java program to run efficiently. Current code and assignment attached. Assignment :Write a class Movies.java that reads in a movie list file (movies.txt). The file must contain the following fields: name, genre, and time. Provide the user with the options to sort on each field. Allow the user to continue to sort the list until they enter the exit option. ---------------------------------------------------------- import java.util.*; import java.io.*; public class Movies { String movieTitle; String movieGenre;...

  • ***This is a JAVA question*** ------------------------------------------------------------------------------------------------ import java.awt.*; import java.awt.event.*; import java.awt.image.*; import javax.imageio.*; import javax.swing.*;...

    ***This is a JAVA question*** ------------------------------------------------------------------------------------------------ import java.awt.*; import java.awt.event.*; import java.awt.image.*; import javax.imageio.*; import javax.swing.*; import javax.swing.event.*; public class DrawingPanel implements ActionListener { public static final int DELAY = 50; // delay between repaints in millis private static final String DUMP_IMAGE_PROPERTY_NAME = "drawingpanel.save"; private static String TARGET_IMAGE_FILE_NAME = null; private static final boolean PRETTY = true; // true to anti-alias private static boolean DUMP_IMAGE = true; // true to write DrawingPanel to file private int width, height; // dimensions...

  • Java Program The goal of this assignment is to develop an event-based game. You are going...

    Java Program The goal of this assignment is to develop an event-based game. You are going to develop a Pong Game. The game board is a rectangle with a ball bouncing around. On the right wall, you will have a rectangular paddle (1/3 of the wall height). The paddle moves with up and down arrows. Next to the game board, there will be a score board. It will show the count of goals, and count of hits to the paddle....

  • Could someone re-write this code so that it first prompts the user to choose an option...

    Could someone re-write this code so that it first prompts the user to choose an option from the calculator (and catches if they enter a string), then prompts user to enter the values, and then shows the answer. Also, could the method for the division be rewritten to catch if the denominator is zero. I have the bulk of the code. I am just having trouble rearranging things. ------ import java.util.*; abstract class CalculatorNumVals { int num1,num2; CalculatorNumVals(int value1,int value2)...

  • please help me to creating UML class diagrams. Snake.java package graphichal2; import java.awt.Color; import java.awt.Font; import...

    please help me to creating UML class diagrams. Snake.java package graphichal2; import java.awt.Color; import java.awt.Font; import java.awt.Frame; import java.awt.Graphics; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.util.Random; enum Dir{L, U, R, D}; class Food { public static int FOODSTYLE = 1; private int m = r.nextInt(Yard.WIDTH / Yard.B_WIDTH); private int n = r.nextInt(Yard.HEIGHT / Yard.B_WIDTH - 30/Yard.B_WIDTH) + 30/Yard.B_WIDTH;// 竖格 public static Random r = new Random(); public int getM() { return m; } public void setM(int m)...

  • could you please help me with this problem, also I need a little text so I...

    could you please help me with this problem, also I need a little text so I can understand how you solved the problem? import java.io.File; import java.util.Scanner; /** * This program lists the files in a directory specified by * the user. The user is asked to type in a directory name. * If the name entered by the user is not a directory, a * message is printed and the program ends. */ public class DirectoryList { public static...

  • This is my current output for my program. I am trying to get the output to...

    This is my current output for my program. I am trying to get the output to look like This is my program Student.java import java.awt.GridLayout; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import javax.swing.JFileChooser; public class Student extends javax.swing.JFrame {     BufferedWriter outWriter;     StudentA s[];     public Student() {         StudentGUI();            }     private void StudentGUI() {         jScrollPane3 = new javax.swing.JScrollPane();         inputFileChooser = new javax.swing.JButton();         outputFileChooser = new javax.swing.JButton();         sortFirtsName = new...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT