Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Computer Graphics Expert

Implement an abstract class Vehicle and concrete subclasses Car and Truck. A vehicle has a position on the screen. Write methods draw that draw cars and trucks as follows:

Then write a method randomVehicle that randomly generates Vehicle references, with an equal probability for constructing cars and trucks, with random positions. Call it 10 times and draw all of them.

Use the following class as your main class:
import javax.swing.JComponent;
import javax.swing.JFrame;

/**
This program draws cars and trucks in random order.
*/
public class RandomVehicleViewer
{
public static void main(String args[])
{
JFrame frame = new JFrame();
final int FRAME_WIDTH = 600;
final int FRAME_HEIGHT = 600;

frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JComponent component = new RandomVehicleComponent();
frame.add(component);
frame.setVisible(true);
}
}

You need to supply the following classes in your solution:
RandomVehicleComponent
Car
Truck

Use the following class in your solution:
import java.awt.Graphics2D;

/**
This class represents a vehicle.
*/
public abstract class Vehicle
{
private int xleft;
private int ytop;

/**
Construct a Vehicle object.
*/
public Vehicle()
{
xleft = 0;
ytop = 0;
}

/**
Draw the specified vehicle.
@param g2 the graphics context
*/
public abstract void draw(Graphics2D g2);

/**
Set the location of the vehicle.
@param x the x coordinate
@param y the y coordinate
*/
public void setLocation(int x, int y)
{
xleft = x;
ytop = y;
}

/**
Returns the x coordinate of the left-top corner of the vehicle
@return the x coordinate
*/
public int getX()
{
return xleft;
}

/**
Returns the y coordinate of the left-top corner of the vehicle
@return the y coordinate
*/
public int getY()
{
return ytop;
}

public abstract int getHeight();
public abstract int getWidth();
}

import java.awt.Graphics2D;

/**
This class represents a vehicle.
*/
public abstract class Vehicle
{
private int xleft;
private int ytop;

/**
Construct a Vehicle object.
*/
public Vehicle()
{
xleft = 0;
ytop = 0;
}

/**
Draw the specified vehicle.
@param g2 the graphics context
*/
public abstract void draw(Graphics2D g2);

/**
Set the location of the vehicle.
@param x the x coordinate
@param y the y coordinate
*/
public void setLocation(int x, int y)
{
xleft = x;
ytop = y;
}

/**
Returns the x coordinate of the left-top corner of the vehicle
@return the x coordinate
*/
public int getX()
{
return xleft;
}

/**
Returns the y coordinate of the left-top corner of the vehicle
@return the y coordinate
*/
public int getY()
{
return ytop;
}

public abstract int getHeight();
public abstract int getWidth();
}

import javax.swing.JComponent;
import javax.swing.JFrame;

/**
This program draws cars and trucks in random order.
*/
public class RandomVehicleViewer
{
public static void main(String args[])
{
JFrame frame = new JFrame();
final int FRAME_WIDTH = 600;
final int FRAME_HEIGHT = 600;

frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JComponent component = new RandomVehicleComponent();
frame.add(component);
frame.setVisible(true);
}
}

Computer Graphics, Computer Science

  • Category:- Computer Graphics
  • Reference No.:- M91621689
  • Price:- $30

Priced at Now at $30, Verified Solution

Have any Question?


Related Questions in Computer Graphics

Please read my question if you post copy and pasted code

Please read my question if you post copy and pasted code you will get a negative review. This is java In this graphical program, you are going to simulate arcade car racing game. You need a single player that is movable ...

You have a program draw a rectangle on the screen and allow

You have a program draw a rectangle on the screen and allow the user to change the color/size of the rectangle. The program will store in the browser the settings of the color/size so that upon revisiting the page, the v ...

The problemyou are to search a bitmap in rgb format and

The problem: You are to search a bitmap in RGB format and count the number of pixels which match a set of colors. The bitmap will be 1024 by 1024 pixels for the purposes of explaining this problem. The set of colors will ...

The saffir-simpson hurricane scale classifies hurricanes

The Saffir-Simpson Hurricane Scale classifies hurricanes into five categories numbered 1 through 5. Write an application named Hurricane that outputs a hurricane's category based on the user's input of the wind speed. CA ...

Question a describe the architecture of simple raster

Question : a) Describe the architecture of simple raster graphics system? b) Explain raster scan display processor?

The problemsit is strongly recommended that you tackle the

The problems: It is strongly recommended that you tackle the following tasks in a modular way -- a separate standalone function for each task. Draw a rectangle as a Rectangle and as a Polygon. Make the lines of the Polyg ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As