我正在尝试扫描在主类中创建的AraayList,在turtle类中检查各种条件。我尽可能地简化了代码。
主要班级:
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.awt.event.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
class TurtleProgram
{
public ArrayList<DynamicTurtle> turtles;
public static void main(String[] args)
{
new TurtleProgram();
}
public TurtleProgram()
{
//INSTANTIATED JFRAME WITH BUTTONS/SLIDER AND CREATED LISTENER METHODS
turtles = new ArrayList<DynamicTurtle>(); //THE ARRAYLIST I AM WANTING TO USE IN ANOTHER CLASS
turtles.add(new RandomTurtleB(canvas, 400, 300, 100, 0)); //THIS ARRAYLIST BECOMES LARGER WHEN BUTTONS ON JFRAME TO ADD NEW TURTLES
gameLoop(); //LOOP FOREVER
}
private void gameLoop()
{
int deltaTime = 20;
while(true)
{
for (int i = 0; i < turtles.size(); i++)
{
(turtles.get(i)).unDrawTurtle(); //REMOVE FROM CANVAS
(turtles.get(i)).wrapPosition((turtles.get(i)).getPositionX(), (turtles.get(i)).getPositionY()); //MAKE SURE TURTLES NOT OFF SCREEN
}
for (int i = 0; i < turtles.size(); i++)
{
(turtles.get(i)).update(1000); //MAKE THE TURTLES MOVE WITH A CHANCE OF TURNING
}
for (int i = 0; i < turtles.size(); i++)
{
(turtles.get(i)).drawTurtle(); //DRAW TO CANVAS
}
for(int i = 0; i < turtles.size(); i++)
{
(turtles.get(i)).cohesian(); //THIS IS HOW I WOULD LIKE TO ADDRESS THE COHESIAN METHOD
}
Utils.pause(deltaTime/2);
}
}
}
然后我想检查当前的龟对抗其他海龟,看看它是否在cohesion()方法中接近以执行动作:
class Turtle
{
protected Canvas canvas; // private field reference to a canvas private
private CartesianCoordinate myLocation, oldLocation;
private boolean penDown = true;
private double angle, maxX, maxY, nowPosX, nowPosY, maximumX, maximumY, x, y;
public double d, e, first, second;
private int speed;
public Turtle(Canvas canvas, CartesianCoordinate initLocation)
{
this.canvas = canvas;
this.myLocation = new CartesianCoordinate(0,0);
penDown = true;
myLocation = initLocation.copy();
}
public void cohesian()
{
double flockingDistanceLimit = 200;
double numberOfFlockers = 0;
double combinedX = 0;
double combinedY = 0;
double averageCombinedX, averageCombinedY, averageCombinedY, moveToFlock, turnToFlock, distanceToPotentialFlock;
for (DynamicTurtle t : turtles) //CANNOT USE THE ARRAYLIST TO SCAN THROUGH ITS ELEMENTS
{
if(this.getPositionX() != t.getPositionX() && this.getPositionY() != t.getPositionY()) //MAKE SURE TURTLE ISNT SCANNING ITS SELF
{
distanceToPotentialFlock = Math.sqrt(Math.pow((this.getPositionX()-t.getPositionX()),2 ) + Math.pow((this.getPositionY()-this.getPositionY()),2)); //FIND DISTANCE BETWEEN CURRENT AND SCANNED TURTLE
if(distanceToPotentialFlock < flockingDistanceLimit) //MAKE SURE THE FOUND TURTLE IS WITHIN RANGE USING THE DISTANCE BETWEEN POINTS METHOD
{
combinedX = combinedX + t.getPositionX(); //FIND SUMMATION OF X POSITIONS
combinedY = combinedY + t.getPositionY(); //FIND SUMMATION OF Y POSITIONS
numberOfFlockers++; //AS A FLOCKER HAS BEEN FOUND INCREMENT THE NUMBER OF FLOCKERS
}
}
if(numberOfFlockers > 0)
{
averageCombinedX = (combinedX / numberOfFlockers); //FIND AVERAGE X POSITION
averageCombinedY = (combinedY / numberOfFlockers); //FIND AVERAGE Y POSITION
moveToFlock = Math.sqrt(Math.pow(averageCombinedX,2 ) + Math.pow(averageCombinedY, 2)); //CALCULATE DISTANCE TO CENTER OF FLOCKING
turnToFlock = Math.atan(averageCombinedY / averageCombinedX);
if(turnToFlock < 0)
{
turnToFlock = 360 - (-turnToFlock); //ADJUSTING FOR NEGATIVES
}
}
}
}
public void wrapPosition(double x, double y)
{
this.maxX = x;
this.maxY = y;
if(maxX < 0)
{
this.setPositionX(800);
}
else if(maxX > 800)
{
this.setPositionX(0);
}
if(maxY < 0)
{
this.setPositionY(600);
}
else if(maxY > 600)
{
this.setPositionY(0);
}
}
}
如果我可以帮助在Turtle类中使用ArrayList,那么在cohesian()方法中,我们将非常感激。先感谢您。
答案 0 :(得分:0)
将arrayList作为参数传递给cohesian,或将其声明为静态字段