gpt4 book ai didi

java - BufferedImage 到 BufferedImage 数组,图 block 消失

转载 作者:行者123 更新时间:2023-12-01 10:48:50 26 4
gpt4 key购买 nike

好吧,Stackoverflow,你是我现在的最后一行。

如果您看一下下面的代码和图片,您会发现有两个文件被立即命名

瓷砖.java

TileMap.java

有关这些类(class)的更多信息,请 Google“ForeignGuyMike Dragon Tale 教程第 8 部分”以获取该项目的下载文件。我现在正在参加他的瓷砖阅读类(class),因为这对我的努力似乎非常有效。切换到 BufferedImage 数组的原因是允许每个图 block 都有动画,截至目前它确实可以处理包含超过 1 帧的图像。

现在这两个类:它们被故意设置为不通过图像进行动画处理,只是为了显示它们打破了该程序,并且没有合并我的成像动画功能。

他们在这里

代码/图像中断之前 enter image description here

public class TileMap {

// position
private double x;
private double y;

// bounds
private int xmin;
private int ymin;
private int xmax;
private int ymax;

private double tween;

// map
private int[][] map;
private int tileSize;
private int numRows;
private int numCols;
private int width;
private int height;

// tileset
private BufferedImage[] tileset;
private int numTilesAcross;
private Tile[][] tiles;

private Animation an;

// drawing
private int rowOffset;
private int colOffset;
private int numRowsToDraw;
private int numColsToDraw;

public TileMap(int tileSize) {
this.tileSize = tileSize;
numRowsToDraw = GamePanel.HEIGHT / tileSize + 2;
numColsToDraw = GamePanel.WIDTH / tileSize + 2;
tween = 0.07;
an = new Animation();
}

public void loadTiles(BufferedImage[] s, int delay) {

an.setDelay(delay);
an.setFrames(s);

try {

tileset = s;

numTilesAcross = tileset[0].getWidth() / tileSize;
tiles = new Tile[2][numTilesAcross];

BufferedImage[] subimage = new BufferedImage[s.length];
for(int col = 0; col < numTilesAcross; col++) {
subimage[0] = tileset[0].getSubimage(
col * tileSize,
0,
tileSize,
tileSize
);
tiles[0][col] = new Tile(subimage[0], Tile.NORMAL);
subimage[0] = tileset[0].getSubimage(
col * tileSize,
tileSize,
tileSize,
tileSize
);
tiles[1][col] = new Tile(subimage[0], Tile.BLOCKED);
}

}
catch(Exception e) {
e.printStackTrace();
}

}

public void loadMap(String s) {

try {

InputStream in = getClass().getResourceAsStream(s);
BufferedReader br = new BufferedReader(
new InputStreamReader(in)
);

numCols = Integer.parseInt(br.readLine());
numRows = Integer.parseInt(br.readLine());
map = new int[numRows][numCols];
width = numCols * tileSize;
height = numRows * tileSize;

xmin = GamePanel.WIDTH - width;
xmax = 0;
ymin = GamePanel.HEIGHT - height;
ymax = 0;

String delims = "\\s+";
for(int row = 0; row < numRows; row++) {
String line = br.readLine();
String[] tokens = line.split(delims);
for(int col = 0; col < numCols; col++) {
map[row][col] = Integer.parseInt(tokens[col]);
}
}

}
catch(Exception e) {
e.printStackTrace();
}

}

public int getTileSize() { return tileSize; }
public double getx() { return x; }
public double gety() { return y; }
public int getWidth() { return width; }
public int getHeight() { return height; }

public int getType(int row, int col) {
int rc = map[row][col];
int r = rc / numTilesAcross;
int c = rc % numTilesAcross;
return tiles[r][c].getType();
}

public void setTween(double d) { tween = d; }

public void setPosition(double x, double y) {

this.x += (x - this.x) * tween;
this.y += (y - this.y) * tween;

fixBounds();

colOffset = (int)-this.x / tileSize;
rowOffset = (int)-this.y / tileSize;

}

private void fixBounds() {
if(x < xmin) x = xmin;
if(y < ymin) y = ymin;
if(x > xmax) x = xmax;
if(y > ymax) y = ymax;
}

public void update() {
an.update();
}

public void draw(Graphics2D g) {

for(
int row = rowOffset;
row < rowOffset + numRowsToDraw;
row++) {

if(row >= numRows) break;

for(
int col = colOffset;
col < colOffset + numColsToDraw;
col++) {

if(col >= numCols) break;

if(map[row][col] == 0) continue;

int rc = map[row][col];
int r = rc / numTilesAcross;
int c = rc % numTilesAcross;

g.drawImage(
tiles[r][c].getImage(),
(int)x + col * tileSize,
(int)y + row * tileSize,
null
);

}

}

}

public class Tile {

private BufferedImage image;
private int type;

// tile types
public static final int NORMAL = 0;
public static final int BLOCKED = 1;

public Tile(BufferedImage image, int type) {
this.image = image;
this.type = type;
}

public BufferedImage getImage() { return image; }
public int getType() { return type; }

现在这是实现更改后的代码

变更集:

瓷砖.java

-构造函数参数已从 BufferedImage 更改为 BufferedImage[]

-BufferedImage图像到BufferedImage[]图像;

-getImage 到 getImage(int i) { return image[i]; }

TileMap.java

-初始化子图像并将其设置为BufferedImage[s.length];

-删除所有子图像[0]到子图像

-draw 中的 getImage 现在是 getImage(0);//硬编码

之后

enter image description here

public class TileMap {

// position
private double x;
private double y;

// bounds
private int xmin;
private int ymin;
private int xmax;
private int ymax;

private double tween;

// map
private int[][] map;
private int tileSize;
private int numRows;
private int numCols;
private int width;
private int height;

// tileset
private BufferedImage[] tileset;
private int numTilesAcross;
private Tile[][] tiles;

private Animation an;

// drawing
private int rowOffset;
private int colOffset;
private int numRowsToDraw;
private int numColsToDraw;

public TileMap(int tileSize) {
this.tileSize = tileSize;
numRowsToDraw = GamePanel.HEIGHT / tileSize + 2;
numColsToDraw = GamePanel.WIDTH / tileSize + 2;
tween = 0.07;
an = new Animation();
}

public void loadTiles(BufferedImage[] s, int delay) {

an.setDelay(delay);
an.setFrames(s);

try {

tileset = s;

numTilesAcross = tileset[0].getWidth() / tileSize;
tiles = new Tile[2][numTilesAcross];

BufferedImage[] subimage = new BufferedImage[s.length];
for(int col = 0; col < numTilesAcross; col++) {
subimage[0] = tileset[0].getSubimage(
col * tileSize,
0,
tileSize,
tileSize
);
tiles[0][col] = new Tile(subimage, Tile.NORMAL);
subimage[0] = tileset[0].getSubimage(
col * tileSize,
tileSize,
tileSize,
tileSize
);
tiles[1][col] = new Tile(subimage, Tile.BLOCKED);
}

}
catch(Exception e) {
e.printStackTrace();
}

}

public void loadMap(String s) {

try {

InputStream in = getClass().getResourceAsStream(s);
BufferedReader br = new BufferedReader(
new InputStreamReader(in)
);

numCols = Integer.parseInt(br.readLine());
numRows = Integer.parseInt(br.readLine());
map = new int[numRows][numCols];
width = numCols * tileSize;
height = numRows * tileSize;

xmin = GamePanel.WIDTH - width;
xmax = 0;
ymin = GamePanel.HEIGHT - height;
ymax = 0;

String delims = "\\s+";
for(int row = 0; row < numRows; row++) {
String line = br.readLine();
String[] tokens = line.split(delims);
for(int col = 0; col < numCols; col++) {
map[row][col] = Integer.parseInt(tokens[col]);
}
}

}
catch(Exception e) {
e.printStackTrace();
}

}

public int getTileSize() { return tileSize; }
public double getx() { return x; }
public double gety() { return y; }
public int getWidth() { return width; }
public int getHeight() { return height; }

public int getType(int row, int col) {
int rc = map[row][col];
int r = rc / numTilesAcross;
int c = rc % numTilesAcross;
return tiles[r][c].getType();
}

public void setTween(double d) { tween = d; }

public void setPosition(double x, double y) {

this.x += (x - this.x) * tween;
this.y += (y - this.y) * tween;

fixBounds();

colOffset = (int)-this.x / tileSize;
rowOffset = (int)-this.y / tileSize;

}

private void fixBounds() {
if(x < xmin) x = xmin;
if(y < ymin) y = ymin;
if(x > xmax) x = xmax;
if(y > ymax) y = ymax;
}

public void update() {
an.update();
}

public void draw(Graphics2D g) {

for(
int row = rowOffset;
row < rowOffset + numRowsToDraw;
row++) {

if(row >= numRows) break;

for(
int col = colOffset;
col < colOffset + numColsToDraw;
col++) {

if(col >= numCols) break;

if(map[row][col] == 0) continue;

int rc = map[row][col];
int r = rc / numTilesAcross;
int c = rc % numTilesAcross;

g.drawImage(
tiles[r][c].getImage(0), // hard code the image at 0
(int)x + col * tileSize,
(int)y + row * tileSize,
null
);

}

}

}

public class Tile {

private BufferedImage[] image;
private int type;

// tile types
public static final int NORMAL = 0;
public static final int BLOCKED = 1;

public Tile(BufferedImage[] image, int type) {
this.image = image;
this.type = type;
}

public BufferedImage getImage(int i) { return image[i]; }
public int getType() { return type; }

最佳答案

对于任何想知道答案的人来说,这个问题已经解决了。

该部分

BufferedImage[] subimage = new BufferedImage[s.length];
for(int col = 0; col < numTilesAcross; col++) {
subimage[0] = tileset[0].getSubimage(
col * tileSize,
0,
tileSize,
tileSize
);
tiles[0][col] = new Tile(subimage, Tile.NORMAL);
subimage[0] = tileset[0].getSubimage(
col * tileSize,
tileSize,
tileSize,
tileSize
);
tiles[1][col] = new Tile(subimage, Tile.BLOCKED);
}

是罪魁祸首。感谢 haraldk 提供的测试此问题的建议,解决方法是将子图像简单地放置在循环中。不断更改内容并使用相同的数组只是将其最后一个内容存储在整个数组中,从而将其搞乱。

更新的代码示例

    for(int col = 0; col < numTilesAcross; col++) {
BufferedImage[] subimage = new BufferedImage[s.length];
subimage[0] = tileset[0].getSubimage(
col * tileSize,
0,
tileSize,
tileSize
);
tiles[0][col] = new Tile(subimage, Tile.NORMAL);
subimage[0] = tileset[0].getSubimage(
col * tileSize,
tileSize,
tileSize,
tileSize
);
tiles[1][col] = new Tile(subimage, Tile.BLOCKED);
}

关于java - BufferedImage 到 BufferedImage 数组,图 block 消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34034572/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com