从平铺贴图加载子图像

时间:2013-07-25 11:52:50

标签: java arrays image awt 2d

我正在努力开发我的第一款游戏。我在加载从地图中提取的Tiles时遇到了严重问题。 这是我到目前为止编写的内容,只是愿意显示第一个图块。

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

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分: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