Java Checkers小程序问题
我正在编写一个小程序,它可以使跳棋以相同的速度彼此相对地来回运行。不过,底部检查器的运行速度比顶部检查器快得多。我该如何纠正它,使它们以相同的速度来回运行。我猜这和for循环有关,但我想不出来Java Checkers小程序问题,java,loops,applet,Java,Loops,Applet,我正在编写一个小程序,它可以使跳棋以相同的速度彼此相对地来回运行。不过,底部检查器的运行速度比顶部检查器快得多。我该如何纠正它,使它们以相同的速度来回运行。我猜这和for循环有关,但我想不出来 import java.awt.*; import java.awt.Graphics; import java.awt.Color; import java.awt.Image; public class Checkers extends java.applet.Applet implements
import java.awt.*;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Image;
public class Checkers extends java.applet.Applet implements Runnable {
Thread runner;
int x1pos;
int x2pos;
Image offscreenImg;
Graphics offscreenG;
public void init() {
offscreenImg = createImage(this.size().width, this.size().height);
offscreenG = offscreenImg.getGraphics();
}
public void start() {
if (runner == null); {
runner = new Thread(this);
runner.start();
}
}
public void stop() {
if (runner != null) {
runner.stop();
runner = null;
}
}
public void run() {
while (true) {
for (x1pos = 5; x1pos <= 105; x1pos+=4)
{
for (x2pos = 105; x2pos >= 5; x2pos-=4)
{
repaint ();
try { Thread.sleep(10); }
catch (InterruptedException e) { }
}
repaint();
try { Thread.sleep(100); }
catch (InterruptedException e) { }
}
x1pos = 5;
x2pos = 105;
}
}
public void update(Graphics g) {
paint(g);
}
public void paint(Graphics g) {
// Draw background onto the buffer area
offscreenG.setColor(Color.black);
offscreenG.fillRect(0,0,100,100);
offscreenG.setColor(Color.blue);
offscreenG.fillRect(100,0,100,100);
offscreenG.setColor(Color.blue);
offscreenG.fillRect(0,100,100,100);
offscreenG.setColor(Color.black);
offscreenG.fillRect(100,100,100,100);
// Draw checker
offscreenG.setColor(Color.red);
offscreenG.fillOval(x1pos,5,90,90);
offscreenG.setColor(Color.green);
offscreenG.fillOval(x2pos,105,90,90);
// Now, transfer the entire buffer onto the screen
g.drawImage(offscreenImg,0,0,this);
}
public void destroy() {
offscreenG.dispose();
}
}
import java.awt.*;
导入java.awt.Graphics;
导入java.awt.Color;
导入java.awt.Image;
公共类检查器扩展了java.applet.applet实现可运行{
螺纹流道;
intx1pos;
int x2pos;
图像偏移;
图形偏移;
公共void init(){
offscreinimg=createImage(this.size().width,this.size().height);
offscreenG=offscreenImg.getGraphics();
}
公开作废开始(){
if(runner==null){
流道=新螺纹(此);
runner.start();
}
}
公共停车场(){
if(runner!=null){
停止();
runner=null;
}
}
公开募捐{
while(true){
用于(x1pos=5;x1pos=5;x2pos-=4)
{
重新油漆();
试试{Thread.sleep(10);}
捕获(中断异常e){}
}
重新油漆();
试试{Thread.sleep(100);}
捕获(中断异常e){}
}
x1pos=5;
x2pos=105;
}
}
公共空间更新(图g){
油漆(g);
}
公共空间涂料(图g){
//在缓冲区上绘制背景
偏移。设置颜色(颜色。黑色);
筛外填充率(0,0100);
偏移。设置颜色(颜色。蓝色);
筛外填充(100,0100);
偏移。设置颜色(颜色。蓝色);
Offscreen.fillRect(0100);
偏移。设置颜色(颜色。黑色);
offscreen.fillRect(100100);
//绘图检查器
偏移。设置颜色(颜色。红色);
筛外圆角(x1pos,5,90,90);
偏移。设置颜色(颜色。绿色);
筛外圆角(x2pos,105,90,90);
//现在,将整个缓冲区转移到屏幕上
g、 drawImage(offscreenImg,0,0,this);
}
公共空间销毁(){
offscreen.dispose();
}
}
谢谢您的run语句中的循环已嵌套。这意味着每次更新x1pos时,都会将x2pos从105更新为5 您的for循环应为(已尽可能与您的类似地重新编写)
for(x1pos=5,x2pos=105;x1pos=5;x1pos+=4,x2pos-=4)
{
重新油漆();
试试{Thread.sleep(10);}
捕获(中断异常e){}
}
1)为什么要编写小程序?如果是由于老师的特殊要求,请参考。2) 为什么是AWT而不是Swing?关于放弃使用AWT组件的许多好理由,请参见我的答案。
for (x1pos = 5, x2pos = 105; x1pos <= 105 && x2pos >= 5 ; x1pos+=4, x2pos-=4)
{
repaint ();
try { Thread.sleep(10); }
catch (InterruptedException e) { }
}