1

调试并发现错误...

我试过这样做:

public void paint(Graphics g) {
        for (int i = 0; i < mapWidth; i++) {
                g.drawRect (0 + i * (windowWidth/mapWidth), 0, windowWidth/mapWidth, windowHeight/mapHeight);
        }
}

它什么也没画,但当我这样做时:

g.drawRect(0, 0, 64, 64);

它完美无缺?

4

2 回答 2

6

因为您在我们的第一个代码中将高度设为零,请参见drawRect的接口:

void    drawRect(int x, int y, int width, int height) 
于 2013-06-13T21:34:43.347 回答
0

这似乎很容易,但通常,错误往往是微不足道的。

您是否检查过您的变量是否确实导致值> 0,即

windowWidth/mapWidth   
//This would be 0 if mapWidth > windowWidth assuming both are ints.

其次,在我看来,您使用了两个表示同一事物的变量:

getMapWidth  //Should this be a method?
mapWidth

使用两个变量不是错误。但是,您可能在两个变量之一中设置了不正确的值。

于 2013-06-13T21:49:23.857 回答