Java-Gaming.org Java4K winners: [ by our judges | by the community ]         
Featured games (67)
games approved by the League of Dukes
Games in Showcase (∞)
games submitted by our members



News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  Print  
  Acceleration of an object  (Read 585 times)
0 Members and 2 Guests are viewing this topic.
Offline 2NickPick

JGO n00b
*

Posts: 9



« on: 2011-02-07 05:12:14 »

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  
/* 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...
Offline krasse

Sr. Member
**

Posts: 282
Medals: 14



« Reply #1 on: 2011-02-07 05:30:05 »

Keep track of the velocity and then just decrease the velocity each step in its opposite direction.
1  
2  
3  
4  
5  
6  
7  
8  
9  
// Class members
double vel;
double pos;


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


Offline ReBirth

JGO Wizard
****

Posts: 1266
Medals: 19



« Reply #2 on: 2011-02-07 05:57:32 »

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.

Games published by our own members! Go get 'em!
Offline lhkbob

JGO Neuromancer
****

Posts: 1174
Medals: 35



« Reply #3 on: 2011-02-07 09:19:15 »

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.

Offline 2NickPick

JGO n00b
*

Posts: 9



« Reply #4 on: 2011-02-14 00:18:35 »

Thank you everybody, those were helpful replies. requestFocus( ) and adding the keyListener to the ImagePanel were what I needed to get it running.
Pages: [1]
  Print  
 
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.505 seconds with 21 queries.