繁体   English   中英

来自数组定位的基于Java Tile的地图

[英]Java tile based map from array positioning

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

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/

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

}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM