Results 1 to 7 of 7
  1. #1
    BariMutation is offline Member
    Join Date
    Oct 2010
    Posts
    22
    Rep Power
    0

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

    Java Code:
    public static void displayImage(int[][] arrayTwo) 
    	{
        	int imageWidth = arrayTwo.length;
    		int imageHeight = arrayTwo[0].length;
        
    		BufferedImage showImage = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_4BYTE_ABGR);
    
          JFrame frame = new JFrame();
          JLabel label = new JLabel(new ImageIcon(showImage));
          frame.getContentPane().add(label, BorderLayout.CENTER);
          frame.pack();
          frame.setVisible(true);    
       }

    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! :)

  2. #2
    venerik is offline Member
    Join Date
    Oct 2010
    Posts
    94
    Rep Power
    0

    Default

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

    Cheers,
    Erik
    I'm new to Java but I like to help where ever I can. :)

  3. #3
    BariMutation is offline Member
    Join Date
    Oct 2010
    Posts
    22
    Rep Power
    0

    Default

    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.

    Java Code:
    public static int[][] readImageintoArray(String filename) 
    	{
    		File f = new File(filename);
    		BufferedImage readimage = null;
    		try 
    			{
             	readimage = ImageIO.read(f);
            	} 
    		catch (IOException e) 
    			{
                e.printStackTrace();
            	}
    		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;
                	}
            	}
          return GrayScaleData;
    	}

  4. #4
    BariMutation is offline Member
    Join Date
    Oct 2010
    Posts
    22
    Rep Power
    0

    Default

    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.

  5. #5
    venerik is offline Member
    Join Date
    Oct 2010
    Posts
    94
    Rep Power
    0

    Default

    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?

    Cheers,
    Erik
    I'm new to Java but I like to help where ever I can. :)

  6. #6
    BariMutation is offline Member
    Join Date
    Oct 2010
    Posts
    22
    Rep Power
    0

    Default

    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?

  7. #7
    venerik is offline Member
    Join Date
    Oct 2010
    Posts
    94
    Rep Power
    0

    Default

    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.
    I'm new to Java but I like to help where ever I can. :)

Similar Threads

  1. Displaying an image
    By Samari413 in forum New To Java
    Replies: 10
    Last Post: 11-06-2010, 09:56 PM
  2. Regarding problem with displaying image in jsp
    By sandeepsai39 in forum New To Java
    Replies: 4
    Last Post: 10-04-2010, 05:23 PM
  3. Displaying an image
    By SWEngineer in forum AWT / Swing
    Replies: 11
    Last Post: 01-28-2010, 06:48 AM
  4. displaying image into screen !
    By burningflower12 in forum AWT / Swing
    Replies: 14
    Last Post: 11-16-2009, 03:02 AM
  5. Displaying image in same jsp
    By SreenivasGurramkonda in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 12-08-2008, 07:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •