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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
|
import javax.swing.*; import java.awt.*; import java.awt.event.*;
public class RacingCarFrame extends JFrame { public RacingCarFrame( ) { ImagePanel car = new ImagePanel( ); add( car );
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 { private Timer timer = new Timer( 10, new TimerListener( ) );
int carX = 20; int carY = 40;
int carSpeed = 0;
int carBodyWidth = 50; int carBodyHeight = 10; int carHoodWidth = 10; int carHoodHeight = 10; int carTireHeight = 10; int carTireWidth = 10;
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 ); }
private class TimerListener implements ActionListener { public void actionPerformed( ActionEvent ae ) { carX += carSpeed; if( carX >= getWidth( ) ) carX = 20; repaint( ); } }
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 ) { } }
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...