Question: in this java eclipse class, there is a vehicle that is supposed to turn left and right based on pressing the left or right arrow
in this java eclipse class, there is a vehicle that is supposed to turn left and right based on pressing the left or right arrow keys. before the play/pause button is pressed and the time is started, the code acknowledges that the arrow keys are being pressed. however, when the play/pause button is pressed and the timer is started, the code no longer acknowledges that they arrow keys are being pressed. what can i change about my code that allows the vehicle to turn at the press of the arrow keys while the time is running?
public class VehicleViewer implements KeyListener {
// Use with TODO #2 how far to turn on each press (key/mouse) public static double TURN_DELTA = Math.PI / 8; public static final int FRAME_WIDTH = 1000; public static final int FRAME_HEIGHT = 600;
VehicleComponent vehicleComponent = new VehicleComponent();
boolean isLeftPressed = false; boolean isRightPressed = false;
Timer timer = new Timer(100, new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (timer.isRunning()) { System.out.println("Timer is running"); } if (isLeftPressed) { vehicleComponent.turnVehicle(-TURN_DELTA); System.out.println("Left arrow pressed"); } if (isRightPressed) { vehicleComponent.turnVehicle(TURN_DELTA); System.out.println("Right arrow pressed"); } vehicleComponent.tick(); vehicleComponent.repaint(); } });
/** * ensures: creates, initializes, and sets visible the VehicleViewer's frame and * component */ private void driverMain() { final String frameTitle = "Graphics Question"; final int frameXLoc = 100; final int frameYLoc = 200;
JFrame frame = new JFrame(); frame.setTitle(frameTitle); frame.setSize(FRAME_WIDTH, FRAME_HEIGHT); frame.setLocation(frameXLoc, frameYLoc); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(vehicleComponent);
JPanel controlPanel = new JPanel(); JButton turnLeft = new JButton("Turn Left");
// TODO #1 - Turn Update Button into Play/Pause Button JButton playPauseButton = new JButton("Play/Pause"); JButton turnRight = new JButton("Turn Right");
controlPanel.add(turnLeft); controlPanel.add(playPauseButton); controlPanel.add(turnRight);
turnRight.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { vehicleComponent.turnVehicle(TURN_DELTA); vehicleComponent.repaint(); } });
turnLeft.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { vehicleComponent.turnVehicle(-TURN_DELTA); vehicleComponent.repaint(); } });
playPauseButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (timer.isRunning()) { timer.stop(); } else { timer.start(); } } });
// TODO #2 You will want to modify this class to allow a user // to control the user controllable Vehicle with a Mouse or Key press
frame.setFocusable(true); frame.requestFocus();
frame.add(controlPanel, BorderLayout.NORTH); frame.setVisible(true);
frame.addKeyListener(this); } // driverMain
@Override public void keyTyped(KeyEvent e) { // TODO Auto-generated method stub
}
@Override public void keyPressed(KeyEvent e) { int key = e.getKeyCode(); if (key == KeyEvent.VK_RIGHT) { isRightPressed = true; System.out.println("Right arrow key pressed"); } else if (key == KeyEvent.VK_LEFT) { isLeftPressed = true; System.out.println("Left arrow key pressed"); } }
@Override public void keyReleased(KeyEvent e) { int key = e.getKeyCode(); if (key == KeyEvent.VK_RIGHT) { isRightPressed = false; } else if (key == KeyEvent.VK_LEFT) { isLeftPressed = false; } }
// ------------------------------------------------------------
/** * The app's main entry point for the operating system * * @param args - ignored */ public static void main(String[] args) { VehicleViewer VehicleViewer = new VehicleViewer(); VehicleViewer.driverMain(); } // main
} // end class VehicleViewer
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
