使用秋千将平铺图像绘制为一张2D平铺地图的大图像 [英] Draw Tile images to one large Image for 2d Tile Map with swing
问题描述
我正在尝试为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,该命令应该将图块Image绘制到我刚刚创建的地图Image上.这是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屋!