2

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
            }
        }
    }
}
Neil Essy
  • 3,587
  • 1
  • 18
  • 23
Anderson Green
  • 27,734
  • 61
  • 179
  • 311

1 Answers1

6

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.

trashgod
  • 200,320
  • 28
  • 229
  • 974
  • This example is easy to follow. I think this example solved my problem. – Anderson Green Nov 24 '11 at 03:46
  • Excellent. After you've have a chance to tinker with it, you can accept this answer by clicking on the [empty check mark](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work/5235#5235) at the left. – trashgod Nov 24 '11 at 13:54
  • A related example using `SwingWorker` is shown [here](http://stackoverflow.com/a/24977111/230513). – trashgod Aug 01 '14 at 20:36