gpt4 book ai didi

java - 光栅格式异常 (Java)

转载 作者:行者123 更新时间:2023-12-01 15:22:18 25 4
gpt4 key购买 nike

我正在尝试使用一些代码,例如在另一个问题答案中看到的代码:https://stackoverflow.com/a/621849/1044984

使用此方法后,我收到以下错误:

Exception in thread "AWT-EventQueue-0" java.awt.image.RasterFormatException: (y + height) is outside of Raster
at sun.awt.image.ByteInterleavedRaster.createWritableChild(Unknown Source)
at java.awt.image.BufferedImage.getSubimage(Unknown Source)
at main.Grid.paintComponent(Grid.java:111)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JLayeredPane.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paintToOffscreen(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
at javax.swing.RepaintManager.paint(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
at java.awt.Container.paint(Unknown Source)
at java.awt.Window.paint(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.seqPaintDirtyRegions(Unknown Source)
at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

以下是与此错误相关的代码:

try {

tileSheetBig = ImageIO.read(new File("sprites/tiles.png"));
charSheetBig = ImageIO.read(new File("sprites/player.png"));

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

final int tileWidth = 64;
final int tileHeight = 64;
final int tileRows = 1;
final int tileCols = 11;
tileSheet = new BufferedImage[tileRows * tileCols];

for (int i = 0; i < tileRows; i++) {
for (int j = 0; j < tileCols; j++) {
tileSheet[(i * tileCols) + j] = tileSheetBig.getSubimage(i
* tileWidth, j * tileHeight, tileWidth, tileHeight);
}
}

final int charWidth = 16;
final int charHeight = 23;
final int charRows = 2;
final int charCols = 3;
charSheet = new BufferedImage[charRows * charCols];

for (int i = 0; i < charRows; i++) {
for (int j = 0; j < charCols; j++) {
charSheet[(i * charCols) + j] = charSheetBig.getSubimage(i
* charWidth, j * charHeight, charWidth, charHeight);
}
}

由于答案中提供的代码没有太大变化,我看不出问题可能是什么。我尝试用 google 搜索该错误,但与我的问题相关的答案并不多。

最佳答案

那个RasterFormatExceptionthrown当 [ x,y : x+width, y+height] 指定的区域不包含在 BufferedImage 区域内时,通过 getSubImage() 执行。

检查您的tiles.png图像是否至少为704x64像素(宽度*列数,高度*行数),同样,player.png图像至少为48x46像素。

编辑:抱歉,我第一眼没有注意到; player.png 必须为 32x69 像素,tiles.png 必须为 64x704 像素

编辑2:这可以修复玩家的代码,而无需编辑 Sprite ;对瓷砖做同样的事情

final int charWidth = 64;
final int charHeight = 64;
final int charCols = 11;
final int charRows = 1;
for (int i = 0; i < charCols; i++) {
for (int j = 0; j < charRows; j++) {
charSheet[i * charRows + j] = charSheetBig
.getSubimage(i * charWidth, j * charHeight, charWidth, charHeight);
}
}

关于java - 光栅格式异常 (Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10730028/

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