1

我正在尝试开发我的第一个游戏。我在加载从地图中提取的瓷砖时遇到了严重的问题。这是我到目前为止编写的代码,只是愿意显示第一个图块。

public class TileMap {

    //FIELDS
    public int Tilemapx;
    public int Tilemapy;
    private int nRows = 8;
    private int nColumns = 8;
    public int TileWidth = Panel.WIDTH / nColumns;
    public int TileHeight = Panel.HEIGHT / nRows;
    public BufferedImage EntireMap;
    public BufferedImage SingleTile;

    public TileMap (){
        String EntireMappath = "res/Tiles.png";
        try {
            EntireMap = ImageIO.read(new File (EntireMappath));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void drawTile (){
        for (int Tilemapx = 0; Tilemapx < Panel.WIDTH / nColumns; Tilemapx++){
            for (int Tilemapy = 0; Tilemapy < Pannel.HEIGHT / nRows; Tilemapy++){
                BufferedImage SingleTile = EntireMap.getSubimage(100, 100, Tilemapx, Tilemapy);

            }
        }
    }
}

并且,在 Panel 类中,我正在尝试绘制图块,但没有出现任何内容(前两个图像按预期工作)

public void draw() {
    BufferStrategy bs = getBufferStrategy();
    if (bs == null) {
        createBufferStrategy(3);
        return;
    }

    Graphics g = bs.getDrawGraphics();
    g.drawImage(bg, 0, 0, getWidth(), getHeight(), null);
    g.drawImage(heroImg1, hero.cordX, hero.cordY, hero.diameter, hero.diameter, null);
    g.drawImage(tiles.SingleTile, 100, 200, tiles.Tilemapx, tiles.Tilemapx, null);
    g.dispose();
    bs.show();
}
4

2 回答 2

0

您应该提供一种方法,TileMap该方法返回给定图块的适当子图像。然后,在 中draw,您可以调用该方法,获取图像,并将其绘制在图形上。如果您想绘制整个电路板,只需使用循环即可。

于 2013-07-25T12:03:46.670 回答
0
public void drawTile (){

// THIS IMAGE SingleTile SHOULD BE GLOBAL AND THIS IMAGE SHOULD BE RETURNED GLOBAL FROM A METHOD

BufferedImage SingleTile = EntireMap.getSubimage(100, 100, Tilemapx, Tilemapy);


// get the graphics context from what you want to paint onto
Graphics ga = OTHERIMAGE_OR_BUFFERSTRATEGY_OR_PANEL.getGraphics(); //or getDrawGraphics() for bufferstrategy

//then draw on it ga.drawImge(....................


}//END METHOD
于 2013-07-25T13:44:27.750 回答