gpt4 book ai didi

java - 如何在单独的 java 类中包含关键监听器

转载 作者:太空宇宙 更新时间:2023-11-04 06:38:22 26 4
gpt4 key购买 nike

我已经研究单人乒乓球游戏有一段时间了,我已经成功地通过使用鼠标控制 Racket 来使代码正常工作。但是,我想弄清楚如何在该程序中包含一个关键监听器,以便我可以添加第二个桨以使其成为多人游戏。这是我现在拥有的代码:

主要类(还包含球类):

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);

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;

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

int colorIndex;

@Override
public void keyTyped(KeyEvent e)
{
System.out.println("Pressed" + e.getKeyChar()); //This doesn't show up when I type a key
}

@Override
public void keyPressed(KeyEvent e)
{

}
@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;
}
});




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.*;
import java.awt.*;
import javax.swing.*;
import java.awt.geom.*;
import java.util.concurrent.*;

public class BallRoom extends JApplet{
public static final int WIDTH = 400;
public static final int HEIGHT = 400;
public void init()
{
this.setSize(WIDTH, HEIGHT);
this.setVisible(true);
}
}

Run class:


public class RunExperiment {
public static void main(String[] args)
{
Experiment exp = new Experiment();

}
}

当我运行此代码时,打印语句不会执行,这使我相信它从未识别出该键被单击。我将不胜感激任何人能给我的任何帮助。

最佳答案

您需要在具有键监听器的组件上的某个位置调用 addKeyListener

在您的情况下,您很可能想在 PaintSurface 构造函数中说 addKeyListener(this)

关于java - 如何在单独的 java 类中包含关键监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24997301/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com