塞特斯似乎不在主要方法中工作

时间:2016-05-12 00:03:30

标签: java swing jframe

我在使用JFrame" position"的主类中使用setter方法时遇到问题。 JFrame的活动部分(三角形称为" turtles"移动)离开JFrame,当我发生这种情况时,我当前改变坐标的方法不起作用。任何帮助将不胜感激,因为我已经被困了好几天。

方法wrapPosition旨在改变坐标,如果他们离开"屏幕"这是(800x600)像素。这个检查在循环中,所以乌龟永远不会离开屏幕。

这是主类:

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

class Lab7b
{

    public static void main(String [ ] args) 
    {
        int deltaTime = 50;
        JFrame frame = new JFrame();
        Canvas canvas = new Canvas();                       
        frame.setTitle("Welcome to turtle land!");
        frame.setSize(800, 600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       
        frame.setVisible(true);
        frame.add(canvas);              

        ArrayList<DynamicTurtle> turtles = new ArrayList<DynamicTurtle>(); //An ArrayList containing Turtle elemenets

               turtles.add(new RandomTurtleA(turtleCanvas, 400, 300));  
        turtles.add(new RandomTurtleA(turtleCanvas, 400, 300));
        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()); //print to JFRAME
                System.out.println("X: " + (turtles.get(i)).getPositionX() + "    Y: " + (turtles.get(i)).getPositionY()); //print to CMD

                (turtles.get(i)).wrapPosition((turtles.get(i)).getPositionX(), (turtles.get(i)).getPositionY()); //this is the wrapPosition method that does not work
            }

            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);
        }   
    }
}

带有麻烦的wrapPosition方法的Turtle类:

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, x, y;
    public double d, e, first, second;


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

    public void wrapPosition(double x, double y)
    {
        this.x = maxX;
        this.y = maxY; 

        if(maxX < 0)
        {
            this.setPositionX(800);
        }

        if(maxX > 800)
        {
            this.setPositionX(0);
        }

        if(maxY < 0)
        {
            this.setPositionY(600);
        }

        if(maxY > 600)
        {
            this.setPositionY(0);
        }        
    }


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

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

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

    public void setPositionY( double y)
    {        
        myLocation.setY(y);
    }    
}

另外,如果对坐标类有任何帮助:

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;
    }
} 

任何帮助将不胜感激。先感谢您。

1 个答案:

答案 0 :(得分:1)

&#34; setter&#34;在类CartesianCoordinate中几乎什么都不做,因为它们只是分配给未读取的成员变量。

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

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

尝试使用这些来更新要读取的坐标:

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

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