Purchase Solution

Java generate random vehicles on JFrame

Not what you're looking for?

Ask Custom Question

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

Purchase this Solution

Solution Summary

Java generate random vehicles on JFrame in the solution.

Purchase this Solution


Free BrainMass Quizzes
Javscript Basics

Quiz on basics of javascript programming language.

Word 2010: Table of Contents

Ever wondered where a Table of Contents in a Word document comes from? Maybe you need a refresher on the topic? This quiz will remind you of the keywords and options used when working with a T.O.C. in Word 2010.

Basic UNIX commands

Use this quiz to check your knowledge of a few common UNIX commands. The quiz covers some of the most essential UNIX commands and their basic usage. If you can pass this quiz then you are clearly on your way to becoming an effective UNIX command line user.

Java loops

This quiz checks your knowledge of for and while loops in Java. For and while loops are essential building blocks for all Java programs. Having a solid understanding of these constructs is critical for success in programming Java.

Word 2010: Tables

Have you never worked with Tables in Word 2010? Maybe it has been a while since you have used a Table in Word and you need to brush up on your skills. Several keywords and popular options are discussed as you go through this quiz.