Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Help - Swing Timer, 2 KeyEvents

  1. #1
    Junior Member
    Join Date
    Jul 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help - Swing Timer, 2 KeyEvents

    I am making a 2 player game that needs two objects to move around.

    In a KeyEvent, it detects when I move Player 1 Up and Down (jLabel7) using the Up key and the Down key.

    In the same KeyEvent, it detects when I can move Player 2 Up and Down (jLabel6) using the W key (up) and the S key (down).

    Both ships work fine. When I press Up, it activates a timer and moves jLabel7 up.
    When I press Down, it activates a timer and moves jLabel7 down.
    When I press W, it activates a timer and moves jLabel6 up.
    When I press S, it activates a timer and moves jLabel6 down.

    Also, I have a KeyRelease event that detects when these keys were released, and it sets a boolean to false, letting the timers know they can shut off.



    So all is well right? wrong. If I try to hold Up and S together for instance, moving each jLabel in a direction, it will activate their timers, moving them. If I release the keys, it will not detect a Keyrelease on one of the jLabels and the jLabel won't stop, even if i keep hitting all four keys to try and reregister a KeyRelease. The ship that stopped, now wont move unless i hold one of the keys that moved the ship that is stuck moving, then press it's keys. For example:

    I press Up (jLabel7 moves up) and S (jLabel6 moves down) at the same time. I let go of both keys, jLabel7 stops moving, jLabel6 is stuck going down no matter what i do. Now jLabel7 will not move unless I hold a key that was supposed to move jLabel6 THEN press a jLabel7 key.


    Now I'm guessing, that it is because pressing multiple keys at once won't work because they aren't in separate threads, they get all funny because the KeyPressed Action Event is only working once and can't work with all these seperate key presses and it gets stuck somewhere. Each press of a key activates a Timer/thread, but it itself isn't one.



    What is the error and the best way to fix it?

    private void jButton1KeyPressed(java.awt.event.KeyEvent evt) {                                    
    //KEY UP TIMER
               ActionListener timerUp = new ActionListener() {
                           public void actionPerformed(ActionEvent evt) {
     
                 jLabel7.setLocation(jLabel7.getX(),jLabel7.getY()-2);
                         repaint();
     
                         //STOPS TIMER
                         if(Ship1Up == false){
                         timer.stop();
                         }
              }
           };
     
    //KEY UP
    if(evt.getKeyCode() == KeyEvent.VK_UP && Ship1Up == false){
    Ship1Up = true;
    Ship1Down = false;
     
           //PLAYS UP TIMER
      if(Ship1Up == true){
              timer = new Timer(20, timerUp);
              timer.start();
      }
      }
     
     
    //KEY DOWN TIMER
               ActionListener timerDown = new ActionListener() {
                           public void actionPerformed(ActionEvent evt) {
     
                 jLabel7.setLocation(jLabel7.getX(),jLabel7.getY()+2);
                         repaint();
     
                         //STOPS TIMER
                         if(Ship1Down == false){
                         timer.stop();
                         }
              }
           };
     
    //KEY DOWN
    if(evt.getKeyCode() == KeyEvent.VK_DOWN && Ship1Down == false){
    Ship1Down = true;
    Ship1Up = false;
     
           //PLAYS DOWN TIMER
              timer = new Timer(20, timerDown);
              timer.start();
      }
     
     
     
     
           //KEY W TIMER
               ActionListener timerW = new ActionListener() {
                           public void actionPerformed(ActionEvent evt) {
     
                 jLabel6.setLocation(jLabel6.getX(),jLabel6.getY()-2);
                         repaint();
     
                         //STOPS TIMER
                         if(Ship2Up == false){
                         timer.stop();
                         }
              }
           };
     
    //KEY W
    if(evt.getKeyCode() == KeyEvent.VK_W && Ship2Up == false){
    Ship2Up = true;
     
           //PLAYS W TIMER
              timer = new Timer(20, timerW);
              timer.start();
      }
     
     
    //KEY S TIMER
               ActionListener timerS = new ActionListener() {
                           public void actionPerformed(ActionEvent evt) {
     
                 jLabel6.setLocation(jLabel6.getX(),jLabel6.getY()+2);
                         repaint();
     
                         //STOPS TIMER
                         if(Ship2Down == false){
                         timer.stop();
                         }
              }
           };
     
    //KEY S
    if(evt.getKeyCode() == KeyEvent.VK_S && Ship2Down == false){
    Ship2Down = true;
     
           //PLAYS DOWN TIMER
              timer = new Timer(20, timerS);
              timer.start();
      }
        }                                   
     
        private void jButton1KeyReleased(java.awt.event.KeyEvent evt) {
     
    //KEY UP RELEASED
    if(evt.getKeyCode() == KeyEvent.VK_UP){
    Ship1Up = false;
      }
     
    //KEY DOWN RELEASED
    if(evt.getKeyCode() == KeyEvent.VK_DOWN){
    Ship1Down = false;
      }
     
    //KEY UP RELEASED
    if(evt.getKeyCode() == KeyEvent.VK_W){
    Ship2Up = false;
      }
     
    //KEY DOWN RELEASED
    if(evt.getKeyCode() == KeyEvent.VK_S){
    Ship2Down = false;
      }
     
     
     
        }
    Last edited by Gheta; July 29th, 2009 at 02:02 PM.


  2. #2
    Junior Member
    Join Date
    Jul 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help - Swing Timer, 2 KeyEvents

    Sorry for the double post but I figured out multiple key presses for anyone else having this problem.

    1 You need to have booleans to remember the key press state (press a key, a boolean becomes true, release the key and the boolean becomes false, also have a timer.)

    2 This was my error. At the beginning of all the code, I declared:

    Timer timer;

    when I should have declared separately for each key:

    Timer upTimer;
    Timer downTimer;
    Timer wTimer;
    Timer sTimer;


    This solved my issue and multiple keys can work at once for multiple objects on screen.

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Help - Swing Timer, 2 KeyEvents

    Hello Gheta, welcome to the forums!

    I'm glad you solved your problem I didn't even have a chance to look at it.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. How to Add ActionListener to a JButton in Swing?
    By JavaPF in forum Java Swing Tutorials
    Replies: 17
    Last Post: April 24th, 2013, 05:14 PM
  2. Java program to Add a JMenu toolbar to a Java Swing application
    By JavaPF in forum Java Swing Tutorials
    Replies: 6
    Last Post: March 6th, 2012, 12:25 PM
  3. How to Use a JSlider - Java Swing
    By neo_2010 in forum Java Swing Tutorials
    Replies: 4
    Last Post: March 29th, 2010, 09:33 AM
  4. Swing incorrect behaviour from JRE5 to JRE6
    By singhkanhaiya in forum AWT / Java Swing
    Replies: 1
    Last Post: August 25th, 2009, 01:23 AM
  5. How to Use the JList component - Java Swing
    By neo_2010 in forum Java Swing Tutorials
    Replies: 1
    Last Post: July 11th, 2009, 04:02 AM