Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: unable to display BufferedImage with RGBA colorspace

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default unable to display BufferedImage with RGBA colorspace

    Hello,

    I have some problems displaying a BufferedImage in a GUI.

    The program I'm writing uses a set of rules to generate an integer array representing an image in RGBA. I have something like this:

    Values[0] = 1st pixel / R component
    Values[1] = 1st pixel / G component
    Values[2] = 1st pixel / B component
    Values[3] = 1st pixel / A component
    Values[4] = 2nd pixel / R component
    Values[5] = 2nd pixel / G component
    ...

    I am then creating a BufferedImage like this:
    this.image = new BufferedImage(this.width, this.height, BufferedImage.TYPE_INT_ARGB);
    this.image.setRGB(0, 0, this.width, this.height, Values, 0, this.width);

    When verifying if that BufferedImage contains the right values, I found that it does.
    But the problem is, I can't display it. I tried painting in on a JPanel or JLabel, creating an ImageIcon out of it... It is as if it was completely transparent although the alpha values are varying.



    this is the crucial part of the code:

                public void step(JLabel view) {
     
    		BufferedImage image = this.displayGrid.computeGraphics(this.cellGrid); // Construction of the BufferedImage with random
    		                                                                                              // values
    		int[] results = image.getRGB(0, 0, this.width, this.height, null, 0, this.width);
     
    		for(int i = 0; i < results.length; i++)
    			System.out.println(results[i]); // to see the values contained in 'image'
     
                           ImageIcon icon = new ImageIcon(image);
    		view.setIcon(icon); // this doesn't seem to do anything although it should !
    	}
    When i run this code, everytime the function step() is called, The program should display a new BufferedImage in the Jlabel 'view'.
    What I actually get is that everything seems to work fine but 'view' stays empty throughout the execution...


    Any help is appreciated and sorry if I'm doing something dumb :^)
    Thanks in advance
    Last edited by Cereno; October 15th, 2012 at 01:23 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,302
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: unable to display BufferedImage with RGBA colorspace

    Can you post an SSCCE (and please use the code tags)?

  3. #3
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: unable to display BufferedImage with RGBA colorspace

    Just a side note.... Does Red or Alpha go first in an ARGB ordering?



    int[] results = image.getRGB
    Wait, are we dealing with RGB or ARGB?



    int[] results = image.getRGB(0, 0, this.width, this.height
    Does this JComponent have a specified width and height at this time?



    I would include a few more sysouts and refer to the docs on these jparts to get a feel of what is going on.


    PS please be a kind hearted individual and fix the code with code tags too. See the page you should have read before posting for help.

  4. #4
    Junior Member
    Join Date
    Oct 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: unable to display BufferedImage with RGBA colorspace

    Quote Originally Posted by jps View Post
    Just a side note.... Does Red or Alpha go first in an ARGB ordering?
    I am not sure, i think it comes last but I could be wrong. The point is that each component in each pixels of my BufferedImage is set randomly to be between 0 and 255, so seeing Nothing when trying to display it is kind of unrealisticly improbable.

    Quote Originally Posted by jps View Post
    Wait, are we dealing with RGB or ARGB?
    Actually getRGB returns an array of integer pixels in the default RGB color mode which happen to be ARGB.
    see here

    Quote Originally Posted by jps View Post
    Does this JComponent have a specified width and height at this time?
    Yes it does. My window is 600x600 and the image is 200x200 so there should be plenty of space to fit it inside.

  5. #5
    Junior Member
    Join Date
    Oct 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: unable to display BufferedImage with RGBA colorspace

    Here is my SSCCE. Sorry for not thinking of that sooner.

    import java.awt.image.BufferedImage;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
     
     
    public class Main {
     
    	public static void main(String args[]) {
     
    	JFrame window = new JFrame();
    	JPanel panel = new JPanel();
    	JLabel label = new JLabel();
     
    	int width = 100;
    	int height = 100;
     
    	int[] RGBA = new int[width*height*4];
     
    	for(int index = 0; index < RGBA.length; index++) {
     
    		RGBA[index] = 50; // I didn't reproduced the randomness involved here
     
    	}
     
    	BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    	image.setRGB(0, 0, width, height, RGBA, 0, width);
     
    	ImageIcon icon = new ImageIcon(image);
    	label.setIcon(icon);
     
    	window.setSize(600, 600);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	window.setLocation(0, 0); // default is 0,0 (top left corner)
    	window.setResizable(false);
    	window.setVisible(true);
     
    	panel.add(label);
     
    	window.getContentPane().add(panel);
     
    	}
     
    }

    This shows straightforwardly the problem I'm having. The window that this code generates is empty...
    Last edited by Cereno; October 15th, 2012 at 01:49 PM.

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,302
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: unable to display BufferedImage with RGBA colorspace

    The BufferedImage has an alpha component, which your RGB values are setting to 0 (transparent). Set the alpha component to an appropriate value, or change the image type to exclude the alpha (for instance BufferedImage.TYPE_INT_RGB). For instance, try setting the RGBA values to
    RGB[index] = (int)(255 << 24) | 50;//bitshift the 255 value into the a component of the int

  7. #7
    Junior Member
    Join Date
    Oct 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: unable to display BufferedImage with RGBA colorspace

    How can the alpha component be set to zero when i do this?

    for(int index = 0; index < RGBA.length; index++) {
     
    		RGBA[index] = 50; // I didn't reproduced the randomness involved here
     
    	}

    This loop sets every value of the RGBA colorspace to 50, including the alpha, if I'm correct.

  8. #8
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,302
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: unable to display BufferedImage with RGBA colorspace

    Quote Originally Posted by Cereno View Post
    This loop sets every value of the RGBA colorspace to 50, including the alpha, if I'm correct.
    The loop sets every pixel value to alpha = 0, red = 0, green = 0, and blue = 50. You need to manipulate each pixel's four values (presuming an TYPE_INT_ARGB image type) to get the color you wish. The code I posted above bitshifted 255 into the alpha position, you can do something similar for each subsequent position.

  9. #9
    Junior Member
    Join Date
    Oct 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: unable to display BufferedImage with RGBA colorspace

    Quote Originally Posted by copeg View Post
    The loop sets every pixel value to alpha = 0, red = 0, green = 0, and blue = 50. You need to manipulate each pixel's four values (presuming an TYPE_INT_ARGB image type) to get the color you wish.
    That's exactly what I am doing since my RGBA array looks like this:

    Values[0] = 1st pixel / R component
    Values[1] = 1st pixel / G component
    Values[2] = 1st pixel / B component
    Values[3] = 1st pixel / A component
    Values[4] = 2nd pixel / R component
    Values[5] = 2nd pixel / G component

    Since I put 50 in every cell of the array, I am therefore setting an alpha of 50 too.
    I understood that it is supposed to work that way since changing the size of my RGBA array gives an ArrayIndexOutOfBoundsException during the call to setRGB().

    edit:

    I just tried your code with the bitshift and I can confirm that it works, it displays the BufferedImage now. However I'm rather confused... Could you try to explain to me the difference between your line and mine? Thank you anyways.
    Last edited by Cereno; October 15th, 2012 at 04:14 PM.

  10. #10
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,302
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: unable to display BufferedImage with RGBA colorspace

    Quote Originally Posted by Cereno View Post
    That's exactly what I am doing since my RGBA array looks like this:
    That is not what you are doing...the int values are not treated that way. With type INT_ARGB, each int value represents a pixel - an int is 32 bytes, the least significant 8 bits are blue, the most significant are alpha, etc...I recommend reading the API for the setRGB method you are using

    There are only 8-bits of precision for each color component in the returned data when using this method. With a specified coordinate (x, y) in the this image, the ARGB pixel can be accessed in this way:

    pixel = rgbArray[offset + (y-startY)*scansize + (x-startX)];
    Edit: a more explicit example
    int a = 255;
    int r= 0;
    int g= 0;
    int b= 255;
    int pixel = (a << 24) | (r << 16 ) | (g << 8 ) | b;//bitshift each color (0-255) 8 bit value to the appropriate pixel position
    RGBA[index] = pixel;
    Last edited by copeg; October 15th, 2012 at 04:26 PM.

  11. The Following User Says Thank You to copeg For This Useful Post:

    Cereno (October 15th, 2012)

  12. #11
    Junior Member
    Join Date
    Oct 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: unable to display BufferedImage with RGBA colorspace

    I understand now. I never suspected the rgba values needed to be packed that way.

    Thank you so very much for your time. Thanks to you, my code is working now

  13. #12
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,302
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: unable to display BufferedImage with RGBA colorspace

    Quote Originally Posted by Cereno View Post
    I understand now. I never suspected the rgba values needed to be packed that way.

    Thank you so very much for your time. Thanks to you, my code is working now
    You are welcome. Glad to be of help. If all your questions have been answered, please mark this thread as solved (go to the top toolbar 'thread tools')

Similar Threads

  1. Replies: 3
    Last Post: July 17th, 2012, 11:59 PM
  2. Constructing 48 bit RGB bufferedimage
    By themadmathematician in forum AWT / Java Swing
    Replies: 2
    Last Post: March 20th, 2012, 02:49 PM
  3. [SOLVED] unable to store, display date and time from db to extjs grid
    By VaniRathna in forum Java Servlet
    Replies: 3
    Last Post: November 16th, 2011, 08:36 AM
  4. Creating a bufferedimage!
    By Vexst in forum Java ME (Mobile Edition)
    Replies: 0
    Last Post: June 16th, 2010, 08:05 AM
  5. Replies: 2
    Last Post: June 29th, 2009, 03:06 PM