各位大侠,一下代码要怎样才能使得游戏可以暂停又恢复,我知道暂停只要在线程里的循环里给一个判断,然后continue;但是怎么恢复画面呢
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
PaintThread tt = new PaintThread();
if (e.getActionCommand().equals("NewGame")) {
new TankClient(); //游戏主版面构造
}
else if (e.getActionCommand().endsWith("Stop")) {
// b=1;
// tt.run();
System.out.println("暂停!");
}
else if (e.getActionCommand().equals("Continue")) {
System.out.println("继续");
// b = 2;
// tt.run();
} else if (e.getActionCommand().equals("Exit")) {
System.out.println("退出");
System.exit(0);
} else if (e.getActionCommand().equals("help")) {
// MyHelpPanel mhp = new MyHelpPanel();
// this.remove(msp);
// this.add(mhp);
// this.setVisible(true);
}
}
private class PaintThread implements Runnable {
public void run() {
// TODO Auto-generated method stub
while (true) {
// if(变量)continue;
repaint(); //重画画面
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
PaintThread tt = new PaintThread();
if (e.getActionCommand().equals("NewGame")) {
new TankClient(); //游戏主版面构造
}
else if (e.getActionCommand().endsWith("Stop")) {
// b=1;
// tt.run();
System.out.println("暂停!");
}
else if (e.getActionCommand().equals("Continue")) {
System.out.println("继续");
// b = 2;
// tt.run();
} else if (e.getActionCommand().equals("Exit")) {
System.out.println("退出");
System.exit(0);
} else if (e.getActionCommand().equals("help")) {
// MyHelpPanel mhp = new MyHelpPanel();
// this.remove(msp);
// this.add(mhp);
// this.setVisible(true);
}
}
private class PaintThread implements Runnable {
public void run() {
// TODO Auto-generated method stub
while (true) {
// if(变量)continue;
repaint(); //重画画面
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
解决方案 »
- 这个句子是对的吗?
- 为什么Java编译时返回"cannot resolve symbol symbol : method doubleValue location : class java.lang.Double"的错误?
- 关于java scoket 同时收与发的问题!
- 几乎每句都有注释,请帮忙挑挑错,很简单的问题。谢谢
- 数据库连接问题
- 趁大家还没有睡觉 帮我看看
- java对xml和web service的支持如何??
- 多类的dos运行!
- 请问你们作JAVA程序一般用什么编译器?
- 大家来讨论一下高级程序员(软件工程师)在软件开发过程中的作用问题
- 大家快来看。一道面试题。。。很搞。。。
- 关于eclipse中SWT文档的问题
如
成员变量
boolean running = true;
boolean active = true;
public void run() {
while (running) {
if (active) {
//这里做相关的处理
}
yield();
}
}public void suspendGame() {
active = false;
}
public void resumeGame() {
active = true;
}
public void endGame() {
running = false;
}在你的按钮事件中调用相应的方法,如
} else if (e.getActionCommand().endsWith("Stop")) {
your_thread.suspendGame();
} else if (e.getActionCommand().equals("Continue")) {
your_thread.resumeGame();
}