为什么keyListener不起作用? [英] Why isn't the keyListener working?

查看:161
本文介绍了为什么keyListener不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我制作的乒乓球游戏,但是唯一不起作用的是桨在运动.我不确定如何使键盘与程序一起使用.这是主要游戏的代码:

This is a pong game I made, but the only thing that is not working is the paddles moving. I am not sure how to make the keyboard work with the program. Here is the code for the main game:

//Project: A program where you play the game pong

//This module defines the properties of the user interface

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;


public class pongframe extends JFrame
{
    private JLabel playerlefttitle;
    private JLabel playerrighttitle;
    private JLabel speedtitle;
    private JTextField speedfield;
    private JTextField playerleftscorefield;
    private JTextField playerrightscorefield;
    private JButton newgame;
    private JButton gobutton;
    private JButton pausebutton;
    private JButton quitgame;
    private Timer systemclock;
    private Timer paddleclock;
    private String speedstring;
    private int speed;
    private int leng = 10;
    private int delayinterval;
    private float millisecondpersecond = 1000.0f;
    private boolean win = false;
    private int playerleftscore=0;
    private int playerrightscore=0;
    private boolean rightup = false;
    private boolean rightdown = false;
    private boolean leftup = false;
    private boolean leftdown = false;
    private boolean barhit = true;
    private double direction=0;
    private pongoperations graphicalpanel;
    private Clockhandlerclass clockhandler;
    private PaddleClockHandlerClass paddleclockhandler;
    private PaddleHandlerClass paddlehandler;

    public pongframe()
    {
        super("Pong");
        JPanel panel1 = new JPanel(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();

        graphicalpanel = new pongoperations();
        graphicalpanel.setBackground(Color.GREEN);
        add(graphicalpanel, BorderLayout.CENTER);

        add(panel1, BorderLayout.SOUTH);
        c.insets = new Insets(0,0,10,10);
        newgame = new JButton("New");
        c.gridx = 0;
        c.gridy = 1;
        panel1.add(newgame, c);
        speedtitle = new JLabel("Speed");
        c.gridx = 1;
        c.gridy = 0;
        panel1.add(speedtitle, c);
        speedfield = new JTextField(3);
        c.gridx = 1;
        c.gridy = 1;
        panel1.add(speedfield, c);
        quitgame = new JButton("Quit");
        c.gridx = 2;
        c.gridy = 1;
        panel1.add(quitgame, c);
        playerlefttitle = new JLabel("Player Left");
        c.gridx = 0;
        c.gridy = 3;
        panel1.add(playerlefttitle, c);
        playerrighttitle = new JLabel("Player Right");
        c.gridx = 3;
        c.gridy = 3;
        panel1.add(playerrighttitle, c);
        playerleftscorefield = new JTextField(2);
        playerleftscorefield.setEditable(false);
        c.gridx = 0;
        c.gridy = 4;
        panel1.add(playerleftscorefield, c);
        gobutton = new JButton("Go");
        c.gridx = 1;
        c.gridy = 4;
        panel1.add(gobutton, c);
        pausebutton = new JButton("Pause");
        c.gridx = 2;
        c.gridy = 4;
        panel1.add(pausebutton, c);
        playerrightscorefield = new JTextField(2);
        playerrightscorefield.setEditable(false);
        c.gridx = 3;
        c.gridy = 4;
        panel1.add(playerrightscorefield, c);
        buttonhandler myhandler = new buttonhandler();
        newgame.addActionListener(myhandler);
        gobutton.addActionListener(myhandler);
        pausebutton.addActionListener(myhandler);
        quitgame.addActionListener(myhandler);
        clockhandler = new Clockhandlerclass();
        paddleclockhandler = new PaddleClockHandlerClass();
        paddlehandler = new PaddleHandlerClass();
    }
    private class buttonhandler implements ActionListener
    {
        public void actionPerformed(ActionEvent event)
        {
            graphicalpanel.reset();
            if(event.getSource() == newgame)
            {
                speedfield.setText("");
                playerleftscore = 0;
                playerrightscore = 0;
                playerleftscorefield.setText("0");
                playerrightscorefield.setText("0");
                graphicalpanel.reset();
                graphicalpanel.repaint();
                gobutton.setEnabled(true);
                speedfield.setEditable(true);
                newgame.setEnabled(false);
            }
            else if(event.getSource() == gobutton)
            {
                speedstring = speedfield.getText();
                leng = speedstring.length();
                if(leng == 0)
                    speed = 0;
                else 
                {
                    speed = Integer.parseInt(speedstring);
                    gobutton.setEnabled(false);
                    speedfield.setEditable(false);
                }
                newgame.setEnabled(false);
                delayinterval = (int)Math.floor(millisecondpersecond/speed + 0.5f);
                systemclock = new Timer(delayinterval, clockhandler);
                systemclock.start();
                paddleclock = new Timer(delayinterval, paddleclockhandler);
                paddleclock.start();
            }
            else if(event.getSource() == pausebutton)
            {
                gobutton.setEnabled(true);
                newgame.setEnabled(true);
                systemclock.stop();
            }
            else if(event.getSource() == quitgame)
            {
                System.exit(0);
            }
            else
                System.out.println("Unkown error");
        }
    }
    private class Clockhandlerclass implements ActionListener
    {
        public void actionPerformed(ActionEvent event)
        {
            if(event.getSource() == systemclock)
            {
                graphicalpanel.grabFocus();
                graphicalpanel.moveball(speed,delayinterval, direction, barhit);
                direction = graphicalpanel.changedirection(direction);

                if(win == false)
                {
                    barhit = graphicalpanel.barhits(barhit);
                    graphicalpanel.repaint();
                    win = graphicalpanel.winner();
                    playerleftscore = graphicalpanel.playerleftwin(playerleftscore);
                    playerrightscore = graphicalpanel.playerrightwin(playerrightscore);
                    playerleftscorefield.setText(Integer.toString(playerleftscore));
                    playerrightscorefield.setText(Integer.toString(playerrightscore));
                }
                else if(win == true)
                {
                    graphicalpanel.reset();
                    win = false;
                    systemclock.restart();
                    if(playerleftscore == 10)
                    {
                        newgame.setEnabled(true);
                        systemclock.stop();
                    }
                    else if(playerrightscore == 10)
                    {
                        newgame.setEnabled(true);
                        systemclock.stop();
                    }
                    graphicalpanel.repaint();
                }   
            }
            else
                System.out.printf("%s\n", "There is a bug in the clock.");
        }
    }
    private class PaddleClockHandlerClass implements ActionListener
    {
        public void actionPerformed(ActionEvent event)
        {
            if(event.getSource() == paddleclock)
            {
                graphicalpanel.moveplayer1(rightup, rightdown);
                graphicalpanel.moveplayer2(leftup, leftdown);
                graphicalpanel.repaint();
            }
        }
    }
    private class PaddleHandlerClass implements KeyListener
    {
        public void keyTyped(KeyEvent event)
        {
        }
        public void keyPressed(KeyEvent event)
        {
            if(event.getKeyCode() == KeyEvent.VK_UP)
            {
                rightup = true;
                System.out.println("Up");
            }
            else if(event.getKeyCode() == KeyEvent.VK_DOWN)
                rightdown = true;
            if(event.getKeyCode() == KeyEvent.VK_W)
                leftup = true;
            else if(event.getKeyCode() == KeyEvent.VK_S)
                leftdown = true;
        }
        public void keyReleased(KeyEvent event)
        {
            if(event.getKeyCode() == KeyEvent.VK_UP)
                rightup = false;
            else if(event.getKeyCode() == KeyEvent.VK_DOWN)
                rightdown = false;
            if(event.getKeyCode() == KeyEvent.VK_W)
                leftup = false;
            else if(event.getKeyCode() == KeyEvent.VK_S)
                leftdown = false;
        }
    }
}   

下面的代码是我在游戏中使用的功能:

This next bit of code are the functions that I am using for the game:

//Project: A program where you play the game pong

//This module contains the graphics and algorithms used by this project.

import java.awt.Graphics;
import javax.swing.JPanel;
import java.awt.Color;

public class pongoperations extends JPanel
{
    int widthofgraphicalarea;
    int heightofgraphicalarea;
    final int ballradius = 8;
    int ballxcoordinate;
    double ballycoordinate;
    int paddle1xcoordinate;
    int paddle1ycoordinate;
    int paddle2xcoordinate;
    int paddle2ycoordinate;

    public void paintComponent(Graphics graphicarea)
    {//function to paint the area
        super.paintComponent(graphicarea);
        this.setBackground(Color.GREEN);
        widthofgraphicalarea = getWidth();
        heightofgraphicalarea = getHeight();
        //create paddle 1
        graphicarea.setColor(Color.BLUE);
        graphicarea.fillRect(paddle1xcoordinate, paddle1ycoordinate, 10, 50);
        //create paddle 2
        graphicarea.setColor(Color.RED);
        graphicarea.fillRect(paddle2xcoordinate, paddle2ycoordinate, 10, 50);
        //Give the ball a white color
        graphicarea.setColor(Color.WHITE);
        //The 3rd and 4th parameters below represent the diameter of the ball.
        graphicarea.fillOval(ballxcoordinate,(int)ballycoordinate,2*ballradius,2*ballradius);
    }//End of method paintComponent
    public void moveplayer1(boolean rightup, boolean rightdown)
    {
        if(rightup == true)
        paddle1ycoordinate -= 5;
        else if(rightdown == true)
        paddle1ycoordinate += 5;
        positionPlayer1(paddle1xcoordinate, paddle1ycoordinate);
    }
    public void moveplayer2(boolean leftup, boolean leftdown)
    {
        if(leftup == true)
        paddle2ycoordinate -= 5;
        else if(leftdown == true)
        paddle2ycoordinate += 5;
        positionPlayer2(paddle2xcoordinate, paddle2ycoordinate);
    }
    public void positionPlayer1(int x, int y)
    {
        this.paddle1xcoordinate = x;
        this.paddle1ycoordinate = y;
        repaint();
    }
    public void positionPlayer2(int x, int y)
    {
        this.paddle2xcoordinate = x;
        this.paddle2ycoordinate = y;
        repaint();
    }
    public void moveball(int speed, int delayinterval, double direction, boolean barhit)
    {
        if(barhit == true)
            ballxcoordinate+=1;
        else if(barhit == false)
            ballxcoordinate-=1;
        if(direction > 0)
            ballycoordinate = ballycoordinate + (double)Math.sin(Math.toRadians(direction));
        else if(direction < 0)
            ballycoordinate = ballycoordinate + (double)Math.sin(Math.toRadians(direction));
    }
    public boolean barhits(boolean barhit)
    {
        if(ballxcoordinate == paddle1xcoordinate+10 && (ballycoordinate >= paddle1ycoordinate && ballycoordinate <= paddle1ycoordinate+50))
            barhit = true;
        else if(ballxcoordinate+2*ballradius == paddle2xcoordinate && (ballycoordinate+2*ballradius >= paddle2ycoordinate && ballycoordinate+2*ballradius <= paddle2ycoordinate+50))
            barhit = false;
        return barhit;
    }
    public boolean winner()
    {
        if(ballxcoordinate+2*ballradius >= widthofgraphicalarea)
            return true;
        else if(ballxcoordinate+2*ballradius <= 16)
            return true;

        return false;
    }
    public int playerleftwin(int playerleftscore)
    {
        if(ballxcoordinate+2*ballradius >= widthofgraphicalarea)
            playerleftscore+=1;

        return playerleftscore;
    }
    public int playerrightwin(int playerrightscore)
    {
        if(ballxcoordinate+2*ballradius <= 16)
            playerrightscore+=1;

        return playerrightscore;
    }
    public double changedirection(double direction)
    {//function to change the direction of the ball to bounce of walls
        if((int)ballycoordinate+2*ballradius >= heightofgraphicalarea || (int)ballycoordinate+2*ballradius < 16)
            direction = -direction;
        return direction;
    }
    public void reset()
    {//reset the ball at middle of left side
        ballxcoordinate = widthofgraphicalarea/2 - ballradius;
        ballycoordinate = heightofgraphicalarea/2 - ballradius;
        paddle1xcoordinate = 10;
        paddle1ycoordinate = heightofgraphicalarea/2-25;
        paddle2xcoordinate = widthofgraphicalarea-20;
        paddle2ycoordinate = heightofgraphicalarea/2-25;
    }
}

请有人帮助.预先感谢.

Please someone help. Thanks in advance.

另一个可以帮助的问题是,一旦完成,如何将其切换为applet形式?

Another question if you can help is once this is finished how do I switch this over to applet form?

推荐答案

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆