将平铺图像绘制为一张大图像,用于带有摆动的 2d 平铺地图 [英] Draw Tile images to one large Image for 2d Tile Map with swing

查看:25
本文介绍了将平铺图像绘制为一张大图像,用于带有摆动的 2d 平铺地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为 2D Tile 游戏绘制地图,我需要一些帮助.这是应该绘制地图的类.

I am trying to draw a map for a 2D Tile game and I could use some help. Here is the class that is supposed to draw the map.

`package view;
import java.awt.*;
import java.awt.image.BufferedImage;

import model.*;

import javax.swing.*;
public class DrawMap extends JPanel{
    private static  Map map = Map.getMap(); 
    private static Tile[][] field = map.getField();
    BufferedImage im = new BufferedImage(3000,3000,BufferedImage.TYPE_INT_RGB);

    public DrawMap() {
        init();


    }
    public void init(){
        Graphics g = im.getGraphics();
        for(int i = 0; i < 100; i++){
            for(int j = 0; j < 100; j++){
                field[i][j].drawTile(g);
            }
        }
    }
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        g.drawImage(im,0,0,null);

    }

    public static void main(String[] args){
        JFrame frame = new JFrame("GameMap");
        DrawMap draw = new DrawMap();
        frame.add(draw);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

}
`

本质上,这会生成一个将成为地图的新图像,然后我遍历我的 2D 数组并调用 drawTile,它应该将该图块图像绘制到我刚刚创建的地图图像上.这是 drawTile 的样子.我不应该认为 img 是一个私有的 BufferedImage,在我尝试在 drawTile 中设置它之前没有设置任何内容.

Essentially this makes a new Image that is going to be the map, then I go through my 2D array and call drawTile which is supposed to draw that tiles Image to the map Image I just created. Here is what drawTile looks like. I should not that img is a private BufferedImage that is not set to anything until I try to set it here in drawTile.

    'public void drawTile(Graphics g){
    try{
        img = ImageIO.read(new File("src/model/tileTest.png"));
    }catch (IOException e){
        e.printStackTrace(System.out);
    }
    g.drawImage(img, this.getXChord(), this.getYChord(), null);




}'

我在读取图像文件时遇到内存不足错误.如果我让 drawMap 类只搜索前 50 个图块,我会得到一个图像,但它是一堆非常小的图块和 1 个大的图像图块.我应该如何阅读图像,以免出现内存不足错误.如果可能,我想使用缓冲图像.另外我想我在 drawTile 中调用 g.drawImage 时需要使用某种宽度和高度参数,但我不确定从哪里开始.

I am getting an Out of memory error from reading the image file. If I make the drawMap class only search through the first 50 tiles I get an image but it is a bunch of really small tiles and 1 large image tile. How should I be reading in the image so I don't get the out of memory error. Id like to use buffered image if possible. Also Im thinking I will need to use some kind of width and height parameters when I call g.drawImage in drawTile but I am not sure where to begin with that.

感谢您的帮助

推荐答案

您正在为每个磁贴运行此代码:

You're running this code for every tile:

public void drawTile(Graphics g){
    try {
        img = ImageIO.read(new File("src/model/tileTest.png"));
    } catch (IOException e) {
        e.printStackTrace(System.out);
    }
    g.drawImage(img, this.getXChord(), this.getYChord(), null);
}

这意味着您正在重新阅读图像每个图块.对于您渲染的每个图块,每秒 60 次,您都会运行到您的硬盘驱动器并抓取该图像.图像可能缓存在内存中,因此内存不足.

That means you're rereading the image for every tile. For every tile you render, 60 times a second, you're running into your hard drive and grabbing that image. The images might be cached in memory, and thus it's running out of memory.

您绝对应该创建一个图像并多次使用它.例如:

You definitely should be creating one image and using it multiple times. For example:

static Image img = null;

public Tile() {
    if (img == null) {
        try {
            img = ImageIO.read(new File("src/model/tileTest.png"));
        } catch (IOException e) {
            e.printStackTrace(System.out);
        }
    }
}

public void drawTile(Graphics g) {
    g.drawImage(img, this.getXChord(), this.getYChord(), null);
}

随着您获得越来越多的磁贴,您可能需要更可靠的解决方案.但是现在这应该可以解决您的问题.

As you get more and more tiles, you may want a more robut solution. But for now this should solve your problem.

(顺便说一下,e.printStackTrace(System.out)e.printStackTrace() 相同;)

(By the way, e.printStackTrace(System.out) is identical to e.printStackTrace() ;)

这篇关于将平铺图像绘制为一张大图像,用于带有摆动的 2d 平铺地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆