I am making a Pong game and I am having trouble making the paddles move. I have already drawn them onto my content pane and I want to use a key listener to make them move. I have heard about using key binding, but I do not understand the tutorial of how to do it. My teacher has not taught any of this material yet, and he does not plan on it. So any help would be greatly appreciated. I have a key listener setup but does not move the object at all. Thanks again! :)

} else if (buttonText.equals("Vs. Comp")) {
                name1 = JOptionPane.showInputDialog(null, "Name of Player One:", "Player One", JOptionPane.OK_CANCEL_OPTION);
                contentPane.remove(pane);
                contentPane.validate();
                contentPane.remove(singleplayer);
                contentPane.validate();
                contentPane.remove(multiplayer);
                contentPane.validate();
                contentPane.remove(comp);
                contentPane.validate();
                p = new PlayerOne();
                p.paintPaddle();
                contentPane.validate();
            }
        }
    }

    private class PlayerOne extends Component implements KeyListener {

        int x, y = 180;
        public int width = contentPane.getWidth();

        public void run() {


            paintPaddle();
            doNothing(PAUSE);
            setFocusable(true);
            addKeyListener(this);
        }

        public void paintPaddle() {
            Graphics g = contentPane.getGraphics();


            g.setColor(Color.RED);
            g.fillRect(1, y, 5, (width / 12));
            repaint();
        }

        public void keyTyped(KeyEvent e) {
        }

        public void keyPressed(KeyEvent e) {

            y = getY();
            if (e.getKeyCode() == KeyEvent.VK_DOWN) {
                y += 10;

                repaint();
            } else if (e.getKeyCode() == KeyEvent.VK_UP) {
                y -= 10;
                repaint();
            }
            repaint();
        }

        public void keyReleased(KeyEvent e) {
            repaint();
        }
    }

Recommended Answers

All 13 Replies

Try putting a few print staements to see if:
the keylistener is being called
the appropriate if test bocks are being executed
the new vlue of y is correct
the new value of y is being used in the paintPaddle method.

My guess? paintPaddle isn't being called - you should override paintComponent. Also, I can't see where PlayerOne actually creates a Swing component that's actually visible on the screen, nor can I see when/whether the run() method ever gets called...

Try putting a few print staements to see if:
the keylistener is being called
the appropriate if test bocks are being executed
the new vlue of y is correct
the new value of y is being used in the paintPaddle method.

My guess? paintPaddle isn't being called - you should override paintComponent. Also, I can't see where PlayerOne actually creates a Swing component that's actually visible on the screen, nor can I see when/whether the run() method ever gets called...

I tried calling the run() method and that did not work. So, I added the contents of the run() method to a constructor inside of PlayerOne and it still does not move. I'm so confused as to why though.

} else if (buttonText.equals("Vs. Comp")) {
                name1 = JOptionPane.showInputDialog(null, "Name of Player One:", "Player One", JOptionPane.OK_CANCEL_OPTION);
                contentPane.remove(pane);
                contentPane.validate();
                contentPane.remove(singleplayer);
                contentPane.validate();
                contentPane.remove(multiplayer);
                contentPane.validate();
                contentPane.remove(comp);
                contentPane.validate();
                p = new PlayerOne();
                /*
                p.requestFocus();
                p.paintPaddle();
                contentPane.validate();
                p.validate();
                 *
                 */
                p.requestFocus();
                contentPane.add(p);
                
            }
        }
    }

    private class PlayerOne extends Component implements KeyListener {

        int x, y = 180;
        public int width = contentPane.getWidth();

        public PlayerOne (){
            paintPaddle();
            doNothing(PAUSE);
            setFocusable(true);
            addKeyListener(this);
        }
        /*
        public void run() {
            paintPaddle();
            doNothing(PAUSE);
            setFocusable(true);
            addKeyListener(this);
        }
         *
         */

        public void paintPaddle() {
            Graphics g = contentPane.getGraphics();
            g.setColor(Color.RED);
            g.fillRect(1, y, 5, (width / 12));
            repaint();
        }

        public void keyTyped(KeyEvent e) { }

        public void keyPressed(KeyEvent e) {
            y = getY();
            if (e.getKeyCode() == KeyEvent.VK_DOWN) {
                y += 10;
                repaint();
            } else if (e.getKeyCode() == KeyEvent.VK_UP) {
                y -= 10;
                repaint();
            }
            repaint();
        }

        public void keyReleased(KeyEvent e) {
            repaint();
        }
    }

Like I said - lots of print statements to see exactly what's happening (or not happening).

Ok so I placed print statements inside of the keypressed methods, and nothing printed out. So there is something wrong with my listener. I'm going to try a switch statement instead of an if statement.

public void keyPressed(KeyEvent e) {
            y = getY();
            if (e.getKeyCode() == KeyEvent.VK_DOWN) {
                System.out.println("Pressed");
                //y += 10;
               // repaint();
            } else if (e.getKeyCode() == KeyEvent.VK_UP) {
                //y -= 10;
             //   repaint();
            }
           // repaint();
        }

Like I said - lots of print statements to see exactly what's happening (or not happening).

Try a print as the first line of the listener - right now you don't know whether the if is failing or the method isn't being called at all.
Random code changes (eg convertingf ifs to switch) are generally useless until you have identified the problem properly.

So, I got the run() method to work. I changed the PlayerOne from a component to a Thread, yet the key listener still is not working.

} else if (buttonText.equals("Vs. Comp")) {
                name1 = JOptionPane.showInputDialog(null, "Name of Player One:", "Player One", JOptionPane.OK_CANCEL_OPTION);
                contentPane.remove(pane);
                contentPane.validate();
                contentPane.remove(singleplayer);
                contentPane.validate();
                contentPane.remove(multiplayer);
                contentPane.validate();
                contentPane.remove(comp);
                contentPane.validate();
                p = new PlayerOne();
                /*
                p.requestFocus();
                p.paintPaddle();
                contentPane.validate();
                p.validate();
                 *
                 */
                //p.requestFocus();
                //contentPane.add(p);
                p.start();
            }
        }
    }

    private class PlayerOne extends Thread implements KeyListener {

        int x, y = 180;
        public int width = contentPane.getWidth();
/*
        public PlayerOne (){
            paintPaddle();
            doNothing(PAUSE);
            setFocusable(true);
            addKeyListener(this);
            
        }
 *
 */
        
        public void run() {
            while(true){
            paintPaddle();
            doNothing(PAUSE);
            setFocusable(true);
            addKeyListener(this);
            }
        }
         

        public void paintPaddle() {
            Graphics g = contentPane.getGraphics();
            g.setColor(Color.RED);
            g.fillRect(1, y, 5, (width / 12));
            //repaint();
        }

        public void keyTyped(KeyEvent e) { }

        public void keyPressed(KeyEvent e) {
            y = getY();
            /*
            if (e.getKeyCode() == KeyEvent.VK_DOWN) {
                //System.out.println("Pressed");
                y += 10;
               // repaint();
            } else if (e.getKeyCode() == KeyEvent.VK_UP) {
                y -= 10;
               //repaint();
            }
            repaint();
             *
             */
            switch (e.getKeyCode()) {

                case KeyEvent.VK_DOWN:
                    y -= 5;

                    break;

            }
            switch (e.getKeyCode()) {

                case KeyEvent.VK_UP:
                    y += 5;

                    break;

            }
                        repaint();
        }

        public void keyReleased(KeyEvent e) {
            repaint();
        }
    }

See previous post.

I tried that.. still no response from the keylistener.

public void keyPressed(KeyEvent e) {
            y = getY();
            if(e.getKeyCode() == KeyEvent.VK_DOWN){
                System.out.print("Pressed");
                //y+=10;


            }

See previous post.

I'm not saying it again.
Good luck.
J.

your suggestion made no sense. that is what i did. thanks for explaining what you meant. clearly I need help, but whatever. thanks anyway

His post said to make print the first statement in your listener to make sure the method was even getting called at all - and you didn't do that.

Getting snippy about it isn't likely to encourage anyone else to volunteer their time to help you.

Yes, I understand and I did that and I asked for clarification on what he meant and he just repeated himself which is no help. I'm not getting snippy I'm just asking for help that is all. I tried and placed it as the first statement and it did not work so that is why I am still asking for help. I didn't post the code of me making it the first statement, because that wouldn't help at all.

His post said to make print the first statement in your listener to make sure the method was even getting called at all - and you didn't do that.

Getting snippy about it isn't likely to encourage anyone else to volunteer their time to help you.

You posted code that showed the print being inside the if() test on the key code and said that it didn't work. That is why James re-iterated what he said.

I didn't post the code of me making it the first statement, because that wouldn't help at all.

Why would you post code showing you not doing what he suggested and saying that you tried it and it didn't work? That makes no sense at all.

Anyway, it's most likely a focus issue. Key listeners only fire when the component they are attached to have the focus. You didn't post the whole code, so your containment hierarchy isn't entirely clear and it's hard to tell if perhaps you've added the listener to the wrong component.

I'd also recommend that you take another of James' suggestions and override paintComponent() to handle your graphics rendering. Update object positions with your listeners and a timing thread for the ball position and then repaint().

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.