2D游戏中的Java swing渲染现象
我目前正在做我的第一个跳跃跑游戏。最初的部分已经可以正常工作了,但我仍然会遇到一个“bug”,当我的“colums”移动时会出现。只有当“移动速度”为1时,才会发生这种情况。我通过更改以下内容将其固定:2D游戏中的Java swing渲染现象,java,swing,rendering,2d-games,Java,Swing,Rendering,2d Games,我目前正在做我的第一个跳跃跑游戏。最初的部分已经可以正常工作了,但我仍然会遇到一个“bug”,当我的“colums”移动时会出现。只有当“移动速度”为1时,才会发生这种情况。我通过更改以下内容将其固定: private void recycleColums() { if (cList.get(0).getX() + cList.get(0).getWidth() <= 0) { cList.remove(0); cList.add(7, new Column(7 + 1
private void recycleColums() {
if (cList.get(0).getX() + cList.get(0).getWidth() <= 0) {
cList.remove(0);
cList.add(7, new Column(7 + 1, false, cList.get(6).getX()));
frame.add(cList.get(7));
}
}
到底是什么问题?什么是错误?这些列不应该在那里吗?问题是我的列之间的这些小间隙。(我在图中用蓝色箭头标记了它们)所以当你说“列”时,你是指黑色和绿色的瓷砖/地吗?确切地说,我不知道如何称呼它们,否则^^^必须在AWT事件调度线程中创建和操作Swing对象。您正在另一个线程中修改它们,这就是您看到奇怪行为的原因。见和。
...
public class Column extends JPanel {
JPanel floor;
JPanel grass;
Random rand = new Random();
private static final long serialVersionUID = 1L;
public final static int WIDTH = Board.WIDTH / 6;
public final int HEIGHT = Board.HEIGHT;
private int position;
public final static int FLOOR_H = WIDTH;
public Column(int pos, boolean init) {
this.position = pos;
setBounds((WIDTH * position) - WIDTH, 0, WIDTH, HEIGHT);
setLayout(null);
setBackground(Color.WHITE);
floor = new JPanel();
floor.setLayout(null);
floor.setBackground(Color.BLACK);
if (init) {
floor.setBounds(0, HEIGHT - FLOOR_H, WIDTH, FLOOR_H);
} else {
floor.setBounds(0,
(HEIGHT - (FLOOR_H + (FLOOR_H * rand.nextInt(2) / 2))),
WIDTH, FLOOR_H * 2);
}
grass = new JPanel();
grass.setBounds(0, 0, WIDTH, 10);
grass.setBackground(Color.GREEN);
floor.add(grass);
add(floor);
}
}
private void recycleColums() {
if (cList.get(0).getX() + cList.get(0).getWidth() <= 0) {
cList.remove(0);
cList.add(7, new Column(7 + 1, false, cList.get(6).getX()));
frame.add(cList.get(7));
}
}
public Column(int pos, boolean init, int lastX) {
this.position = pos;
setLayout(null);
setBackground(Color.WHITE);
floor = new JPanel();
floor.setLayout(null);
floor.setBackground(Color.BLACK);
if (init) {
setBounds((WIDTH * position) - WIDTH, 0, WIDTH, HEIGHT);
floor.setBounds(0, HEIGHT - FLOOR_H, WIDTH, FLOOR_H);
} else {
setBounds(lastX + WIDTH, 0, WIDTH, HEIGHT);
floor.setBounds(0,
(HEIGHT - (FLOOR_H + (FLOOR_H * rand.nextInt(2) / 2))),
WIDTH, FLOOR_H * 2);
}