- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试用java制作国际象棋游戏,我从谷歌获得了棋子的图像。现在我想将其剪切为 6*2 图像(黑白图像)。但我不明白这个有什么问题。
public static final String PIECES_IMAGE_PATH = "Images/chess_pieces.png";
public static final int PIECE_IMAGES_INROW = 6;
public static final int PIECE_IMAGE_ROWS = 2;
BufferedImage[][] pieceIcons;
private void setupPieceImages(){
try {
BufferedImage image = ImageIO.read(new File(Config.PIECES_IMAGE_PATH));
int pieceImageWidth = image.getWidth()/Config.PIECE_IMAGES_INROW;
int pieceImageHeight = image.getHeight()/Config.PIECE_IMAGE_ROWS;
for(int x = 0; x < pieceImageHeight; x++){
for(int y = 0; y < pieceImageWidth; y++){
try{
pieceIcons[x][y] = image.getSubimage(x*pieceImageHeight,y*pieceImageWidth,pieceImageWidth,pieceImageHeight);
}catch(Exception e){
System.out.println("Error1: "+e.getMessage());
}
}
}
}catch(Exception e){
System.out.println("error2: "+e.getMessage());
}
}
知道为什么我不断收到 Expectation error1: null
最佳答案
我不确定为什么您要迭代宽度/高度的量,但我想您想迭代 2 行,每行 6 列(或者在本例中为 6 列,每列 2 行):
public static final String PIECES_IMAGE_PATH = "Images/chess_pieces.png";
public static final int PIECE_IMAGES_INROW = 6;
public static final int PIECE_IMAGE_ROWS = 2;
public static void main(String args[]) {
BufferedImage[][] pieceIcons = new BufferedImage[PIECE_IMAGES_INROW][PIECE_IMAGE_ROWS];
try {
BufferedImage image = ImageIO.read(new File(Try.class.getResource(PIECES_IMAGE_PATH).toURI()));
int pieceImageWidth = image.getWidth()/PIECE_IMAGES_INROW;
int pieceImageHeight = image.getHeight()/PIECE_IMAGE_ROWS;
for(int x = 0; x < PIECE_IMAGES_INROW; x++){
for(int y = 0; y < PIECE_IMAGE_ROWS; y++){
try{
pieceIcons[x][y] = image.getSubimage(x*pieceImageWidth,y*pieceImageHeight,pieceImageWidth,pieceImageHeight);
}catch(Exception e){
System.out.println("Error1: "+e.getMessage());
}
}
}
}catch(Exception e){
System.out.println("error2: "+e.getMessage());
}
}
您遇到的错误似乎与 Error1
有关,如果您使用意外的最大值初始化 pieceIcons
,则可能会出现 indexOutOfBoundsException
运行时期间的 x/y 量。
关于java - getSubimage 给了我期望 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60121721/
这个问题已经有答案了: Java getSubimage() outside of raster (2 个回答) 已关闭 6 年前。 我编写了一小段代码来将 Sprite 表分割成单独的图像.....
我正在尝试从 BufferedImage 中提取图像的一部分。我该怎么做呢?它不允许我为 y 设置大于零的值。 例如: getSubimage(0,0,theImage.getWidth(),theI
我正在尝试拍摄一张图像并将其存储在 16x16 子图像数组中。我使用的图像是 512x512 像素。但是,在循环迭代时,getSubimage() 因 Raster 异常而停止。 这是代码: publ
我正在尝试拍摄图像并将其存储在 16x16 子图像数组中。我使用的图像是 512x512 像素。但是,在遍历循环时,getSubimage() 因 Raster 异常而停止。 代码如下: public
我正在尝试用java制作国际象棋游戏,我从谷歌获得了棋子的图像。现在我想将其剪切为 6*2 图像(黑白图像)。但我不明白这个有什么问题。 public static final String
嗯,我第一次尝试制作图形游戏,但是: BufferedImage tileset = null; try{ tileset = ImageIO.read(new File("sets/tile
你好, 我正在运行以下代码来制作 JPanel 的屏幕截图。 private void makePanelImage(Component panel, String saveAs) { Dim
我正在开发一个“绘图”应用程序。整个“绘图”不断地存储在 BufferedImage 对象中(使用不断的更新循环)。我们称它为 drawingArea。 我正在使用 BufferedImage 类的
这里是 Java 8。尝试坚持使用 BufferedImage API,而不是深入研究 JavaFx 领域。 我有一个宽 768 像素、高 432 像素的 JPG 图像。我想使用 BufferedIm
所以我正在开发一个裁剪图像的组件,它工作正常。 如果图像大于它的父容器(只能是max-width: 100vw),选择一个的div要裁剪的图像部分具有将在 BufferedImage.getSubIm
我实现了一个 GetSubImage 函数来提取图像的一部分。我使用 SetROI、Copy 和 ResetROI。但是,当并行任务使用相同的图像时,这不起作用,因为 SetROI 不可并行化。 还有
我正在开发 Java 2D 渲染程序(在 1.6.0_11 上运行),它使用外部图像进行 UI 渲染。这些大图像同时包含多个 UI 图形部分,我现在使用 BufferedImage.getSubima
我有一张尺寸为 16x6080 的图片。这是一个包含 16x16 部分的国家标志的堆叠图像。我的目标是从此图像中仅提取特定国家/地区的国旗并将其保存为自己的文件。这是我当前的代码
我目前正在尝试拆分图像,遇到了一个告密者,我不知道为什么会这样。 这是我的函数的快速伪代码分解 使用 ImageIO.read(File file) 方法读入图像 使用 getSubimage() 方
我是一名优秀的程序员,十分优秀!