我正在用Java开发一个跳棋游戏,我偶然发现了一个问题。 我想要创造的是,当按下一个棋盘时,它前面的可用2个点应变为灰色2秒。
变成灰色很容易,但是当我希望它在2秒后使用Thread.sleep(2000)返回时,我注意到它在实际睡眠之前首先在此开关中完成剩余的情况(所以它转动了新鲜的灰色方块立即回黑)
出了什么问题?提前谢谢!
switch (bord[ypos][xpos]) {
case 0:
break;
case 1:
break;
case 2:
if (bord[ypos + 1][xpos - 1] == 1) {
bord[ypos + 1][xpos - 1] = 4;
}
if (bord[ypos + 1][xpos + 1] == 1) {
bord[ypos + 1][xpos + 1] = 4;
}
repaint();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
}
if (bord[ypos + 1][xpos - 1] == 4) {
bord[ypos + 1][xpos - 1] = 1;
}
if (bord[ypos + 1][xpos + 1] == 4) {
bord[ypos + 1][xpos + 1] = 1;
}
break;
case 3:
break;
}
尝试使用计时器(我是新手,所以请不要太过刻苦)
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package dammen;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JPanel;
import javax.swing.Timer;
/**
*
* @author Boyen
*/
public class Board extends JPanel implements MouseListener {
boolean test;
boolean black = false;
boolean redpiece = false;
boolean bluepiece = true;
Timer timer;
private int[][] bord = {{0, 2, 0, 2, 0, 2, 0, 2},
{2, 0, 2, 0, 2, 0, 2, 0},
{0, 2, 0, 2, 0, 2, 0, 2},
{1, 0, 1, 0, 1, 0, 1, 0},
{0, 1, 0, 1, 0, 1, 0, 1},
{3, 0, 3, 0, 3, 0, 3, 0},
{0, 3, 0, 3, 0, 3, 0, 3},
{3, 0, 3, 0, 3, 0, 3, 0}};
public Board(Dammen parent) {
addMouseListener(this);
timer = new Timer(100,taskPerformer);
timer.setRepeats(false);
}
public void start() {
}
public void paint(Graphics g) {
super.paint(g);
Dimension size = getSize();
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
switch (bord[i][j]) {
case 0:
g.setColor(Color.WHITE);
break;
case 1:
g.setColor(Color.BLACK);
break;
case 2:
g.setColor(Color.RED);
break;
case 3:
g.setColor(Color.BLUE);
break;
case 4:
g.setColor(Color.gray);
break;
}
g.fillRect((size.width / 8) * j, (size.height / 8) * i, size.width / 8, size.height / 8);
}
}
}
int mouseX, mouseY;
@Override
public void mouseClicked(MouseEvent e) {
mouseX = e.getX();
mouseY = e.getY();
zoekmogelijkespots(mouseX, mouseY);
}
ActionListener taskPerformer = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
}
};
public void zoekmogelijkespots(int mouseX, int mouseY) {
Dimension size = getSize();
System.out.println(mouseX + "," + mouseY);
int xpos;
int ypos;
xpos = (int) (mouseX / (size.width / 8));
ypos = (int) (mouseY / (size.height / 8));
System.out.println(ypos + "," + xpos);
System.out.println(bord[ypos][xpos]);
switch (bord[ypos][xpos]) {
case 0:
break;
case 1:
break;
case 2:
if (bord[ypos + 1][xpos - 1] == 1) {
bord[ypos + 1][xpos - 1] = 4;
}
if (bord[ypos + 1][xpos + 1] == 1) {
bord[ypos + 1][xpos + 1] = 4;
}
repaint();
timer.start();
if (bord[ypos + 1][xpos - 1] == 4) {
bord[ypos + 1][xpos - 1] = 1;
}
if (bord[ypos + 1][xpos + 1] == 4) {
bord[ypos + 1][xpos + 1] = 1;
}
break;
case 3:
break;
}
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
}
答案 0 :(得分:3)
你可以使用Swing javax.swing.Timer来实现你想要的......
if (bord[ypos + 1][xpos - 1] == 1) {
bord[ypos + 1][xpos - 1] = 4;
}
if (bord[ypos + 1][xpos + 1] == 1) {
bord[ypos + 1][xpos + 1] = 4;
}
repaint();
Timer timer = new Timer(2000, new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if (bord[ypos + 1][xpos - 1] == 4) {
bord[ypos + 1][xpos - 1] = 1;
}
if (bord[ypos + 1][xpos + 1] == 4) {
bord[ypos + 1][xpos + 1] = 1;
}
}
});
timer.setRepeats(false);
timer.start();
答案 1 :(得分:1)
而不是Thread.sleep(2000);
,你可以这样做(注意,我无法测试这个):
public static class GrayWorker extends SwingWorker<Object, Object> {
private final int xpos;
private final int ypos;
private final int[][] bord;
public GrayWorker(int[][] bord, int xpos, int ypos) {
this.bord = bord; this.xpos = xpos; this.ypos = ypos;
}
@Override
public Object doInBackground() {
Thread.sleep(2000);
return null;
}
@Override
protected void done() {
if (bord[ypos + 1][xpos - 1] == 4) {
bord[ypos + 1][xpos - 1] = 1;
}
if (bord[ypos + 1][xpos + 1] == 4) {
bord[ypos + 1][xpos + 1] = 1;
}
}
}
然后,在你的其他课堂上,做:
GrayWorker worker = new GrayWorker(bord, xpos, ypos);
worker.execute();