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 8 of 8

Thread: Image issues

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Image issues

    I've been trying to play around a little with images lately, and I ran into one issue that I've not found any reason for (so I think it's because of a misunderstanding on my part). What I'm trying to do is to draw images that overlap eachother, but parts of the images should be transparent so I can see several images stacked onto eachother. However, no matter what I do I can't make that happen. I make them transparent by doing this (I've preloaded the images before running this code):
        BufferedImage shipimg=new BufferedImage(tempimg.getWidth(null), tempimg.getHeight(null), BufferedImage.TYPE_INT_ARGB);
        Graphics tempg=shipimg.getGraphics();
        tempg.drawImage(tempimg, 0, 0, null);
        for (int x=0;x<shipimg.getWidth(null);x++)
        {
          for (int y=0;y<shipimg.getHeight(null);y++)
          {
            if ((shipimg.getRGB(x, y)&0x00FFFFFF)==0) // If the pixel is black
            {
              System.out.println("Setting "+x+":"+y+" blank.");
              shipimg.setRGB(x, y, 0);
            }
          }
        }
    It prints out the right amounts of "Setting blank" and loops through the right amount of loops, so I doubt there are any problems with the image not being completely loaded, and if I try to read the RGB back it says 0. It still draws the color as black when I use g.drawImage(shipimg, x, y) though (which draws onto another BufferedImage with TYPE_INT_ARGB). Any thoughts?

    Also posted at Image issues


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

    Default Re: Image issues

    Have you tried playing with the alpha value for a region of consecutive pixels? Right now it looks to be searching for pixels that are white with alpha 0, then setting that pixel to black with alpha 0...with 100% transparency of both the previous and changed pixel I wouldn't expect the picture to change.

  3. #3
    Junior Member
    Join Date
    May 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Image issues

    I think you're confusing the mask for the color I'm looking for. I'm masking out the last 24 bits out of the int (i.e. the RGB code) and checking if that RGB code is 0 (i.e. black). I don't care much about the transparency of the original picture (I'm fairly certain the image I'm playing with doesn't contain any alpha-values anyway). Also, none of the pixels become transparent; each new image covers the previous ones, including the black square border.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Image issues

    I think you're confusing the mask for the color I'm looking for. I'm masking out the last 24 bits out of the int
    Serves me right for posting without drinking my coffee. But I'm still a bit confused...you are setting the alpha to zero on only a few pixels (I presume, how many pixels are being set?). What do you expect to see? Just for kicks, loop through the image and set the alpha to something like 100, then draw onto another image to get an idea of what truly is going on.

  5. #5
    Junior Member
    Join Date
    May 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Image issues

    The image is roughly 70-80% black, I'd estimate, so it's more than just a few pixels. I'm also currently drawing one image per x-coordinate (the image is, I believe 64 pixels wide) and randomize on which y-coordinate it's drawn; with a screensize of 640x480, it means I'm drawing 640 images from right to left. The result: I often only see the tip of the ships before the next image covers it.

  6. #6
    Junior Member
    Join Date
    May 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Image issues

    Let's see how these images turn out...

    This is how it is right now:

    This is how I want it:

    (and yes, the image in question is taken from another game for testing purposes).

  7. #7
    Junior Member
    Join Date
    May 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Image issues

    It can be added that I just changed the Graphics to Graphics2D, but that made no change whatsoever. I'm starting to think that either I've completely misunderstood the alpha-channel, or Java doesn't support it that well (if at all)...

  8. #8
    Junior Member
    Join Date
    May 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Image issues

    After a few pointers from the other forum, I found out about and added an AlphaComposite and fiddled a bit more, and might've gotten it to work. I'll mark it as solved in 24 hours if there are no further issues with it.

Similar Threads

  1. Alignment issues
    By fride360 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: March 10th, 2011, 02:58 PM
  2. Replies: 2
    Last Post: February 14th, 2011, 05:36 PM
  3. Having Issues Past this
    By baGoGoodies111 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 12th, 2009, 08:19 PM
  4. Pixel Alteration in an image/image rotation
    By bguy in forum Java Theory & Questions
    Replies: 3
    Last Post: November 1st, 2009, 10:50 AM
  5. Replies: 0
    Last Post: October 2nd, 2009, 10:51 PM