8 Replies - 1834 Views - Last Post: 20 October 2010 - 03:46 PM Rate Topic: -----

#1 learnplaycreate  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 119
  • Joined: 27-August 09

KeyListener problems

Posted 20 October 2010 - 02:07 AM

hey guys, i'm trying learn java, and apparently a good place to start is a pong like game, so i've had some moderate success. i can make a ball bounce around a board, and detect when it hits a paddle. now the problem i'm facing is getting the paddle to move.
the following code as i said above works
package secondball;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Main extends JApplet {
    public static final int xsiz = 300;
    public static final int ysiz = 300;
    boolean pressdown = false, pressup = false;

    balls ball1 = new balls();
    paddle paddle1 = new paddle();

    public Main (){
        //Timer to repaint() once every 30 ms
        Timer t = new Timer(30, new ActionListener(){
            public void actionPerformed(ActionEvent e){
                //update Ball's position
                ball1.testedge();
                if (pressdown){
                    paddle1.moveleft();
                }
                if (pressup){
                    paddle1.moveright();
                }
                checkballpaddle();
                ball1.moveball();
                repaint();
        }
        });

   t.start();
   
    }
  
    
   public void paint (Graphics g){
       super.paint(g);
       g.setColor(Color.BLACK);
       g.drawRect(0, 0, xsiz, ysiz);

       g.setColor(Color.red);
       g.fillOval(ball1.x, ball1.y, 25, 25);

       g.setColor(Color.black);
       g.fillRect(paddle1.posx, paddle1.posy, paddle1.x, paddle1.y);

   }

public void checkballpaddle(){
    if (((ball1.x +25)> paddle1.posx)&&(ball1.x < (paddle1.posx + 75))){
    if ((ball1.y + 25)> paddle1.posy){
        ball1.ychange();
        }
    }
}

}


i'm using netbeans and there are other classes that i have not included for the sake of brevity.
the following code is the modified version where i try to implement KeyListener and the code compiles fine, but it doesn't run, any advice would be much appreciated.
package secondball;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Main extends JApplet implements KeyListener {
    public static final int xsiz = 300;
    public static final int ysiz = 300;
    boolean pressdown = false, pressup = false;

    balls ball1 = new balls();
    paddle paddle1 = new paddle();

    public Main (){
        //Timer to repaint() once every 30 ms
        Timer t = new Timer(30, new ActionListener(){
            public void actionPerformed(ActionEvent e){
                //update Ball's position
                ball1.testedge();
                if (pressdown){
                    paddle1.moveleft();
                }
                if (pressup){
                    paddle1.moveright();
                }
                checkballpaddle();
                ball1.moveball();
                repaint();
        }
        });

   t.start();
   
    }
   public void keypressed(KeyEvent e){
       if(e.getKeyCode() == KeyEvent.VK_DOWN){
           pressdown = true;
       }
       if(e.getKeyCode() == KeyEvent.VK_UP){
           pressup = true;
       }
     e.consume();
   }
    
   public void paint (Graphics g){
       super.paint(g);
       g.setColor(Color.BLACK);
       g.drawRect(0, 0, xsiz, ysiz);

       g.setColor(Color.red);
       g.fillOval(ball1.x, ball1.y, 25, 25);

       g.setColor(Color.black);
       g.fillRect(paddle1.posx, paddle1.posy, paddle1.x, paddle1.y);

   }

public void checkballpaddle(){
    if (((ball1.x +25)> paddle1.posx)&&(ball1.x < (paddle1.posx + 75))){
    if ((ball1.y + 25)> paddle1.posy){
        ball1.ychange();
        }
    }
}
public boolean keyDown(Event e, int key){
    if (key == 1006){
        paddle1.moveleft();
    }
    if (key == 1007){
        paddle1.moveright();
    }
    return true;
}
}


i beleive the problem is with the line where i try to implement KeyLestener as i can comment out the keydown() method and i still doesn't run. thanx again

This post has been edited by learnplaycreate: 20 October 2010 - 02:41 AM


Is This A Good Question/Topic? 0
  • +

Replies To: KeyListener problems

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2734
  • View blog
  • Posts: 11,515
  • Joined: 20-September 08

Re: KeyListener problems

Posted 20 October 2010 - 03:21 AM

What do you mean by 'doesn't run'?
Was This Post Helpful? 0
  • +
  • -

#3 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,032
  • Joined: 15-July 08

Re: KeyListener problems

Posted 20 October 2010 - 03:40 AM

Firstly, don't use a constructor for Applets. Use the init() method. Secondly, you forgot to add the key listener and handler. I have a tutorial on an InputManager class that will make your task easier.

http://www.dreaminco...-manager-class/
Was This Post Helpful? 0
  • +
  • -

#4 learnplaycreate  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 119
  • Joined: 27-August 09

Re: KeyListener problems

Posted 20 October 2010 - 04:10 AM

so i figured out what was going wrong, apparently you have to override all the events in keylistener, i've fixed the code and now it will run; the ball bounces around the board again, it is not recognizing any input from the keyboard. i've tried to make it so the up button moves the paddle in one direction and the down button moves it in the other. i'm not sure if it is the if statements on line 20 and 23 that are not working or the keypress and keyrelease methods
package secondball;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Main extends JApplet implements KeyListener {
    public static final int xsiz = 300;
    public static final int ysiz = 300;
    boolean pressdown = false, pressup = false;

    balls ball1 = new balls();
    paddle paddle1 = new paddle();

    public Main (){
        //Timer to repaint() once every 30 ms
        Timer t = new Timer(30, new ActionListener(){
            public void actionPerformed(ActionEvent e){
                //update Ball's position
                ball1.testedge();
                if (pressdown){
                    paddle1.moveleft();
                }
                if (pressup){
                    paddle1.moveright();
                }
                checkballpaddle();
                ball1.moveball();
                repaint();
        }
        });

   t.start();
   
    }
   public void keyPressed(KeyEvent e){
       if(e.getKeyCode() == KeyEvent.VK_DOWN){
           pressdown = true;
       }
       if(e.getKeyCode() == KeyEvent.VK_UP){
           pressup = true;
       }
     e.consume();
   }
   public void keyReleased (KeyEvent e){
       if(e.getKeyCode() == KeyEvent.VK_DOWN){
           pressdown = false;
       }
       if(e.getKeyCode() == KeyEvent.VK_UP){
           pressup = false;
       }
       e.consume();
   }
   public void keyTyped(KeyEvent e){

   }
   public void paint (Graphics g){
       super.paint(g);
       g.setColor(Color.BLACK);
       g.drawRect(0, 0, xsiz, ysiz);

       g.setColor(Color.red);
       g.fillOval(ball1.x, ball1.y, 25, 25);

       g.setColor(Color.black);
       g.fillRect(paddle1.posx, paddle1.posy, paddle1.x, paddle1.y);

   }

public void checkballpaddle(){
    if (((ball1.x +25)> paddle1.posx)&&(ball1.x < (paddle1.posx + 75))){
    if ((ball1.y + 25)> paddle1.posy){
        ball1.ychange();
        }
    }
}

}


sorry dogstopper i just read your post after i posted this, i'll read your tutorial before i ask any more questions, thanx mate

This post has been edited by learnplaycreate: 20 October 2010 - 04:14 AM

Was This Post Helpful? 0
  • +
  • -

#5 learnplaycreate  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 119
  • Joined: 27-August 09

Re: KeyListener problems

Posted 20 October 2010 - 05:09 AM

back again, so i've read your tutorial, seems very straight forward, i understand what you have done and why it should work. i created the class as suggested - well really i just copied yours to limit the risk of typos, i changed the constructor in Main.class to init() - everything still worked; i don't know where to add the
addKeyListener(new InputManager());


line, if i put it in the init() method it will not compile so i made a constructor and it did not compile.
i get the error; invalid method declaration; return type required addKeyListener(new InputManager());

i tried a line
InputManager IM = new InputManager(); 


instead of the line you suggested - but the paddle did not move when i pressed the corresponding buttons,
not sure what i'm doing wrong, any help would be greatly appreciated.

so my code as it stands looks like this
package secondball;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Main extends JApplet{
    public static final int xsiz = 300;
    public static final int ysiz = 300;
    boolean pressdown = false, pressup = false;
    
    balls ball1 = new balls();
    paddle paddle1 = new paddle();
    
    public void init(){
        //Timer to repaint() once every 30 ms
        Timer t = new Timer(30, new ActionListener(){
        addKeyListener(new InputManager());   
            public void actionPerformed(ActionEvent e){
                //update Ball's position
                ball1.testedge();
                if (InputManager.leftPressed){
                    paddle1.moveleft();
                }
                if (InputManager.rightPressed){
                    paddle1.moveright();
                }
                checkballpaddle();
                ball1.moveball();
                repaint();
        }
        });

   t.start();
   
    }
   
   public void paint (Graphics g){
       super.paint(g);
       g.setColor(Color.BLACK);
       g.drawRect(0, 0, xsiz, ysiz);

       g.setColor(Color.red);
       g.fillOval(ball1.x, ball1.y, 25, 25);

       g.setColor(Color.black);
       g.fillRect(paddle1.posx, paddle1.posy, paddle1.x, paddle1.y);

   }

public void checkballpaddle(){
    if (((ball1.x +25)> paddle1.posx)&&(ball1.x < (paddle1.posx + 75))){
    if ((ball1.y + 25)> paddle1.posy){
        ball1.ychange();
        }
    }
}

}



there are now three other classes that i have not included, balls, paddle, and InputManager. i'm confident there is nothing wrong with them as balls and paddle have not had any problems and InputManager i copied directly from your tutorial.
Was This Post Helpful? 0
  • +
  • -

#6 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,032
  • Joined: 15-July 08

Re: KeyListener problems

Posted 20 October 2010 - 12:22 PM

You need to check if your key is down or not! That's why I wrote the InputManager class. You make calls to ask whether a key is currently down or not.
Was This Post Helpful? 0
  • +
  • -

#7 learnplaycreate  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 119
  • Joined: 27-August 09

Re: KeyListener problems

Posted 20 October 2010 - 02:38 PM

View PostDogstopper, on 20 October 2010 - 11:22 AM, said:

You need to check if your key is down or not! That's why I wrote the InputManager class. You make calls to ask whether a key is currently down or not.

line 17 creates an error that will not allow the code to compile, the error is; invalid method declaration; return type required addKeyListener(new InputManager());
have i added this line in the wrong place?

i thought line 27 and 30 were checking if a key was down or not, if this is not the case can you please explain why?

it the error on line 17 that is causing me concern, i thought i followed your tutorial to the letter but clearly i missed something critical. thank you in advance
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10596
  • View blog
  • Posts: 39,257
  • Joined: 27-December 08

Re: KeyListener problems

Posted 20 October 2010 - 03:06 PM

Notice how you add the KeyListener within the ActionListener. As the ActionListener in-line declaration is technically a class (specifically, an anonymous inner class), you can only declare methods, fields, and other classes within it. Like normal classes, you must invoke methods within other methods. Really, though, it makes more sense to move the addKeyListener() call out of the Timer declaration.
public void init(){  
      //Timer to repaint() once every 30 ms  
      Timer t = new Timer(30, new ActionListener(){  
            addKeyListener(new InputManager());     


Was This Post Helpful? 2
  • +
  • -

#9 learnplaycreate  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 119
  • Joined: 27-August 09

Re: KeyListener problems

Posted 20 October 2010 - 03:46 PM

thank you so much, i appreiciate your reading the whole post and supplying the solution, all works well now, does exactly what i wanted it to.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1