访问主类之外的ArrayList对象以更改CartesianCoordiantes

时间:2016-05-11 18:47:36

标签: java jframe

我试图操纵存储在主类中的ArrayList中的信息有点困难。

我的ArrayList包含一个名为“turtles”的“RandomTurtleA”对象(稍后会将更多的海龟添加到ArrayList中),它扩展了“DynamicTurtle”,扩展了“Turtle”。如果我可以为Class Turtle中的ArrayList中存储的“turtles”更改CartesianCoordinates,那么我可以确保它们不会离开可见的JFrame。

我有更改CartesianCoordinates的setter方法,但是无法在wrapPosition方法中找出如何在Main类之外使用它们,其中各种if语句用于检查乌龟是否关闭屏幕。

这是主类:( ArrayList)

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;

class Lab7b
{
	public static void main(String [ ] args) 
	{
		int deltaTime = 50;
		double currentX, currentY;

		JFrame frame = new JFrame();
		Canvas canvas = new Canvas();						
		frame.setTitle("Welcome to turtle land!");
		frame.setSize(1000, 600);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);		
		frame.setVisible(true);
		frame.add(canvas);			
		

		JLabel hello = new JLabel("Hello all Word!");
		hello.setHorizontalAlignment(SwingConstants.CENTER);				
		frame.add(hello, BorderLayout.PAGE_END);				

		
		Canvas turtleCanvas = new Canvas();
		frame.add(turtleCanvas, BorderLayout.CENTER);		

		ImageIcon supermanImage = new ImageIcon("superman.png");
		JLabel supermanJLable = new JLabel(supermanImage);
		frame.add(supermanJLable, BorderLayout.LINE_START);

		ImageIcon hulkImage = new ImageIcon("hulk.png");
		JLabel hulkJLable = new JLabel(hulkImage);
		frame.add(hulkJLable, BorderLayout.LINE_END);

		CartesianCoordinate randomInit = new CartesianCoordinate(400,300);		

		ArrayList<DynamicTurtle> turtles = new ArrayList<DynamicTurtle>();
		turtles.add(new RandomTurtleA(turtleCanvas, 400, 300));		

		

		while(true)
		{
			for (int i = 0; i < turtles.size(); i++)
			{
				(turtles.get(i)).unDrawTurtle();
				hello.setText("X: " + (turtles.get(i)).getPositionX() + "    Y: " + (turtles.get(i)).getPositionY());
			
				(turtles.get(i)).getPositionX() = currentX; // Problems here trying to access the current position of the turtle
				(turtles.get(i)).getPositionX() = currentY;	// "                                                              "			
				
				(turtles.get(i)).wrapPosition(1000, 600);
			}

			for (int i = 0; i < turtles.size(); i++)
			{
				(turtles.get(i)).update(1000);
			}


			for (int i = 0; i < turtles.size(); i++)
			{
				(turtles.get(i)).drawTurtle();
			}
		
		Utils.pause(deltaTime/2);
		}	
	}
}

Turtle Class :(包含wrapPosition方法)

import java.util.ArrayList;

class Turtle 
{
protected Canvas canvas; // private field reference to a canvas private           
private CartesianCoordinate myLocation, oldLocation; 
private boolean penDown = true;
private double Angle;
public Turtle kieranMullen;

public Turtle(Canvas canvas, CartesianCoordinate initLocation) 
{
    this.canvas = canvas;
    this.myLocation = new CartesianCoordinate(0,0);
    Angle = 0;
    penDown = true;
    myLocation = initLocation.copy();        
}

public void wrapPositon(double maximumXPosition, double mamimumYPosition) 
{
    if(turtles.getPositionX() < 0) //if current x of turtle is less than 0
    {
        this.setX(1000); //set to 1000
    }

    if(currentX > 1000)
    {
        this.setX(0);
    }

    if(currentY < 0)
    {
        this.setY(600);
    }

    if(currentY > 600)
    {
        this.setX(0);
    }
}

public double getPositionX()
{
    double getPosX;
    getPosX = myLocation.getX();
    return getPosX;
}

public double getPositionY()
{
    double getPosY;
    getPosY = myLocation.getY();
    return getPosY;
}

}

笛卡尔坐标类:(包含setter方法)

   class CartesianCoordinate
{
private double xPosition, yPosition, setterX, setterY;


public CartesianCoordinate(double x, double y)
{
	this.xPosition = x;
	this.yPosition = y;
}

public double getX()
{
	return this.xPosition;
}

public double getY()
{
	return this.yPosition;
}

public void setX(double setterX)
{
	this.setterX = xPosition;
}

public void setY(double setterY)
{
	this.setterY = yPosition;
}	

}

我非常感谢给予我的任何帮助,在此先感谢。此致,Hornsbeh。

1 个答案:

答案 0 :(得分:0)

你的问题是,你的龟类中只有x和y位置的吸气剂。

您必须为Turtle类中公共的位置添加一个setter,该setter又调用Coordinates Object的函数。就像你已经使用get方法一样。

Setter:

class Turtle {

    //...

    public void setPositionX(final double x)
    {
         myLocation.setX(x);
    }

对于Y来说同样如此。

然后在您的主类中,您可以使用该方法设置X:

(turtles.get(i)).setPositionX(currentX);