如何使图形只在JFrame内移动,而不是在JFrame的边界外?



我目前正在开发一款Catch the Falling Object类型的游戏,但现在的问题是玩家(又名捕获对象的人)。当我移动到左侧并触及窗口边界时,它将玩家发送到x - 1,但当我移动到右侧时(尽管我将代码设置为if (x > windowBoundRight){ x = x - 1 },但它仍然移动到窗口边界之外)。有人能帮我吗?(注:没有错误btw)

这是代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Game extends JPanel implements ActionListener, KeyListener {
public int x = 0, y = 0, velx = 0, vely = 0;
public boolean hitAnObject = false;
public Color c = Color.BLACK;
Timer timer = new Timer(5, this);

public Game() {
timer.start();
setBackground(Color.CYAN);
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
}
@Override
public void paint(Graphics g){
super.paint(g);
Graphics2D g2 = (Graphics2D) g;
g2.fillRect(x, y, 50, 50);
g.drawString("X = " + x + "Y = " + y, 0, 0);
}
public void actionPerformed(ActionEvent ae) {
repaint();
x += velx;
y += vely;
}
public void left() {
velx = -5;
vely = vely + 0;
}
public void right() {
velx = 5;
vely = vely + 0;
}
public void up() {
vely = -5;
velx = velx + 0;
}
public void down() {
vely = 5;
velx = velx + 0;
}
public void stopMovement_UP() {
vely = 0;
velx = velx + 0;
}
public void stopMovement_DOWN() {
vely = 0;
velx = velx + 0;
}
public void stopMovement_LEFT() {
velx = 0;
vely = vely + 0;
}
public void stopMovement_RIGHT() {
velx = 0;
vely = vely + 0;
}
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_LEFT || keyCode == KeyEvent.VK_A) {
left();
if (x < 0) {
x = 0;
}
}
else if (keyCode == KeyEvent.VK_RIGHT || keyCode == KeyEvent.VK_D) {
right();
//checck if the graphic aka player moves out of the boundary
}
else if (keyCode == KeyEvent.VK_DOWN || keyCode == KeyEvent.VK_S) {
down();
}
else if (keyCode == KeyEvent.VK_UP || keyCode == KeyEvent.VK_W) {
up();
if (y < 0) {
y = 0;
}
}
}
@Override
public void keyTyped(KeyEvent e) { }
@Override
public void keyReleased(KeyEvent e) { 
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_LEFT || keyCode == KeyEvent.VK_A) {
stopMovement_LEFT();
if (x < 0) {
x = 0;
}
}
else if (keyCode == KeyEvent.VK_RIGHT || keyCode == KeyEvent.VK_D) {
stopMovement_RIGHT();
if (x > 436) {
x = x + 2;
}
}
else if (keyCode == KeyEvent.VK_DOWN || keyCode == KeyEvent.VK_S) {
stopMovement_DOWN();
}
else if (keyCode == KeyEvent.VK_UP || keyCode == KeyEvent.VK_W) {
stopMovement_UP();
if (y < 0) {
y = 0;
}
}

}
public static void main(String[] args) {
Game game = new Game();
game.getBackground();
}
}

actionPerformed方法中,您正在更改xy的值,但您没有检查计算值是否在Game面板的范围内。

paint方法中(在Game类中),xy坐标位于面板的坐标空间内,即GameGame左边缘的x坐标为0(零)。右边缘的x坐标是Game的宽度,您可以通过getWidth方法检索。

同样,Game的上边缘的y坐标为零,而下边缘的y坐标是Game的高度,可以通过getHeight方法检索。

因此,将actionPerformed方法中xy变量的计算值与Game的宽度和高度进行比较,看它们是否在Game的边界之外。

public void actionPerformed(ActionEvent ae) {
x += velx;
if (x > getWidth() || x < 0) {
// 'x' is outside of the bounds
}
y += vely;
if (y > getHeight() || y < 0) {
// 'y' is outside of the bounds
}
repaint();
}
我认为你应该在之后调用repaint方法您已经计算了xy的新值(而不是之前,如在您的问题中的代码中)。

我不知道你想让玩家在碰到Game的一个边时如何移动,所以我只在上面代码的if条件下写了一个注释。

如果你还没有这样做,我建议你阅读用Swing创建GUIOracle Java教程的执行自定义绘画课。

相关内容

  • 没有找到相关文章

最新更新