Question

In: Computer Science

(Java program) Write an applet that draws two animated cars that must race each other. The...

(Java program) Write an applet that draws two animated cars that must race each other. The race cars should move along a straight line, with each starting behind a Start Line. The user should have the option to set the speed of each car. The speed range is 0 to 100. There should be a Start button to begin the race and an End button to stop the race at any time.

Solutions

Expert Solution

import java.awt.*;

import javax.swing.*;

import java.awt.image.*;

import java.net.url;

import java.awt.event.KeyEvent;

import java.awt.event.KeyListener;

import java.awt.event.ActionEventListener;

import javax.swing.JButton;

public class CarRace extends JPanel

{

int cross_a, cross_b;

int car_a, car_b;

int speed_a, speed_b;

int nOpp;

String imageLoc[];

int la[], lb[];

int score;

int max_score;

int speed_Opp;

boolean isDone;

boolean isUP, isDOWN, isLEFT, isRIGHT;

public CarRace()

{

cross_a = cross_b = -999;

addKeyListener(new KeyListener(){

public void keyTyped(keyEvent e)

}

public void keyReleased(keyEvent e){

car_stop(e);

public void keyPressed(keyEvent e){

car_move(e);

}});

jButton play = new JButton("play");

play.ActionEventListener(new ActionListener()

{

public void ActionPerformed(ActionEvent e)

{

CarRace play = new CarRace();

}

});

car_a = car_b = 0;

speed_a = speed_b = 0;

nOpp = 0;

la = new int[20];

lb = new int[20];

imageLoc = new String[20];

speed_Opp = new int[20];

score = max_score = 0;

}

public void paint(Graphics g){

super.paint(g);

Graphics2D obj = (Graphics2D) g;

obj.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

try{

obj.drawImage(getToolkit().getImage("images/straight_road.jpg"), 0, 0, this);

if (cross_a >= -499 && cross_b >= -499)

obj.drawImage(getToolkit().getImage("images/cross_road.jpg"), cross_x, cross_y, this);

obj.drawImage(getToolkit().getImage("images/get_car.jpg"), car_a, car_b, this);

}

catch(Exception e){

System.out.println(e);

}

void Road_move(int count){

if(cross_a == -999 && cross_b == -999)

if(count % 10 == 0)

cross_a = 499;

cross_b = 0;

}}

else

cross_a--;

if(cross_a = -499 && cross_b = 0)

cross_a = cross_b = -999;

car_a += speed_a;

car_b += speed_b;

if (car_a < 0)

car_a = 0;

int index[] = new int[nOpp]

for(int i = 0; i < nOpp; i++){

if(la[i] >= 127)

index[i] == 1

}

int c = 0;

for(int i = 0; i < nOpp; i++){

if(index[i] == 1){

imageLoc[c] = imageLoc[i];

la[c] = la[i];

lb[c] = lb[i];

speed_Opp[c] = speed_Opp[i];

c++;

}

}

score += nOpp - c;

if(score > max_score)

max_score = score;

nOpp = c;

void Finish()

{

String str = "";

this.repaint();

if(score == max_score && score != 0)

str = "\n Congratulations...!!!You won";

JOptionPane.showMessageDialog(this, "Game Over!!\n Your score" :+score+"\nMax score" :+max_score+str)

System.exit(ABORT);

public void car_move(KeyEvent e){

if(e.getKeyCode() == KeyEvent.VK_UP){

isUP = true;

speed_a = 1;

if(e.getKeyCode() == KeyEvent.VK_DOWN){

isDOWN = true;

speed_a = -2;

if(e.getKeyCode() == KeyEvent.VK_RIGHT){

isRIGHT = true;

speed_b = 1;

if(e.getKeyCode() == KeyEvent.VK_LEFT){

isLEFT = true;

speed_b = -1;

}

}

public void car_stop(keyEvent e){

if(e.getKeyCode() == KeyEvent.VK_UP){

isUP = false;

speed_a = 0;

if(e.getKeyCode() == KeyEvent.VK_DOWN){

isDOWN = false;

speed_a = 0;

if(e.getKeyCode() == KeyEvent.VK_RIGHT){

isRIGHT = false;

speed_b = 0;

if(e.getKeyCode() == KeyEvent.VK_LEFT){

isLEFT = false;

speed_b = 0;

public static void main(String arge[]){

JFrame Frame = new JFrame("Car Racing Game");

CarRace game = new CarRace();

frame.add(game);

frame.setSize(500, 500);

frame.setVisible(true);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

int count = 1, c = 1;

while(true){

game.Road_move(count){

while (c <= 1){

game.repaint();

}

c++;

}

c = 1;

count++;

}

}


Related Solutions

in a gui ' in java write a program that draws equal a simple fence with...
in a gui ' in java write a program that draws equal a simple fence with vertical, spaced slats backed by two boards. Behind the fence show a simple house support Make sure the in the und. house is visible between the slats in the fence.
In java processing 3, write a program that draws a circle. The center of the circle...
In java processing 3, write a program that draws a circle. The center of the circle would be first point on the canvas where the mouse is pressed on it. While the mouse is pressed, the circle can be drawn by moving the mouse cursor farther than the first point that mouse was pressed (circle center). As soon as the code is run, a timer at the bottom of the canvas should start working. Also, the radius of the circle...
Needs to be in JAVA. Write a Java program that accepts the total amount of cars...
Needs to be in JAVA. Write a Java program that accepts the total amount of cars sold and total sales amount of a car salesperson for a given month. The salesperson’s paycheck is computed as follows: a. Every sales person gets 10% (commission) of total sales b. Sales totals greater than $50,000 get 5% of total sales amount c. 8 or more cars sold earns the salesperson an extra 3% Please remove 30% (taxes) of the gross pay and list...
In JAVA A. A Java applet is a stand-alone program that does not require a web...
In JAVA A. A Java applet is a stand-alone program that does not require a web browser. True or False B.Which of the following are valid identifiers in java? (will the identifier compile?)   Object#Count   $money   Late_Fee   __$__ C.What is the decimal (base 10) equivalent of 162 in octal? D.Which of these identifiers obey the naming convention for object names? Isthisokay goodName C3PO ThisIsReallyOkay areallygreatname anotherBadOne
This program must be done in JAVA. Write a program to monitor the flow of an...
This program must be done in JAVA. Write a program to monitor the flow of an item into an out of a warehouse. The warehouse has numerous deliveries and shipments for this item (a widget) during the time period covered. A shipment out (an order) is billed at a profit of 50% over the cost of the widget. Unfortunately each incoming shipment may have a different cost associated with it. The accountants of the firm have instituted a last-in, first...
Write a Java program that prompts the user to input a word (String). The program must...
Write a Java program that prompts the user to input a word (String). The program must print the reversed word with all consecutive duplicate characters removed. The program must contain the following classes: - The StackX class (you can use the Java Stack class). - The Reverse class which must contain a private data field called “word” of type string, a constructor, and a void method called revNoDup(). The revNoDup() method must reverse the word and remove the consecutive duplicate...
Write a Java program for a car dealership. The dealer has 10 cars in the showroom...
Write a Java program for a car dealership. The dealer has 10 cars in the showroom and space for 50 cars. Each car has a unique plate number, model, color, company, and a year of manufacture. Your program should have two classes: the "Car" class has the car information and the "info" method that displays the car information. The other class is an array class "CarArray" that has four methods:" display", "insert", "find", and the "delete" method. When the program...
Develop a Java program for the question below: A running race among animals: In a race...
Develop a Java program for the question below: A running race among animals: In a race among animals different types of animals are differenciated wih their running styles. In each iteration of the race, each participating animal makes a move acording to its style of running. Total length of the racecourt is NTotal (store this variable properly in the test class). Determine the winner animal and completion time of the race using polymorphism. Each of these animal classes extends from...
PROGRAM MUST BE WRITTEN IN JAVAFX Develop a program flowchart and then write a menu-driven Java...
PROGRAM MUST BE WRITTEN IN JAVAFX Develop a program flowchart and then write a menu-driven Java program that will solve the following problem. The program uses one and two-dimensional arrays to accomplish the tasks specified below. The menu is shown below. Please build a control panel as follows: (Note: the first letter is shown as bold for emphasis and you do not have to make them bold in your program.) Help SetParams FillArray DisplayResults Quit Upon program execution, the screen...
Write an applet in Java that creates a yellow colored filled circle on screen. Inside this...
Write an applet in Java that creates a yellow colored filled circle on screen. Inside this circle the word “GO” with Arial font and size 24, bold face needs to be printed. Justify your syntax.   answer within 25 min plzzz
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT