【问题标题】:Java: Displaying Positions correctly in Tortoise vs. Hare AppletJava:在 Tortoise vs. Hare Applet 中正确显示位置
【发布时间】:2015-08-06 09:51:30
【问题描述】:

我目前正在开展一个项目,该项目以 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。

【问题讨论】:

  • 1) 为什么要编写小程序?如果是老师指定的,请参考Why CS teachers should stop teaching Java applets。 2) 为什么使用 AWT?请参阅this answer 了解放弃 AWT 使用支持 Swing 的组件的许多充分理由。
  • @AndrewThompson 对于您的两个问题,我的老师希望我同时使用小程序和 AWT。

标签: java image applet awt


【解决方案1】:

问题在于您的延迟功能。进入循环使小程序继续运行您的代码,并且它没有机会在移动之间呈现显示。如果你允许运行你的小程序的线程休眠,那么它就有机会在运行时显示。

public void delay()
{
    try {
        Thread.sleep(100);
    } catch (Exception e) {}
}

【讨论】:

  • 嗯,当我在我的 IDE 中运行它时,我看到它通过了比赛。也许您可以尝试以基本动画为起点,也许是 javatpoint.com/Animation-in-applet 。祝你好运。
  • 谢谢,这对我有用。现在我只需要让它删除之前的动作,我应该很好
猜你喜欢
  • 2012-10-21
  • 2012-06-07
  • 2012-03-12
  • 1970-01-01
  • 2016-09-08
  • 2011-09-22
  • 1970-01-01
  • 1970-01-01
  • 2014-05-24
相关资源
最近更新 更多