Acceleration of an object

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…

Keep track of the velocity and then just decrease the velocity each step in its opposite direction.


// Class members
double vel;
double pos;


// Animation
pos += vel;
vel -= friction * vel;
// Friction is smaller than 1


Just modify the velocity variabel on your Car class. Increase it when KeyPressed and when you detect KeyReleased, decrease it with a value repeatly every Timer’s call. On your code you put both increament and decreament on same KeyPressed method.

Don’t add the key listener to a text field in the bottom of the panel. It will only get key events if you click in the field and type in it. Instead try adding the key listener to the ImagePanel or JFrame. You can also call requestFocus() on the frame or panel that has the key listener to make sure it has focus. If a component doesn’t have focus then it won’t receive any of the key events to pass onto its listeners.

Thank you everybody, those were helpful replies. requestFocus( ) and adding the keyListener to the ImagePanel were what I needed to get it running.