显示整数的二维数组在Java中的图像 [英] Displaying a 2D array of integers as an image in Java

查看:146
本文介绍了显示整数的二维数组在Java中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Java中创建一个应用程序,将根据整数数组,其中重$ P $每个整数psents颜色上打印图像。有没有一种简单的方法来做到这一点?

 公共无效displayImage(INT [] [] ARR){
    的for(int i = 0; I< arr.length;我++){
        对于(INT J = 0; J<常用3 [0] .length; J ++){
            开关(ARR [I] [J]){
                情况1:
                //帧内打印灰色像素(I,J)
                情况下0:
                //帧内打印绿色像素在(I,J)
                案例2:
                //帧内打印白象素在(I,J)
            }
        }
    }
}
 

解决方案

您可以使用的 的BufferedImage ,如本的例如

补遗:这个例子举的更新图像的底层的的WritableRaster > <$ C $ 使用颜色分量的 INT 阵列,但<一个href="http://docs.oracle.com/javase/7/docs/api/java/awt/image/BufferedImage.html#setRGB%28int,%20int,%20int%29"相对=nofollow> setRGB() 是方便,如果颜色已经可用。

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. 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.

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.

这篇关于显示整数的二维数组在Java中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆