Displaying an image
I'm trying to display an image using JPanel (it's pretty much the first time I've used this), but I'm having some troubles. Here is what I have so far...
public static void displayImage(int arrayTwo)
int imageWidth = arrayTwo.length;
int imageHeight = arrayTwo.length;
BufferedImage showImage = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_4BYTE_ABGR);
JFrame frame = new JFrame();
JLabel label = new JLabel(new ImageIcon(showImage));
I have a previous method that works perfectly fine that turns the image (which the user entered through a string named "filename") into a 2D array. That all is fine, but this certainly isn't, and I'm not sure why. I know this is a simple question, but thanks to anyone who helps! :)
What is the problem you are having? Does it not compile? Does it give runtime errors?
By the way, you are not using the content of your 2D array, only the dimensions...
Oh, sorry, I didn't explain myself. The values of the array are being passed into it via the main method, hence the dummy array of "int array". So the values are there, just not directly within this method. If it helps, the method that passes the image into a 2D array is at the bottom of my post (I've tested it and it works perfectly).
What's confusing is that it compiles and runs without any errors... but the window that pops up is just gray. No detail, just a gray block.
public static int readImageintoArray(String filename)
File f = new File(filename);
BufferedImage readimage = null;
readimage = ImageIO.read(f);
catch (IOException e)
int imageWidth = readimage.getWidth();
int imageHeight = readimage.getHeight();
int GrayScaleData = new int[imageWidth][imageHeight];
for (int i = 0; i < imageHeight; i++)
for (int j = 0; j < imageWidth; j++)
int pixel = readimage.getRGB(j, i);
GrayScaleData[j][i] = (((pixel >> 16) & 0xff) + ((pixel >> 8) & 0xff) + ((pixel) & 0xff)) / 3;
And by posting that I just realized my problem... that method is turning the image into a grayscale, which is the opposite of what I want - I need it to be in color.
Okay, so, in short, how can I display the image with full color? Or is this something beyond my understanding? lol I'm doing this for extra credit, so it's not absolutely needed - hence me not knowing what to do or if I'm even capable of doing it.
I still think that the code in you first post does not use the data of your array.
I guess you pass the result of readImageintoArray to displayImage? So you are passing the data correctly but inside displayImage you only use this data to determine the size of the image. Shouldn't you iterate through the data and do showImage.setRGB(xxx) to copy the pixels?
I know it's using the data (I tested it using a System.out.println), so that's not the issue.
What you said made perfect sense, except the "showImage.setRGB(xxx)" part. What would go in the xxx? The pixel location?
Yep, xxx should be x, y and the value of the pixel (from arrayTwo).
Do you need the bufferedimage to be of type TYPE_4BYTE_ABGR? When I changed it to TYPE_3BYTE_BGR the picture was shown on my screen.