Управление понгом с KeyListener

Я столкнулся с небольшим количеством проблем, заставляя мою игру в понг работать, этот проект начался просто с придания мячу физики, но затем я решил сделать еще немного работы

У меня есть тот шар, отскакивающий назад и вперед, и все, кроме клавиш W и S, не управляют игроком один, а клавиши со стрелками вверх и вниз не управляют игроком 2

public void keyPressed(KeyEvent e){  
            if(e.getKeyCode() == e.VK_UP){  
                p2Y -= 3; 
                System.out.println("UP");
            }   
            if(e.getKeyCode() == e.VK_DOWN){  
                p2Y += 3;  
                System.out.println("Down");
            }  
            if(e.getKeyCode() == e.VK_W){  
                p1Y -= 3; 
                System.out.println("Up");
            }   
            if(e.getKeyCode() == e.VK_S){  
                p1Y += 3;  
                System.out.println("down");
            }
            repaint();
        } 

Он даже не показывает системные печатные сообщения

Я не знаю, является ли это только эта часть кода, которая является проблемой, или если это где-то еще

Если это где-то еще здесь ссылка на остальную часть этого файлаhttp://pastebin.com/TJbLBxL7

Весь код:

import javax.swing.*;


import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;

public class Pong extends JPanel
{
    Circle circle1;
    javax.swing.Timer timer;
    ArrayList<Point> points = new ArrayList<Point>();
    int p1X=10, p1Y=320;
    int p2X=760, p2Y = 320;
    int y, y1;

    int p1Score, p2Score;

    boolean playing = true;

    public Pong(Color backcolor, int Width, int Height)
    {
        setBackground(backcolor);
        setPreferredSize(new Dimension(Width, Height));

        //first circle

        circle1 = new Circle(Width / 2, 360, 15, Color.white);
        circle1.setVelocity(4);
        circle1.setDirection(180);

        timer =  new javax.swing.Timer(5, new MoveListener());
        addKeyListener(new KeyWatcher());
        timer.start();
    }

    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);

        //first circle
        circle1.draw(g);
        circle1.fill(g);

        // Draw Players
        g.setColor(Color.red);
        g.fillRect(p1X, p1Y, 10, 75);
        g.setColor(Color.blue);
        g.fillRect(p2X, p2Y, 10, 75);

        //Draw Scores
        g.setColor(Color.white);
        g.drawString("Player 1's Score: " + p1Score, 10, 10);
        g.drawString("Player 2's Score: " + p2Score, getWidth() - 120, 10);
        repaint();

        if(!playing)
        {
            g.setColor(Color.red);
            g.drawString("GAMEOVER!!!", (getWidth() / 2) - 15, 10);
        }
        else
        {
        }
    }

    private class MoveListener extends MouseAdapter implements ActionListener
    {
        public void actionPerformed(ActionEvent arg0)
        {

            int x1 = circle1.getX();
            int y1 = circle1.getY();
            int width = getWidth();
            int height = getHeight();
            int radius1 = circle1.getRadius();

            //first circle

            if(x1 - radius1 <= 0)
            {
                p2Score++;
                circle1.setX(getWidth()/2);
                circle1.setY(getHeight()/2);
            }
            if(x1 + radius1 >= width)
            {
                p1Score++;

                circle1.setX(getWidth()/2);
                circle1.setY(getHeight()/2);
            }
            if(y1 - radius1 <= 0 || y1 + radius1 >= height )
            {
                circle1.turnY();

            }

            if((x1 - radius1 == p1X) || (x1 + radius1 == p2X))
            {
                circle1.turnX();
            }

            circle1.move();
            repaint();
        }

        public void GameOver()
        {
            playing = false;
        }

    }
    private class KeyWatcher implements KeyListener
    {

        @Override
        public void keyPressed(KeyEvent e){  
            if(e.getKeyCode() == e.VK_UP){  
                p2Y -= 3; 
                System.out.println("UP");
            }   
            if(e.getKeyCode() == e.VK_DOWN){  
                p2Y += 3;  
                System.out.println("Down");
            }  
            if(e.getKeyCode() == e.VK_W){  
                p1Y -= 3; 
                System.out.println("Up");
            }   
            if(e.getKeyCode() == e.VK_S){  
                p1Y += 3;  
                System.out.println("down");
            }
            repaint();
        }  

        @Override
        public void keyReleased(KeyEvent e){  
             //if(e.getKeyCode() == e.VK_UP){  
            //        y1 = p2Y;  
             //       repaint();
             //   }   
             //   if(e.getKeyCode() == e.VK_DOWN){  
             //       y1 = p2Y;  
            //        repaint();
            //    } 
             //if(e.getKeyCode() == e.VK_W){  
             //       y = p1Y;  
            //        repaint();
            //    }   
            //    if(e.getKeyCode() == e.VK_S){  
            //        y = p1Y;  
            //        repaint();
             //   } 
        }  

        @Override
        public void keyTyped(KeyEvent arg0) {
            // TODO Auto-generated method stub

        }


    }
}

Мой новый код Проблема с этим в том, что одновременно можно управлять только одним игроком

У него также есть моя глупая попытка исправить это, сделав 2 MyKeyAction, но это не изменило его вообще

package Bouncy;

import javax.swing.*;


import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;

public class Pong extends JPanel
{
    Circle circle1;
    javax.swing.Timer timer;
    ArrayList<Point> points = new ArrayList<Point>();
    int p1X=10, p1Y=320;
    int p2X=760, p2Y = 320;
    int y, y1;

    int p1Score, p2Score;

    boolean playing = true;

    public Pong(Color backcolor, int Width, int Height)
    {
        setBackground(backcolor);
        setPreferredSize(new Dimension(Width, Height));

        //first circle

        circle1 = new Circle(Width / 2, 360, 15, Color.white);
        circle1.setVelocity(4);
        circle1.setDirection(180);

        timer =  new javax.swing.Timer(5, new MoveListener());
        setupKeyBinding();
        setupKeyBinding2();
        timer.start();
    }
    private void setupKeyBinding() {
          int condition = JComponent.WHEN_IN_FOCUSED_WINDOW;
          InputMap inMap = getInputMap(condition);
          ActionMap actMap = getActionMap();

          // this uses an enum of Direction that holds ints for the arrow keys
          for (DirectionP1 direction : DirectionP1.values()) {
             int key = direction.getKey();
             String name = direction.name();

             // add the key bindings for arrow key and shift-arrow key
             inMap.put(KeyStroke.getKeyStroke(key, 0), name);
             inMap.put(KeyStroke.getKeyStroke(key, InputEvent.SHIFT_DOWN_MASK), name);
             actMap.put(name, new MyKeyAction(direction));
          }
       }
    private void setupKeyBinding2() {
          int condition = JComponent.WHEN_IN_FOCUSED_WINDOW;
          InputMap inMap = getInputMap(condition);
          ActionMap actMap = getActionMap();

          // this uses an enum of Direction that holds ints for the arrow keys
          for (DirectionP2 direction : DirectionP2.values()) {
             int key = direction.getKey();
             String name = direction.name();

             // add the key bindings for arrow key and shift-arrow key
             inMap.put(KeyStroke.getKeyStroke(key, 0), name);
             inMap.put(KeyStroke.getKeyStroke(key, InputEvent.SHIFT_DOWN_MASK), name);
             actMap.put(name, new MyKeyAction2(direction));
          }
       }
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);

        //first circle
        circle1.draw(g);
        circle1.fill(g);

        // Draw Players
        g.setColor(Color.red);
        g.fillRect(p1X, p1Y, 10, 75);
        g.setColor(Color.blue);
        g.fillRect(p2X, p2Y, 10, 75);

        //Draw Scores
        g.setColor(Color.white);
        g.drawString("Player 1's Score: " + p1Score, 10, 10);
        g.drawString("Player 2's Score: " + p2Score, getWidth() - 120, 10);
        repaint();

        if(!playing)
        {
            g.setColor(Color.red);
            g.drawString("GAMEOVER!!!", (getWidth() / 2) - 15, 10);
        }
        else
        {
        }
    }

    private class MoveListener extends MouseAdapter implements ActionListener
    {
        public void actionPerformed(ActionEvent arg0)
        {

            int x1 = circle1.getX();
            int y1 = circle1.getY();
            int width = getWidth();
            int height = getHeight();
            int radius1 = circle1.getRadius();

            //first circle

            if(x1 - radius1 <= 0)
            {
                p2Score++;
                circle1.setX(getWidth()/2);
                circle1.setY(getHeight()/2);
            }
            if(x1 + radius1 >= width)
            {
                p1Score++;

                circle1.setX(getWidth()/2);
                circle1.setY(getHeight()/2);
            }
            if(y1 - radius1 <= 0 || y1 + radius1 >= height )
            {
                circle1.turnY();

            }

            if((x1 - radius1 <= p1X) || (x1 + radius1 >= p2X))
            {
                circle1.turnX();
            }

            circle1.move();
            repaint();
        }

        public void GameOver()
        {
            playing = false;
        }



    }

    enum DirectionP1 {
           W(KeyEvent.VK_W), S(KeyEvent.VK_S);

           private int key;

           private DirectionP1(int key) {
              this.key = key;
           }

           public int getKey() {
              return key;
           }
        }
    enum DirectionP2 {
           UP(KeyEvent.VK_UP), DOWN(KeyEvent.VK_DOWN);

           private int key;

           private DirectionP2(int key) {
              this.key = key;
           }

           public int getKey() {
              return key;
           }
        }

    class MyKeyAction extends AbstractAction {
           private DirectionP1 direction;

           public MyKeyAction(DirectionP1 direction) {

              this.direction = direction;
           }

           @Override
           public void actionPerformed(ActionEvent e) {
               switch (direction) {
                  case W:
                        p1Y -= 6; 
                     break;
                  case S:
                        p1Y += 6; 
                     break;

                  default:
                     break;
                  }
           }
        }
    class MyKeyAction2 extends AbstractAction {
           private DirectionP2 direction2;

           public MyKeyAction2(DirectionP2 direction2) {

              this.direction2 = direction2;
           }

           @Override
           public void actionPerformed(ActionEvent e) {

               switch (direction2) {
                  case UP:
                        p2Y -= 6; 
                     break;
                  case DOWN:
                        p2Y += 6; 
                     break;

                  default:
                     break;
                  }
           }
        }
}

Ответы на вопрос(1)

Ваш ответ на вопрос