我将再次重新发布这个问题,试图更准确,并希望我能得到一些帮助,因为这让我发疯。我正在开发一款最多 6 名玩家的棋盘游戏,每个玩家都有一个不同颜色的棋子。我将以下图像加载到 BufferedImage 数组中,并将其视为精灵:
这是相关代码,将每个彩色骰子的每个面放在 BufferedImage[] 中的某个位置:
private BufferedImage[] initAnimationBuffer() {
BufferedImage[] result = new BufferedImage[36];
for (int i = 0; i < 6; i++) {
for (int j = i; j < 6 + i; j++)
result[i + j] = DieSprite.getSprite(j, i, 0);
}
return result;
}
然后,每个玩家根据他的颜色,根据获得的骰子值/位置,还将拥有包含其颜色的面的以下矩阵。换句话说,该矩阵包含图像的“一条线”,并且按值索引:
private BufferedImage[][] initExactDieFaces() {
BufferedImage[][] result = new BufferedImage[6][1];
int row = -1;
String myColor = this.coreGame.getMyPartecipant().getColor();
if (myColor.equals(Constants.COLOR[0])) {
row = 0;
} else if (myColor.equals(Constants.COLOR[1])) {
row = 2;
} else if (myColor.equals(Constants.COLOR[2])) {
row = 4;
} else if (myColor.equals(Constants.COLOR[3])) {
row = 1;
} else if (myColor.equals(Constants.COLOR[4])) {
row = 5;
} else if (myColor.equals(Constants.COLOR[5])) {
row = 3;
}
int offset = 0;
for (int i = 0; i < 6; i++) {
result[i][0] = DieSprite.getSprite(row, i, offset);
offset += 2;
}
return result;
}
我想要的是以下内容:
-当按下“翻转骰子”按钮时,我希望(例如)在 JPanel 内的特定 JLabel 中显示 20 个随机骰子面(它们应该取自第一个数组,AnimationBuffer)
- 上一个动画完成后,我希望显示骰子启动所获得的结果(根据颜色 pawn,取自 ExcatDieFaces)。
为了实现这个目的,我知道我需要 Swing Timer,但我无法将它们组合在一起;以下是按下“翻转骰子”按钮时调用的 startAnimationDie 方法的一些代码:
private void startAnimationDie(final JPanel dieContainer) {
final BufferedImage[] animationBuffer = initAnimationBuffer();
final BufferedImage[][] exactDieFaces = initExactDieFaces();
final AnimationSprite animation = new AnimationSprite(
animationBuffer, Constants.DIE_ANIMATION_SPEED);
/* getting launch value fromt the core Game */
int launchResult = coreGame.launchDie();
coreGame.getMyPartecipant().setLastLaunch(launchResult);
final Timer timer = new Timer(250, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dieContainer.removeAll();
dieContainer.updateUI();
animation.start();
JLabel resultDie = new JLabel();
resultDie.setBounds(60, 265, Constants.DIE_SIZE,Constants.DIE_SIZE);
resultDie.setIcon(new ImageIcon(animationBuffer[new Random().nextInt(36)]));
dieContainer.add(resultDie);
dieContainer.updateUI();
updateUI();
repaint();
}
});
/* animation begins, rolling faces are shown each time the Timer ends*/
for(int i = 0; i<20; i++)
timer.start()
/* showing the final face according to the pawn color and the obtained result from the launch */
dieContainer.removeAll();
dieContainer.updateUI();
AnimationSprite resultAnimation = new AnimationSprite(exactDieFaces[launchResult - 1], 6);
resultAnimation.start();
resultAnimation.update();
resultDie.setIcon(new ImageIcon(exactDieFaces[launchResult - 1][0]));
resultDie.setBounds(60, 265, Constants.DIE_SIZE, Constants.DIE_SIZE);
dieContainer.add(resultDie);
dieContainer.updateUI();
dieContainer.repaint();
}
我怎样才能让它发挥作用?我想我应该使用 Swing.invokeAndWait 但我无法将所有部分组合在一起......你能帮忙吗?