我目前正在尝试创建一个填充纯色的形状,然后将其输出为 PNG。这是我的代码。
void CreateRedImage(int xSize, int ySize, String FileName){
BufferedImage bf = new BufferedImage(xSize, ySize, BufferedImage.TYPE_INT_RGB);
Color color = new Color(225, 000, 000);
File f = new File(FileName + ".png");
bf.setRGB(xSize, ySize, color.getRGB());
try {
ImageIO.write(bf, "PNG", f);
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
不幸的是,当我运行我的代码时,我收到了这个错误消息。
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Coordinate out of bounds!
at sun.awt.image.IntegerInterleavedRaster.setDataElements(IntegerInterleavedRaster.java:301)
at java.awt.image.BufferedImage.setRGB(BufferedImage.java:988)
at ImageCreation.CreateBlueImage(ImageCreation.java:53)
at Main.main(Main.java:12)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
现在,我知道问题出在以下行:
bf.setRGB(xSize, ySize, color.getRGB());
我无法弄清楚为什么我的代码不起作用。有人有想法吗?