Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果在Java中按下并按住某个键,您会有什么感觉?_Java - Fatal编程技术网

如果在Java中按下并按住某个键,您会有什么感觉?

如果在Java中按下并按住某个键,您会有什么感觉?,java,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 j

我一直在做一个单人乒乓球游戏,你现在用鼠标控制球拍。我已将关键点侦听器添加到帧中,您可以通过敲击“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;
    }
}
import java.applet.*;
导入java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
导入java.awt.geom.*;
导入java.util.concurrent.*;
导入java.util.*;
公共课堂实验扩展JApplet{
公共静态最终整数宽度=BallRoom.WIDTH;
公共静态最终内部高度=舞厅高度;
公共漆面帆布;
公共void init()
{
此.setSize(宽度、高度);
canvas=新的PaintSurface();
添加(画布、边框布局、中心);
canvas.requestFocusInWindow();
ScheduledThreadPoolExecutor executor=新的ScheduledThreadPoolExecutor(3);
executor.scheduleAtFixedRate(新的AnimationThread(this),0L,20L,时间单位为毫秒);
}
}
类AnimationThread实现Runnable
{
雅普莱特c;
公共动画线程(JApplet c)
{
这个.c=c;
}
公开募捐
{
c、 重新油漆();
}
}
类PaintSurface扩展JComponent实现KeyListener
{
int-paile_x=0;
int桨_y=360;
公共住宅区;
公共场所;
智力得分=0;
浮动英语=1.0f;
球;
Color[]Color={Color.RED,Color.ORANGE,Color.洋红,Color.ORANGE,Color.CYAN,Color.BLUE};
布尔aheld=false;
布尔dheld=假;
int颜色指数;
@凌驾
public void keyTyped(KeyEvent e)
{
System.out.println(“按下”+e.getKeyChar());
}
@凌驾
按下公共无效键(按键事件e)
{
如果(例如getKeyChar()=='d')
桨叶_x+=10;
如果(例如getKeyChar()=='a')
桨叶_x-=10;
}
@凌驾
公共无效密钥已释放(密钥事件e)
{
}
公共漆面()
{
addMouseMotionListener(新的MouseMotionAdapter()
{
public void mouseMoved(MouseEvent e)
{
如果(如getX()-30-桨叶>5)
英语=1.5f;
否则,如果(e.getX()-30-桨叶x<5)
英语=-1.5f;
其他的
英语=1.0f;
桨_x=e.getX()-30;
}
});
addKeyListener(此);
球=新球(20);
}
公共空间涂料(图g)
{
图形2d g2=(图形2d)g;
形状桨=新矩形2D。浮动(桨x,桨y,60,8);
g2.设置颜色(颜色[颜色索引%6]);
如果(球相交(桨x、桨y、60、8)和球y速度>0)
{           
ball.y_speed=-ball.y_speed*1.1;
ball.x_速度=ball.x_速度*1.1;
如果(英语!=1.0f)
{
彩色索引++;
}
分数+=数学绝对值(球x_速度*10);
}
if(ball.getY()+ball.getHeight()>=舞厅高度)
{
球=新球(20);
分数-=1000;
颜色指数=0;
}
ball.move();
g2.填充(球);
g2.设置颜色(颜色为黑色);
g2.填充(叶片);
g2.抽绳(“分数:+分数,250,20);
}
}
类球延伸椭圆2。浮动{
公共双x_速度,y_速度;
私人int d;
私人内部宽度=舞厅宽度;
私人内部高度=舞厅高度;
公共球(内径)
{
超级((int)(Math.random()*(BallRoom.WIDTH-20)+1),0,直径,直径;
d=直径;
这个.x_速度=(int)(Math.random()*5)+1;
this.y_speed=(int)(Math.random()*5)+1;
}
公开作废动议()
{       
if(super.x<0 | | super.x>width-d)
x_速度=-x_速度;
if(super.y<0 | | super.y>height-d)
y_速度=-y_速度;
super.x+=x_速度;
super.y+=y_速度;
}
}

运行将以下代码用于KeyPressed()和KeyReleased()


然后让方法在计时器上运行,如果dheld或aheld为真,则移动拨片。

线索在keylistener中;按键和按键释放。很抱歉,我似乎无法让它工作。我试图使它在aheld或dheld为真时移动拨片,但实际上它不会移动拨片。请解释一下在计时器上运行的方法是什么意思?使用和ActionListener。
@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;
    }