坐标超出 .setRGB 范围

Coordinate is out of bounds from .setRGB

我正在尝试创建一个程序来生成图像并将每个像素着色为相同的值,然后将文件保存到 a.PNG。我目前收到坐标越界错误,并尝试了几种方法来调试它。我认为这个问题与我的讲师编写的一些第三方代码有关,因为我打印了 p 值,它是负的,但我认为它需要在 0 到 255 之间。

目前,它没有生成任何 PNG 文件。

有人可以告诉我如何修复此代码吗?

import java.util.Scanner;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter; 

public class Lab_Week8_ImageFromText {

    public static void main(String[] args) throws Exception  {  

    int image_width = 3;
    int image_height = 3;
    String output_file_path = ("image.png");
    String input_file_path = "";
    int r = 10;
    int b = 10;
    int g = 10;

    BufferedImage imagetowrite = imageFromText(image_width, image_height, input_file_path, output_file_path, r, g, b); 
    File f = new File (output_file_path);
    ImageIO.write(imagetowrite, "png", f);

    }

    public static BufferedImage imageFromText (int image_width, int image_height, String input_file_path, String output_file_path, int r, int g, int b) throws IOException{

        if(image_width <= 0 || image_height <= 0) {
            System.err.println("Width and Height have to be strictly positive!");
            return null;
        }

        System.out.println("Hello");

        BufferedImage image = new BufferedImage (image_width, image_height, BufferedImage.TYPE_INT_ARGB);

        int x = 1;
        int y = 1;
        for (x = 1; x <= image_width; x++)
        {//System.out.println (x);
        for (y = 1; y <= image_height; y++){//System.out.println(y);
            setPixel(image, x, y, r, g, b);
    }}

        System.out.println("Hello 2" + image);
        return image;
    }

    public static void setPixel(BufferedImage image, int x, int y, int r, int g, int b) {
        /*
         * Test cases for error management
         */
        if(x < 0 || y < 0) {
            System.err.println("Coordinates (x, y) cannot be negative numbers!");
            return;
        }
        if(r < 0 || r > 255 || g < 0 || g > 255 || b < 0 || b > 255) {
            System.err.println("Colour values (r, g, b) have to be between 0 and 255!");
            return;
        }
        /*
         * Set the alpha value for the pixel, i.e. the transparency of the pixel, to its max value
         */
        int a = 255;
        /*
         * Write the different value of the pixel, i.e. the colours red, green, blue and alpha.
         * The different colour values are all stored into a single integer using byte operators.
         * a = x << y means write the value of x as bytes at the address y in the object a.
         * a = o1 | o2 means a will be composed by o1 and o2, where o1 and o2 are binary operators.
         * It is necessary to use this operator because the setRGB method of BufferedImage 
         * take only one integer that have to hold all the colour values for one given pixel.
         */
        int p = (a << 24) | (r << 16) | (g << 8) | b;
        /*
         * Use the setRGB method from BufferedImage to write the new pixel value (i.e. colour) to the image
         */
        image.setRGB(x, y, p);
        System.out.println(p);
    }

}

这里是 p 的输出和错误信息:

Hello
-16119286
-16119286
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:1016)
        at Lab_Week8_ImageFromText.setPixel(Lab_Week8_ImageFromText.java:79)
        at Lab_Week8_ImageFromText.imageFromText(Lab_Week8_ImageFromText.java:44)
        at Lab_Week8_ImageFromText.main(Lab_Week8_ImageFromText.java:22)

这里的问题是新手程序员对数组索引的常见误解。大多数现代编程语言(关键包括 Java)索引长度为 size 的数组,第一个元素位于索引 0,最后一个元素位于 size-1

在下面引用的程序的两个循环中,您允许索引等于数组的长度/size。这意味着在某些时候,程序将尝试访问 size 处的元素。

for (x = 1; x <= image_width; x++){
    //System.out.println (x);
    for (y = 1; y <= image_height; y++){
        //System.out.println(y);
        setPixel(image, x, y, r, g, b);
    }
}

因此,要更正这些循环,您需要像这样设置边界:

for (x = 0; x < image_width; x++){
    //System.out.println (x);
    for (y = 0; y < image_height; y++){
        //System.out.println(y);
        setPixel(image, x, y, r, g, b);
    }
}

请注意,我们现在从 0 开始并在 image_whatever - 1 结束,这意味着您的两个循环中的最后一次调用实际上是这样的:

setPixel(image, image_width-1, image_height-1, r, g, b);

我希望这能帮助你理解你的错误。