基本Java海龟图形动画

基本Java海龟图形动画,java,oop,Java,Oop,我正在编写一个即将完成的程序,但我在几个小问题上陷入了困境。我正在写一个程序来移动动画海龟周围的显示输出。我的程序运行得很好,但是我需要一个tester类,它将以OOP风格调用我的函数,我不知道如何实现。另外,程序代码必须允许用户影响动画,这也是我需要帮助的一点 如果有人能够键入一些代码来帮助解决这些问题,我们将不胜感激。多谢各位 这是一个动画程序,将移动动画,彩色海龟在屏幕上 import java.util.Random; import java.util.Date; import java

我正在编写一个即将完成的程序,但我在几个小问题上陷入了困境。我正在写一个程序来移动动画海龟周围的显示输出。我的程序运行得很好,但是我需要一个tester类,它将以OOP风格调用我的函数,我不知道如何实现。另外,程序代码必须允许用户影响动画,这也是我需要帮助的一点

如果有人能够键入一些代码来帮助解决这些问题,我们将不胜感激。多谢各位

这是一个动画程序,将移动动画,彩色海龟在屏幕上

import java.util.Random;
import java.util.Date;
import java.util.List;
import java.awt.Color;

public class Main{
public static void main(String[] args){
new Runner().run();
  }//end main method
}//end class Main
//------------------------------------------------------//

class Runner{

  Random randGen = new Random(new Date().getTime());


  int aquariumWidth = 450;
  int aquariumHeight = 338;
  World aquarium = new World(
                        aquariumWidth,aquariumHeight);


  List turtleList = aquarium.getTurtleList();
  //----------------------------------------------------//

void run(){
aquarium.setPicture(new Picture("aquarium.gif"));

int numberTurtles = 8;


for(int cnt=0;cnt < numberTurtles;cnt++){
  int xCoor =
          Math.abs(randGen.nextInt() % aquariumWidth);
  int yCoor =
         Math.abs(randGen.nextInt() % aquariumHeight);
  new Turtle(xCoor,yCoor,aquarium);
}//end for loop

int angle = 0;
int leaderMove = 0;

Turtle turtle = null; //empty container
Turtle testTurtle = null; //empty container 


Turtle leader = (Turtle)turtleList.get(0);
leader.setShellColor(Color.RED);
int turtleLength = leader.getHeight();


turtle = (Turtle)turtleList.get(3);
turtle.setBodyColor(Color.YELLOW);
turtle.setShellColor(Color.ORANGE);
turtle = (Turtle)turtleList.get(7);
turtle.setBodyColor(Color.ORANGE);
turtle.setShellColor(Color.YELLOW);

while(true){//animation loop will run forever

  leaderMove = (int)(turtleLength/2
               + turtleLength*randGen.nextDouble()/4);

  angle = (int)(45*(randGen.nextDouble() - 0.5));


  for(int cnt = 0;cnt < turtleList.size();cnt++){
    turtle = (Turtle)turtleList.get(cnt);
    turtle.penUp();//no turtle tracks allowed

    //Force the turtles to maintain some distance
    // between them 
    for(int cntr = 1;cntr < turtleList.size();cntr++){
      testTurtle = (Turtle)turtleList.get(cntr);
      //Use size as a part of the equation and increasy by one
      if((testTurtle != turtle) && (cnt != 0)){
        int separation = (int)(turtle.getDistance(
          testTurtle.getXPos(),testTurtle.getYPos()));

          if(separation < 2*turtleLength){

            turtle.turnToFace(testTurtle);
            turtle.turn(180);
            turtle.forward(turtleLength/3);
          }//end if
      }//end if
    }//end for loop on turtle separation

    if(cnt == 0){
      //This is the leader

      int xPos = leader.getXPos();
      int yPos = leader.getYPos();

      if(xPos < turtleLength){
        leader.setHeading(90);
      }else if(xPos > aquariumWidth -turtleLength -2){
        leader.setHeading(-90);
      }//end else

      if(yPos < turtleLength){
        leader.setHeading(180);
      }else if(
             yPos > aquariumHeight -turtleLength - 2){
        leader.setHeading(0);
      }//end else


      leader.turn(angle);
      leader.forward(leaderMove);
    }else{

      turtle.turnToFace(leader);
      int distanceToLeader = (int)(turtle.getDistance(
                  leader.getXPos(),leader.getYPos()));
      turtle.forward(distanceToLeader/10);
    }//end else

  }//end for loop processing all turtles

 //This took some looking over to find the right methods to control the speed. 
  try{
    Thread.currentThread().sleep(100);
  }catch(InterruptedException ex){
  }
}

}
}
import java.util.Random;
导入java.util.Date;
导入java.util.List;
导入java.awt.Color;
公共班机{
公共静态void main(字符串[]args){
新跑步者().run();
}//端主方法
}//末级干管
//------------------------------------------------------//
班长{
Random randGen=new Random(new Date().getTime());
宽度=450;
高度=338;
世界水族馆=新世界(
水族箱宽度、高度);
List turtleList=水族馆.getTurtleList();
//----------------------------------------------------//
无效运行(){
水族馆.setPicture(新图片(“水族馆.gif”);
整数=8;
对于(int cnt=0;cnt宽度-套头衫长度-2){
前导设置标题(-90);
}//结束其他
if(yPos<套头衫长度){
引导器。设置标题(180);
}否则如果(
yPos>水族馆高度-套头衫长度-2){
前导设置标题(0);
}//结束其他
引导。转动(角度);
领队前进(领队前进);
}否则{
乌龟。转身面(领导者);
int-distanceToLeader=(int)(turtle.getDistance)(
leader.getXPos(),leader.getYPos());
乌龟。向前(与领头的距离/10);
}//结束其他
}//结束循环处理所有海龟
//这花了一些时间寻找控制速度的正确方法。
试一试{
Thread.currentThread().sleep(100);
}捕获(中断异常例外){
}
}
}
}