I\'m trying to implement the game Racecar. I would like to control the java prog
ID: 3727179 • Letter: I
Question
I'm trying to implement the game Racecar. I would like to control the java program by numbers 1-9 instead of by clicking the mouse. I have included the whole program, hope someone can show me how to use keyboard instead of mouse.
eimport java.util.ArrayList; 2 import java.util.List; 3 import java . util"Scanner; 4 import java.io. 5 import javax.swing.JFrame; Q 6 import javax,swing, JoptionPane; 8 public class RaceTrack i 10 final static int X-@; // These are used to make the code more readable final static int Y 1; final static int scale 10; // Adjust this value to adjust the size of the course final static int size - 4 scale; / The final size of the field final static int topscore length 10; // Length of the high score list 13 17 18 19 static List IineCoordinates = new ArrayListExplanation / Answer
CarPanel class
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.imageio.*;
import java.io.*;
public class CarPanel extends JPanel
{
private BufferedImage bufCarN;
private BufferedImage bufCarE;
private BufferedImage bufCarW;
private BufferedImage bufCarS;
public int nx = 800, ny = 0;
private int currAngle = 0;
private int acceleration = 0;
private int speed = 0;
private Graphics g;
private void loadPics()
{
try
{
bufCarN = ImageIO.read(new File("KD\KDN.png"));
bufCarE = ImageIO.read(new File("KD\KDE.png"));
bufCarW = ImageIO.read(new File("KD\KDW.png"));
bufCarS = ImageIO.read(new File("KD\KDS.png"));
}
catch(IOException ex)
{
System.out.println("Image not found");
}
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
loadPics();
g.drawImage(bufCarE, 100, 100, null);
}
}
MainGameScreen class
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GameScreen{
public static void main(String[] args)
{
new GameScreen();
}
JFrame f;
private CarPanel cp;
public GameScreen()
{
cp = new CarPanel();
f = new JFrame("Circuit#1");
f.add(cp);
f.setVisible(true);
f.setBounds(100, 100, 800, 600);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}