Hello, I’m a programming student and one of my exercise examples was to animate a car to drive across the screen. Done. Then I wanted to get creative and have the user hold a key to accelerate the car, and then when they let go, have the car slow to a stop. Sadly, I cannot get this to work. Incorporating the KeyListener into the Timer has proven too challenging for me. I would appreciate any help.
I understand that many of these practices aren’t the best. I SHOULD have built a Car class, but the exercise called for drawing Graphics class shapes to make car, and I’ve been building on that… This could also work supposedly with an Image object.
/* Nicholas Pickering
* 2/7/2011 1:25 AM
*
* RacingCar.java
*
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class RacingCarFrame extends JFrame
{
public RacingCarFrame( )
{
//panel to draw car onto.
ImagePanel car = new ImagePanel( );
add( car );
//KeyListener requires an object to accept the KeyEvents... I guess
//I chose a textfield.
JTextField textfield = new JTextField( );
textfield.addKeyListener( car.getKeyListener( ) );
textfield.setFocusable( true );
add( textfield, BorderLayout.SOUTH );
}
public static void main( String [] args )
{
RacingCarFrame frame = new RacingCarFrame( );
frame.setTitle( "RacingCar" );
frame.setSize( 400, 400 );
frame.setLocationRelativeTo( null );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setVisible( true );
}
}
class ImagePanel extends JPanel
{
//timer for animation
private Timer timer = new Timer( 10, new TimerListener( ) );
//car's position
int carX = 20;
int carY = 40;
//car's velocity;
int carSpeed = 0;
//car's dimensions
int carBodyWidth = 50;
int carBodyHeight = 10;
int carHoodWidth = 10;
int carHoodHeight = 10;
int carTireHeight = 10;
int carTireWidth = 10;
//render frame
public void paintComponent( Graphics g )
{
super.paintComponent( g );
timer.start( );
g.setColor( Color.GREEN );
g.fillRect( carX, carY - 10, carBodyWidth, carBodyHeight );
g.setColor( Color.GREEN.darker( ) );
g.fillRect( carX + 20, carY - 20, carHoodWidth, carHoodHeight );
g.setColor( Color.BLACK );
g.fillOval( carX + 10, carY, carTireWidth, carTireHeight );
g.fillOval( carX + 30, carY, carTireWidth, carTireHeight );
}
//causes car to move across screen without user interaction
//carSpeed is currently set to 0, so it doesn't move.
//setting carSpeed higher will causes it to drive
//at a consistent speed across the frame.
private class TimerListener implements ActionListener
{
public void actionPerformed( ActionEvent ae )
{
carX += carSpeed;
if( carX >= getWidth( ) )
carX = 20;
repaint( );
}
}
//listens for keys and increases car's speed when SPACE is pressed
private class KeyHandler implements KeyListener
{
public void keyPressed( KeyEvent ke )
{
if( ke.getID( ) == KeyEvent.VK_SPACE )
{
carSpeed++;
repaint( );
}
else
{
if( carSpeed > 0 )
carSpeed--;
repaint( );
}
}
public void keyReleased( KeyEvent ke )
{ }
public void keyTyped( KeyEvent ke )
{ }
}
//returns a KeyHandler object to the jFrame to accept key events
public KeyHandler getKeyListener( )
{
return new KeyHandler( );
}
}
By the way this code is infernally small to read in preview. And typing at the bottom of the post after the code was posted makes the window focus bounce all over haphazardly…