Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 二维网格阵列_Java_Turtle Graphics - Fatal编程技术网

Java 二维网格阵列

Java 二维网格阵列,java,turtle-graphics,Java,Turtle Graphics,我已经创建了一个程序,使用海龟图形绘制不同的形状。在400px×400px的JFrame上有一个5x5的离散网格。它环绕顶部/底部和左/右,以防形状翻转 我现在需要做的是添加一个2d数组,创建一个0的400x400数组。如果乌龟通过任何像素(对应于2d数组中的一个点),则需要将0更改为1。最好的办法是什么?在我看来,最好的方法是用双变量跟踪海龟的位置(X&Y)和方向,然后用基本三角法计算出海龟击中的下一个“像素”。我假设您将使用计时器或循环运行此代码,因此它可能看起来像这样: //This co

我已经创建了一个程序,使用海龟图形绘制不同的形状。在400px×400px的JFrame上有一个5x5的离散网格。它环绕顶部/底部和左/右,以防形状翻转


我现在需要做的是添加一个2d数组,创建一个0的400x400数组。如果乌龟通过任何像素(对应于2d数组中的一个点),则需要将0更改为1。最好的办法是什么?在我看来,最好的方法是用双变量跟踪海龟的位置(X&Y)和方向,然后用基本三角法计算出海龟击中的下一个“像素”。我假设您将使用计时器或循环运行此代码,因此它可能看起来像这样:

//This code is somehwere in your program
class Turtle
{
     private double x;
     private double y;
     private double direction; //direction in radians
    public double getX() {
        return x;
    }
    public void setX(double x) {
        this.x = x;
    }
    public double getY() {
        return y;
    }
    public void setY(double y) {
        this.y = y;
    }
    public double getDirection() {
        return direction;
    }
    public void setDirection(double direction) {
        while(direction > Math.PI * 2)
        {
            direction -= Math.PI * 2;
        }
        while(direction < 0)
        {
            direction += Math.PI * 2;
        }
        this.direction = direction;
    }
}

private static final int GRID_WIDTH = 400;
private static final int GRID_HEIGHT = 400;


private Turtle myTurtle = new Turtle();
private boolean[][] grid = new boolean[GRID_WIDTH][GRID_HEIGHT];
JFrame myJFrame = new JFrame();
private Graphics gridImage = myJFrame.getGraphics();

private void initialise()
{
    for(int y = 0; y < GRID_HEIGHT; y++)
    {
        for(int x = 0; x < GRID_WIDTH; x++)
        {
            grid[x][y] = false;
        }
    }

    gridImage.setColor(Color.BLACK);
    gridImage.fillRect(0, 0, GRID_WIDTH, GRID_HEIGHT);
    gridImage.setColor(Color.white);
}

//This code would be inside the loop or timer callback function
private void myMainFunction()
{
    double newX, newY;
    double deltaX, deltaY;

    deltaX = Math.cos(myTurtle.getDirection());
    deltaY = Math.sin(myTurtle.getDirection());

    newX = myTurtle.getX() + deltaX;
    newY = myTurtle.getY() + deltaY;

    if(newX < 0)
    {
        newX += GRID_WIDTH; 
    }
    else if(newX > GRID_WIDTH)
    {
        newX -=  GRID_WIDTH;    
    }

    if(newY < 0)
    {
        newY += GRID_HEIGHT; 
    }
    else if(newY > GRID_HEIGHT)
    {
        newY -=  GRID_HEIGHT;   
    }

    grid[(int)Math.floor(newX)][(int)Math.floor(newY)] = true;

    gridImage.fillRect((int)Math.floor(newX), (int)Math.floor(newY), 1, 1);
    myJFrame.update(gridImage);

    myTurtle.setX(newX);
    myTurtle.setY(newY);
}
//此代码在您的程序中
甲鱼
{
私人双x;
私人双y;
专用双向;//弧度方向
公共双getX(){
返回x;
}
公共无效集x(双x){
这个.x=x;
}
公共双盖{
返回y;
}
公共空间设置(双y){
这个。y=y;
}
公共双向{
返回方向;
}
公共方向(双向){
while(方向>Math.PI*2)
{
方向-=Math.PI*2;
}
而(方向<0)
{
方向+=Math.PI*2;
}
这个方向=方向;
}
}
专用静态最终整型网格宽度=400;
专用静态最终内部网格高度=400;
私人海龟myTurtle=新海龟();
私有布尔值[][]栅格=新布尔值[栅格宽度][栅格高度];
JFrame myJFrame=新JFrame();
私有图形gridImage=myJFrame.getGraphics();
私有无效初始化()
{
对于(int y=0;y网格宽度)
{
newX-=网格宽度;
}
if(newY<0)
{
newY+=网格高度;
}
else if(新建>栅格高度)
{
newY-=网格高度;
}
网格[(int)数学地板(newX)][(int)数学地板(newY)]=真;
gridImage.fillRect((int)Math.floor(newX),(int)Math.floor(newY),1,1);
myJFrame.update(gridImage);
乌龟:setX(newX);
我的乌龟。赛蒂(纽伊);
}