0

I am making a Pong program, and I have a start button that begins to draw everything, and quite literally to get the ball rolling (you're welcome for the pun). Anyways, when I hit the start button, a Key Listener to move the paddles won't work unless I click the mouse somewhere on the canvas to give it priority. Is there some sort of code to automatically "click" on the canvas without the user being hassled to do so? Thanks in advance.

This Is running awt by the way. I realize I should learn swing, but never got around to it.

public class Pong extends Applet implements ActionListener, KeyListener
{
    Canvas c1;
    Graphics myG;
    Button start;

    ball ball;
    paddle LPaddle;
    paddle RPaddle;

    public void init()
    {
        this.setSize(1300,700);

        c1 = new Canvas();
        add(c1);
        c1.addKeyListener(this);
        c1.setBackground(Color.pink);

        start = new Button("Start");
        add(start);
        start.addActionListener(this);


        ball = new ball();

        LPaddle = new paddle();
        RPaddle = new paddle();

        myG = c1.getGraphics();


    }

    public void paint(Graphics g)
    {
        c1.setLocation(0,0);
        c1.setSize(1251,700);

        start.setLocation(1255,350);
        start.setSize(40,20);
    }


    public void keyPressed(KeyEvent e) 
    {
        if(e.getKeyCode()==KeyEvent.VK_UP)//up
        {
            RPaddle.erasePaddle(myG);
            RPaddle.movePaddleUp();
            RPaddle.drawPaddle(myG);
        }
        if(e.getKeyCode()==KeyEvent.VK_DOWN)//down
        {
            RPaddle.erasePaddle(myG);
            RPaddle.movePaddleDown();
            RPaddle.drawPaddle(myG);
        }
        if(e.getKeyCode()==KeyEvent.VK_W)
        {
            LPaddle.erasePaddle(myG);
            LPaddle.movePaddleUp();
            LPaddle.drawPaddle(myG);
        }
        if(e.getKeyCode()==KeyEvent.VK_S)
        {
            LPaddle.erasePaddle(myG);
            LPaddle.movePaddleDown();
            LPaddle.drawPaddle(myG);
        }
        if(e.getKeyCode()==KeyEvent.VK_ENTER)
        {
            myG.drawLine(625,0,625,700);
            LPaddle.setInitial(150,0,350);
            RPaddle.setInitial(150,1250,350);

            LPaddle.drawPaddle(myG);
            RPaddle.drawPaddle(myG);
        }
    }

    public void keyReleased(KeyEvent e) 
    {

    }

    public void keyTyped(KeyEvent e) 
    {

    }



    public void actionPerformed(ActionEvent e) 
    {
        if(e.getSource()==start)
        {
            myG.drawLine(625,0,625,700);
            LPaddle.setInitial(150,0,350);
            RPaddle.setInitial(150,1250,350);

            LPaddle.drawPaddle(myG);
            RPaddle.drawPaddle(myG);
        }
    }
Jarrod
  • 9,349
  • 5
  • 58
  • 73
Chris
  • 3
  • 3

1 Answers1

1

KeyListeners are low level interfaces which have a major, significant draw back: The componet they are registered to must be focusable and be focused.

By clicking the start button, you are giving the button focus.

You could call requestFocusInWindow on the instance of the canvas, but this assumes that the the canvas is focusable in the first place.

If you can, you'd be better of using a JComponent/JPanel as the base for your game canvas and use the key bindings API.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366