Java:在 Tortoise vs. Hare Applet 中正确显示位置
Java: Displaying Positions correctly in Tortoise vs. Hare Applet
我目前正在做一个项目,该项目以 Applet 的形式实时显示龟兔赛跑。选择一个随机数,动物根据该数字向前、向后或根本不移动。我的问题是让动物被实时看到。它目前在他们的起始位置显示图像,并说出谁赢了。关于如何完成这项工作的任何指示都会很棒。
我的代码:
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Color;
import java.awt.Font;
public class Project2 extends Applet
{
Image tortoise, hare;
Image scaledTortoise, scaledHare;
final int tortoiseYPos = 50, hareYPos = 400, SQUARE = 20, END = 1200;
int tortoiseXPos = 180, hareXPos = 180;
public void init()
{
tortoise = getImage(getDocumentBase(), "tortoise.gif");
hare = getImage(getDocumentBase(), "hare.gif");
scaledTortoise = tortoise.getScaledInstance(20, 50, Image.SCALE_SMOOTH);
scaledHare = hare.getScaledInstance(20, 50, Image.SCALE_SMOOTH);
}
public void paint(Graphics field)
{
drawField(field);
drawMoves(field);
field.setFont(new Font("Times New Roman", Font.ITALIC, 72));
//Display winner when they get to the finish line
if(tortoiseXPos >= END)
{
field.drawString("Tortoise Wins!!", 650, 240);
}
else if(hareXPos >= END)
{
field.drawString("Hare Wins!!", 650, 240);
}
}
public void drawField(Graphics field)
{
setBackground(Color.green);
Font f = new Font("Times New Roman", Font.BOLD, 48);
field.setFont(f);
field.drawString("Tortoise", 0, 75);
field.drawString("Hare", 0, 425);
//fill alternating black and white rectangles
field.setColor(Color.black);
int x = 180;
for(int i = 0; i < 50; i++)
{
field.fillRect(x, 50, SQUARE, 50);
field.fillRect(x, 400, SQUARE, 50);
x += (SQUARE);
}
field.drawImage(scaledTortoise, 180, tortoiseYPos, this);
field.drawImage(scaledHare, 180, hareYPos, this);
}
public void drawMoves(Graphics s)
{
while(tortoiseXPos < END && hareXPos < END)
{
int move = (int)(Math.random() * 10);
tortoiseMoves(move); hareMoves(move);
s.drawImage(scaledTortoise, tortoiseXPos, tortoiseYPos, this);
s.drawImage(scaledHare, hareXPos, hareYPos, this);
delay(); delay(); delay();
}
}
public void tortoiseMoves(int move)
{ //Moves for Tortoise, 180 is start, 1200 is finish
if(move <= 5)
{
tortoiseXPos += (3 * SQUARE);
}
else if(move <= 8)
{
tortoiseXPos += SQUARE;
}
else if(move <= 10)
{
tortoiseXPos -= (6 * SQUARE);
}
if(tortoiseXPos < 180)
{
tortoiseXPos = 180;
}
if(tortoiseXPos > END)
{
tortoiseXPos = END;
}
}
public void hareMoves(int move)
{ //Moves for Hare, 180 is start, 1200 is finish
if(move <= 2)
{
hareXPos += (9 * SQUARE);
}
else if(move <= 5)
{
hareXPos += (SQUARE);
}
else if(move <= 6)
{
hareXPos -= (12 * SQUARE);
}
else if(move <= 8)
{
hareXPos -= (2 * SQUARE);
}
else if(move <= 10)
{
hareXPos = hareXPos;
}
if(hareXPos < 180)
{
hareXPos = 180;
}
if(hareXPos > END)
{
hareXPos = END;
}
}
public void delay()
{
for(int i = 0; i <= 90000000; i++)
{
}
}
}
请注意,我不熟悉也不应该使用 java swing,因为我应该只使用 awt。
问题出在你的延迟函数上。进入一个循环会使小程序 运行 保留您的代码,并且它没有机会在移动之间呈现显示。如果您允许小程序所在的线程 运行 进入休眠状态,那么它就有机会在运行时显示。
public void delay()
{
try {
Thread.sleep(100);
} catch (Exception e) {}
}
我目前正在做一个项目,该项目以 Applet 的形式实时显示龟兔赛跑。选择一个随机数,动物根据该数字向前、向后或根本不移动。我的问题是让动物被实时看到。它目前在他们的起始位置显示图像,并说出谁赢了。关于如何完成这项工作的任何指示都会很棒。
我的代码:
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Color;
import java.awt.Font;
public class Project2 extends Applet
{
Image tortoise, hare;
Image scaledTortoise, scaledHare;
final int tortoiseYPos = 50, hareYPos = 400, SQUARE = 20, END = 1200;
int tortoiseXPos = 180, hareXPos = 180;
public void init()
{
tortoise = getImage(getDocumentBase(), "tortoise.gif");
hare = getImage(getDocumentBase(), "hare.gif");
scaledTortoise = tortoise.getScaledInstance(20, 50, Image.SCALE_SMOOTH);
scaledHare = hare.getScaledInstance(20, 50, Image.SCALE_SMOOTH);
}
public void paint(Graphics field)
{
drawField(field);
drawMoves(field);
field.setFont(new Font("Times New Roman", Font.ITALIC, 72));
//Display winner when they get to the finish line
if(tortoiseXPos >= END)
{
field.drawString("Tortoise Wins!!", 650, 240);
}
else if(hareXPos >= END)
{
field.drawString("Hare Wins!!", 650, 240);
}
}
public void drawField(Graphics field)
{
setBackground(Color.green);
Font f = new Font("Times New Roman", Font.BOLD, 48);
field.setFont(f);
field.drawString("Tortoise", 0, 75);
field.drawString("Hare", 0, 425);
//fill alternating black and white rectangles
field.setColor(Color.black);
int x = 180;
for(int i = 0; i < 50; i++)
{
field.fillRect(x, 50, SQUARE, 50);
field.fillRect(x, 400, SQUARE, 50);
x += (SQUARE);
}
field.drawImage(scaledTortoise, 180, tortoiseYPos, this);
field.drawImage(scaledHare, 180, hareYPos, this);
}
public void drawMoves(Graphics s)
{
while(tortoiseXPos < END && hareXPos < END)
{
int move = (int)(Math.random() * 10);
tortoiseMoves(move); hareMoves(move);
s.drawImage(scaledTortoise, tortoiseXPos, tortoiseYPos, this);
s.drawImage(scaledHare, hareXPos, hareYPos, this);
delay(); delay(); delay();
}
}
public void tortoiseMoves(int move)
{ //Moves for Tortoise, 180 is start, 1200 is finish
if(move <= 5)
{
tortoiseXPos += (3 * SQUARE);
}
else if(move <= 8)
{
tortoiseXPos += SQUARE;
}
else if(move <= 10)
{
tortoiseXPos -= (6 * SQUARE);
}
if(tortoiseXPos < 180)
{
tortoiseXPos = 180;
}
if(tortoiseXPos > END)
{
tortoiseXPos = END;
}
}
public void hareMoves(int move)
{ //Moves for Hare, 180 is start, 1200 is finish
if(move <= 2)
{
hareXPos += (9 * SQUARE);
}
else if(move <= 5)
{
hareXPos += (SQUARE);
}
else if(move <= 6)
{
hareXPos -= (12 * SQUARE);
}
else if(move <= 8)
{
hareXPos -= (2 * SQUARE);
}
else if(move <= 10)
{
hareXPos = hareXPos;
}
if(hareXPos < 180)
{
hareXPos = 180;
}
if(hareXPos > END)
{
hareXPos = END;
}
}
public void delay()
{
for(int i = 0; i <= 90000000; i++)
{
}
}
}
请注意,我不熟悉也不应该使用 java swing,因为我应该只使用 awt。
问题出在你的延迟函数上。进入一个循环会使小程序 运行 保留您的代码,并且它没有机会在移动之间呈现显示。如果您允许小程序所在的线程 运行 进入休眠状态,那么它就有机会在运行时显示。
public void delay()
{
try {
Thread.sleep(100);
} catch (Exception e) {}
}