Java将字符更改为2D图形

时间:2023-02-09 15:38:09

I open a text file which contains a maze, that consists of # and spaces. Where a hash tag represents a "wall" and the spaces are the area that you can move through.

我打开一个包含迷宫的文本文件,其中包含#和空格。散列标记表示“墙”,空格是您可以穿过的区域。

Currently I have opened the file and stored everything in a 2d array so that the console prints out in the exact format as the text file.

目前我已打开文件并将所有内容存储在2d数组中,以便控制台以与文本文件相同的格式打印出来。

My next stage that I am trying to accomplish is, if the char is a hash tag, fill the rectangle with a dark grey, if its a space fill it with white. At the moment for some reason the maze is showing up but appearing really small.

我想要完成的下一个阶段是,如果char是一个哈希标记,用深灰色填充矩形,如果它的空格用白色填充它。目前由于某种原因迷宫出现但看起来很小。

I don't know if I took the right approach but I am trying to modify the method I have rather than creating another method.

我不知道我是否采取了正确的方法,但我试图修改我的方法,而不是创建另一种方法。

Essentially I want to fill up that whole 600x400 with my maze rather than it being so small. I thought setting the size of the rectangle would achieve this.

基本上我想用我的迷宫填满整个600x400而不是它那么小。我认为设置矩形的大小可以达到这个目的。

What I am trying is:

我在想的是:

public static void mazeFrame() {
    JFrame f = new JFrame("Maze");
    f.setSize(400, 600);
    f.setLocationRelativeTo(null);
    f.add(new Exercise4());
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
}

// ...

    int i = 0; //y axis

    if (c == '#') {
        cells[i][j] = true;
        g.setColor(Color.DARK_GRAY);
        g.drawRect(j,i, 50,50);
    } else {
        cells[i][j] = false;
        g.setColor(Color.white);
        g.drawRect(j,i, 50,50);

    }
  }
  i++;
}

But my output is

但我的输出是

Java将字符更改为2D图形

1 个解决方案

#1


2  

You are not scaling your maze.

你没有缩放你的迷宫。

 g.drawRect(j,i, 50,50);

The values for j and i both start at 0 and are incremented by one.
As a consequence, your 50x50 blocks overlap.

j和i的值都从0开始并递增1。因此,您的50x50块重叠。

If you'd add a scale factor, your maze would be bigger:

如果您添加比例因子,您的迷宫会更大:

 g.drawRect(xScale * j, yScale * i, 50,50);

Since you intend to use 50x50 blocks, the right value for xScale and yScale would be 50. You could go on from there and use:

由于您打算使用50x50块,因此xScale和yScale的正确值将为50.您可以从那里继续使用:

 g.drawRect(xScale * j, yScale * i, xScale, yScale);

Once you've got that working, you should look into the AffineTransform class, which will allow you a lot more options. But that's for later.

一旦你有了工作,你应该查看AffineTransform类,这将允许你更多的选择。但这是为了以后。

#1


2  

You are not scaling your maze.

你没有缩放你的迷宫。

 g.drawRect(j,i, 50,50);

The values for j and i both start at 0 and are incremented by one.
As a consequence, your 50x50 blocks overlap.

j和i的值都从0开始并递增1。因此,您的50x50块重叠。

If you'd add a scale factor, your maze would be bigger:

如果您添加比例因子,您的迷宫会更大:

 g.drawRect(xScale * j, yScale * i, 50,50);

Since you intend to use 50x50 blocks, the right value for xScale and yScale would be 50. You could go on from there and use:

由于您打算使用50x50块,因此xScale和yScale的正确值将为50.您可以从那里继续使用:

 g.drawRect(xScale * j, yScale * i, xScale, yScale);

Once you've got that working, you should look into the AffineTransform class, which will allow you a lot more options. But that's for later.

一旦你有了工作,你应该查看AffineTransform类,这将允许你更多的选择。但这是为了以后。