I.ve problem with java image processing.I've to convert 2d array into image.I'm using the loadPixels function, but it doesen't work.The Image is never shown on the label.
Please help, here is the code of the loadPixels function:

public static void loadPixels(JLabel label, int[][] pixels) {
		// 1. convert to 1-d array
		int width  = pixels.length;
		int height = pixels[0].length; 
		int i=0;    // index into new pixarray
		int[] pixarray = new int[width*height];
		for (int y=0; y<height; y++)
		for (int x=0; x<width;  x++)
			// these are stored y-major!
			pixarray[i++] = pixels[x][y];
		// 2. convert 1-d array into Image*/
		MemoryImageSource source = new MemoryImageSource(width,height,pixarray,0,width);
		
	 imageto=label.createImage(source);   // imageto is declared in the top of the class as "private static Image"
		
		// 3. store Image in the JLabel
		ImageIcon imic=new ImageIcon(imageto);
		
		//imic=image;
		label.setIcon(imic);		
	}

Any help would be greatly appreciated.

Thanks,

Nina

It may simply be a matter of incorrectly specifying the pixel values, because when I use this code (from the example in the MemoryImageSource API)

for (int y = 0; y < height; y++) {
            int red = (y * 255) / (height - 1);
            for (int x = 0; x < width; x++) {
                int blue = (x * 255) / (width - 1);
                pixarray[i++] = (255 << 24) | (red << 16) | blue;
            }
        }

in place of your loop to fill 'pixarray' from the pixels param, it works just fine.

This article has been dead for over six months. Start a new discussion instead.