Results 1 to 8 of 8

Thread: Image issues

  1. #1
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    384
    Rep Power
    4

    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):
    Java 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++)
          {
            int pixeldata=shipimg.getRGB(x, y);
            int val1=pixeldata&0x000000FF;
            int val2=(pixeldata>>8)&0x000000FF;
            int val3=(pixeldata>>16)&0x000000FF;
            int val4=(pixeldata>>24)&0x000000FF;
            if (val1==0 && val2==0 && val3==0)
            {
              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?
    Last edited by Toll; 05-19-2011 at 02:25 PM. Reason: Added a little more code

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Comment on the code: Why shift each byte? Why not just AND with a bit mask with the xFF in the correct location?
    Or why not just a single AND?

  3. #3
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    384
    Rep Power
    4

    Default

    If I just masked with 0x0000FF00, I'd have to shift it in the end anyway or check it against 255*255. Sure, might've been quicker, codewise, but it's something that only runs once after loading the image, so I opted for a little better readability rather than optimum performance. I could've checked if pixeldata&0x00FFFFFF==0 as well, but wanted to make sure I didn't lose myself somewhere.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Your code took several scans for me to see what you are trying to do. Plus the added code leaves room for errors.
    What is it you want to test? That the low order 24 bits are zeros. One AND tests that.

  5. #5
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    384
    Rep Power
    4

    Default

    I wanted to make sure that the RGB-part of the int were the lower 24 bits and that the alpha was the upper 8 bits, which is why I opted for this solution. And yeah, it's perhaps not the most optimal solution, but I wanted to make sure everything were where I thought it would be.

    And as I said, the testing works; it sets the pixels to 0 where they're supposed to be. They're still not transparent when I try to draw the image out though.

  6. #6
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    384
    Rep Power
    4

    Default

    Crossposted to Image issues

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    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.
    Compositing Graphics (The Java Tutorials > 2D Graphics > Advanced Topics in Java2D)

    db

  8. #8
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    384
    Rep Power
    4

    Default

    Ah... The addition of an AlphaComposite (and a bit of fiddling) seems to have solved the problem. I'll mark it as solved in 24 hours if there are no further issues with it.

Similar Threads

  1. Replies: 12
    Last Post: 04-14-2011, 01:58 PM
  2. Rotating Buffered Image distorts image
    By VortexSpin in forum Java 2D
    Replies: 1
    Last Post: 02-13-2011, 05:54 AM
  3. Replies: 1
    Last Post: 04-17-2009, 12:44 AM
  4. Replies: 14
    Last Post: 04-10-2009, 08:46 AM
  5. Replies: 0
    Last Post: 01-08-2008, 05:29 PM

Posting Permissions

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