形状增加而不是移动 - Java(GUI 小程序)
Shape increases in size instead of moving - Java (GUI applet)
我正在尝试更改矩形的 y 位置,但是,每当我尝试这样做时,它都会垂直扩展/变大 .
public class PlayerPaddle implements Paddle {
double yVelocity;
final double GRAVITY = 0.94;
//move up/down faster (if not accelerating = slow down)
boolean upAccel, downAccel;
//determines if player 1 or player 2 (on left or on right)
int player;
//position of actual paddle
int x;
double y;
Rectangle panel;
public PlayerPaddle(int player) {
upAccel = false;
downAccel = false;
y = 210; //not moving initially
yVelocity = 0;
if (player == 1) {
//left side
x = 20;
} else {
//right side
x = 660;
}
}
@Override
public void draw(Graphics g) {
//draw paddle
g.setColor(Color.WHITE);
g.fillRect(x, (int) y, 20, 80);
}
@Override
public void move() {
if (upAccel) {
yVelocity -= 2;
} else if (downAccel) {
yVelocity += 2;
} else if ((!upAccel) && (!downAccel)) {
yVelocity *= GRAVITY;
}
y += yVelocity; //changes y position of paddle
}
public void setUpAccel(boolean input) {
upAccel = input;
}
public void setDownAccel(boolean input) {
downAccel = input;
}
@Override
public int getY() {
return (int) y;
}
}
我想知道如何让矩形垂直上下移动。一个类似的问题只有一个答案说之前绘制的矩形没有被清除,因此正在扩大。但是即使我说 g.clearRect(...) 它仍然会扩展并且不会移动。
我是 Swing 和 Awt 的新手,但我真的致力于学习。 感谢您的帮助。
A similar question had only one answer which said that the previously painted rectangle was not being cleared and as a result is expanding
这可能仍然是问题所在。
在某处,而不是在此处提供的代码中,您需要调用此 class 的 draw(...)
方法。
所以在该代码中,您需要确保在绘制桨之前清除组件的背景。由于您应该覆盖面板的 paintComponent(...)
方法来进行自定义绘制,因此您的代码应该类似于:
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
// draw the paddle
}
阅读有关 Custom Painting 的 Swing 教程部分,了解更多信息和工作示例。
I am new to Swing and Awt but I am really committed to learning.
然后将 link 本教程放在手边,以了解所有 Swing 基础知识。
我正在尝试更改矩形的 y 位置,但是,每当我尝试这样做时,它都会垂直扩展/变大 .
public class PlayerPaddle implements Paddle {
double yVelocity;
final double GRAVITY = 0.94;
//move up/down faster (if not accelerating = slow down)
boolean upAccel, downAccel;
//determines if player 1 or player 2 (on left or on right)
int player;
//position of actual paddle
int x;
double y;
Rectangle panel;
public PlayerPaddle(int player) {
upAccel = false;
downAccel = false;
y = 210; //not moving initially
yVelocity = 0;
if (player == 1) {
//left side
x = 20;
} else {
//right side
x = 660;
}
}
@Override
public void draw(Graphics g) {
//draw paddle
g.setColor(Color.WHITE);
g.fillRect(x, (int) y, 20, 80);
}
@Override
public void move() {
if (upAccel) {
yVelocity -= 2;
} else if (downAccel) {
yVelocity += 2;
} else if ((!upAccel) && (!downAccel)) {
yVelocity *= GRAVITY;
}
y += yVelocity; //changes y position of paddle
}
public void setUpAccel(boolean input) {
upAccel = input;
}
public void setDownAccel(boolean input) {
downAccel = input;
}
@Override
public int getY() {
return (int) y;
}
}
我想知道如何让矩形垂直上下移动。一个类似的问题只有一个答案说之前绘制的矩形没有被清除,因此正在扩大。但是即使我说 g.clearRect(...) 它仍然会扩展并且不会移动。
我是 Swing 和 Awt 的新手,但我真的致力于学习。 感谢您的帮助。
A similar question had only one answer which said that the previously painted rectangle was not being cleared and as a result is expanding
这可能仍然是问题所在。
在某处,而不是在此处提供的代码中,您需要调用此 class 的 draw(...)
方法。
所以在该代码中,您需要确保在绘制桨之前清除组件的背景。由于您应该覆盖面板的 paintComponent(...)
方法来进行自定义绘制,因此您的代码应该类似于:
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
// draw the paddle
}
阅读有关 Custom Painting 的 Swing 教程部分,了解更多信息和工作示例。
I am new to Swing and Awt but I am really committed to learning.
然后将 link 本教程放在手边,以了解所有 Swing 基础知识。