如何在Java中按下并保持键是什么意思?

时间:2014-07-29 16:06:32

标签: java

我一直在制作单人乒乓球游戏,你现在用它来控制球拍。我已经在框架上添加了关键的听众,你可以通过点击“a”和“d”键来移动球拍,但你不能将它们按住。这是我现在的代码,我很感激您提供的任何帮助!

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.*;
import java.util.concurrent.*;
import java.util.*;

public class Experiment extends JApplet {

    public static final int WIDTH = BallRoom.WIDTH;
    public static final int HEIGHT = BallRoom.HEIGHT;


    public PaintSurface canvas;

    public void init()
    {
        this.setSize(WIDTH, HEIGHT);
        canvas = new PaintSurface();
        this.add(canvas, BorderLayout.CENTER);
        canvas.requestFocusInWindow();
        ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(3);
        executor.scheduleAtFixedRate(new AnimationThread(this), 0L, 20L, TimeUnit.MILLISECONDS);

    }
}



class AnimationThread implements Runnable
{
    JApplet c;
    public AnimationThread(JApplet c)
    {
        this.c = c;
    }

    public void run()
    {
        c.repaint();
    }
}

class PaintSurface extends JComponent implements KeyListener
{
    int paddle_x = 0;
    int paddle_y = 360;
    public boolean aDown;
    public boolean dDown;


    int score = 0;
    float english = 1.0f;
    Ball ball;
    Color[] color = {Color.RED, Color.ORANGE, Color.MAGENTA, Color.ORANGE, Color.CYAN, Color.BLUE};
    boolean aheld = false;
    boolean dheld = false;

    int colorIndex;

        @Override
        public void keyTyped(KeyEvent e)
        {
            System.out.println("Pressed " + e.getKeyChar());
        }

        @Override
        public void keyPressed(KeyEvent e)
        {
            if (e.getKeyChar() == 'd')
                paddle_x += 10;
            if (e.getKeyChar() == 'a')
                paddle_x -= 10;
        }
        @Override
        public void keyReleased(KeyEvent e)
        {

        }


    public PaintSurface()
    {
        addMouseMotionListener(new MouseMotionAdapter()
        {
            public void mouseMoved(MouseEvent e)
            {
                if (e.getX() - 30 - paddle_x > 5)
                    english = 1.5f; 
                else if (e.getX() - 30 - paddle_x < 5)
                    english = -1.5f;
                else
                    english = 1.0f;
                paddle_x = e.getX() - 30;
            }
        });

        addKeyListener(this);


        ball = new Ball(20);
    }


    public void paint(Graphics g)
    {
        Graphics2D g2 = (Graphics2D)g;
        Shape paddle = new Rectangle2D.Float(paddle_x, paddle_y, 60, 8);

        g2.setColor(color[colorIndex % 6]);

        if (ball.intersects(paddle_x, paddle_y, 60, 8) && ball.y_speed  > 0)
        {           
            ball.y_speed = -ball.y_speed * 1.1;
            ball.x_speed = ball.x_speed * 1.1;


            if (english != 1.0f)
            {
                colorIndex++;
            }
            score += Math.abs(ball.x_speed * 10);
        }

        if (ball.getY() + ball.getHeight() >= BallRoom.HEIGHT)
        {
            ball = new Ball(20);
            score -= 1000;
            colorIndex = 0;
        }
        ball.move();
        g2.fill(ball);

        g2.setColor(Color.BLACK);
        g2.fill(paddle);
        g2.drawString("Score: " + score, 250, 20);
    }
}

class Ball extends Ellipse2D.Float{
    public double x_speed, y_speed;
    private int d;
    private int width = BallRoom.WIDTH;
    private int height = BallRoom.HEIGHT;

    public Ball(int diameter)
    {
        super((int) (Math.random() * (BallRoom.WIDTH - 20) + 1), 0, diameter, diameter);
        this.d = diameter;
        this.x_speed = (int) (Math.random() * 5) + 1;
        this.y_speed = (int) (Math.random() * 5) + 1;
    }

    public void move()
    {       
        if (super.x < 0 || super.x > width - d)
            x_speed = -x_speed;
        if (super.y < 0 || super.y > height - d)
            y_speed = -y_speed;

        super.x += x_speed;
        super.y += y_speed;
    }
}

1 个答案:

答案 0 :(得分:0)

running使用KeyPressed()和KeyReleased()

的以下代码
@Override
    public void keyPressed(KeyEvent e)
    {
        if (e.getKeyChar() == 'd')
            dheld = true;
        if (e.getKeyChar() == 'a')
            aheld = true;
    }
    @Override
    public void keyReleased(KeyEvent e)
    {
        if (e.getKeyChar() == 'd')
            dheld = false;
        if (e.getKeyChar() == 'a')
            aheld = false;
    }

然后让一个方法在一个定时器上运行,如果dheld或者aheld为真,它会移动paddle。