简体   繁体   English

在Java中将2D整数数组显示为图像

[英]Displaying a 2D array of integers as an image in Java

I'm trying to create an application in Java that will print an image based on an array of integers where each integer represents a color. 我正在尝试用Java创建一个应用程序,该应用程序将基于整数数组打印图像,其中每个整数代表一种颜色。 Is there a straightforward way to accomplish this? 有没有简单的方法可以做到这一点?

public void displayImage(int[][] arr){
    for(int i = 0; i < arr.length; i++){
        for(int j = 0; j < arr[0].length; j++){
            switch(arr[i][j]){
                case 1:
                //print a gray pixel at (i, j) within a frame
                case 0:
                //print a green pixel at (i, j) within a frame
                case 2:
                //print a white pixel at (i, j) within a frame
            }
        }
    }
}

You can use BufferedImage , as shown in this example . 您可以使用BufferedImage ,如本示例所示。

Addendum: The example cited updates the image's underlying WritableRaster using an int array of color components, but setRGB() is convenient if the color is already available. 附录:引用的示例使用int颜色分量数组更新图像的基础WritableRaster ,但如果颜色已经可用,则setRGB()很方便。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM