【问题标题】:Java tile based map from array positioning来自数组定位的基于 Java tile 的地图
【发布时间】:2012-10-08 16:29:11
【问题描述】:

我不明白为什么我的代码会产生如下所示的内容。我希望图块在窗口中对齐。窗口大小在主类中设置:

this.getContentPane().setPreferredSize(new Dimension(WINDOW_X, WINDOW_Y));

这是我的地图类。

public class Map1 {

int mapx = 19;
int mapy = 19;

int tileWidth = 600 / (mapx + 1);
int tileHeight = 500 / (mapy + 1);

int[][] map =      {{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
                    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
                    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
                    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
                    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
                    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
                    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
                    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
                    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
                    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
                    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
                    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
                    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
                    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
                    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
                    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
                    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
                    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
                    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
                    {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}};

public void drawLevel(Graphics g){
    g.setColor(Color.GREEN);

    for (int x = 0; x <= mapx; x++){

        for (int y = 0; y <= mapy; y++){

            int L = x * tileWidth;
            int U = y * tileHeight;
            int R =  tileWidth;
            int D =  tileHeight;

            if (map[y][x] == 1){
                //g.fillRect((x * tileWidth), (y * tileHeight), (x + tileWidth), (y + tileHeight));
                g.fillRect(L, U, R, D);
            }

            }

        }

    }

}

http://www.flickr.com/photos/88246499@N05/8067438151/

【问题讨论】:

    标签: java map tile


    【解决方案1】:

    drawLevel 的嵌套for 循环中,您使用变量y 来索引图块地图的x 坐标,并使用x 来索引y 坐标。当您定义LU 时会出现问题,因为您乘以错误的图块尺寸。因为在你的循环内x 是沿着你必须写int U = x*tileHeight;int L = y*tileWidth 的高度的度量。

    我在下面的代码中交换了两个 for 循环,所以 xy 具有共同的含义。

    for (int y = 0; y <= mapy; y++){
        for (int x = 0; x <= mapx; x++){
            int L = x * tileWidth;
            int U = y * tileHeight;
            int R =  tileWidth;
            int D =  tileHeight;
            if (map[x][y] == 1){
                g.fillRect(L, U, R, D);
            }
        }
    
    }
    

    我希望这能解决你的问题。

    【讨论】:

    • 谢谢,这更有意义,但是瓷砖被绘制在边框后面而不是可见屏幕的左上角。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-12
    • 2014-04-20
    • 1970-01-01
    • 2012-02-13
    • 1970-01-01
    相关资源
    最近更新 更多