2

I hope you can give me some advices to solve my problem. I need to overlay many images on a button.but the problem is, this is the base image (tooth): (http://i.imgur.com/7tIcP.gif)

my first image is this: http://i.imgur.com/FYuD8.gif and then I put this: http://i.imgur.com/mWz9c.gif the first image overlaps the second so I just can see only the second image...

maybe you will tell me that one option is change the order of the image before overlay, but the user will select what will be the first, maybe just want the first image, but in other cases user will put the first AND then the second or vice versa...

my code is this:

    BufferedImage large=null;
    large = ImageIO.read(new File("firstimage.gif"));

    BufferedImage small=null;

    small = ImageIO.read(new File("secondimage.gif"));

    int w = Math.max(large.getWidth(), small.getWidth());
    int h = Math.max(large.getHeight(), small.getHeight());

    BufferedImage combined = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);

    // paint both images, preserving the alpha channels
    Graphics g = combined.getGraphics();
    g.drawImage(large, 0, 0, null);
    g.drawImage(small, 0, 0, null);

    ImageIO.write(combined, "PNG", new File("twoInOne.png"));

    ImageIcon icon1 = new ImageIcon(combined);
    jbutton1.setIcon(icon1);

Maybe is a format of the images issue, or my code, but I prefer that you guys can help me with this problem thank you.

Now I uploaded the 3 images: I skip the base image (tooth) cause I dont think it will be the problem in there.

7
  • 1
    Why does the code state GIF while both those images are JPG? Jan 9, 2013 at 16:54
  • When the user wants both, what is supposed to happen? What should it look like? Jan 9, 2013 at 16:56
  • Freezerburn understand the objetive... the first image needs overlay the second image without overwirte the first image... Jan 9, 2013 at 17:00
  • mm... I rly dont get it... the links I'd posted are just the examples... my original images are in .gif format Jan 9, 2013 at 17:01
  • So the parts of image 2 that are not fully transparent (e.g. blue/red whatever) are supposed to be semi-transparent? Jan 9, 2013 at 17:02

3 Answers 3

4

I need to edit my images to get transparent background?

To make a particular color transparent, you can iterate through the pixels of a BufferedImage or use a suitable LookupOp. For the latter, see the articles cited here. You can then combine the images using drawImage(). The default composite rule, AlphaComposite.SRC_OVER, should be satisfactory; if not, you can change it, as shown here.

1
  • this works! now I can put the 3 images together realy thank you trashgod. Jan 10, 2013 at 3:13
1

Your code should be fine for combining two images together. However, like you said, your two images are of the same size, and they do not seem to have any transparency. This will cause whatever image is drawn second to always "overwrite" the first image in the newly-combined image.

The solution you probably want for this, is to break the various pieces you want to overlay on top of each other out into separate, smaller images. With your images, it looks like you want to have various overlays on top of a tooth to display various pieces of information. You'll want to have three things in this case: an image of a tooth, an image containing the red overlay, and an image containing the blue overlay. All three of these images should have a transparent, and not white, background so that they don't overwrite colors in any previously-drawn image. When you do this, you'll want to draw the tooth, then overlay 1 (red/blue) then overlay 2 (red/blue). This should get you the output you're looking for.

3
  • m... If Im wrong you tell me that I need to edit my images to get transparent background? in fact the problem is the images? Jan 9, 2013 at 16:57
  • Depends on whether you're on Windows, Mac, or Linux. You need to get an image editor and remove the white background, then preferably save it as a PNG (which allows transparency in the image). Windows you can use Paint.NET, Mac you can use SeaShore, and the only program I know for Linux is GIMP, which is a Photoshop replacement so it's pretty complicated. Jan 9, 2013 at 17:04
  • "in fact the problem is the images?" You claim they are GIF, which support 'all or none transparency'. The images at that site are JPEG, which does not support transparency in any form. Only PNG supports partial transparency and would be suited to directly drawing one over the other to see parts of both at the same time. Perhaps you should re-upload the images to imgur.com so we can see them properly. Jan 9, 2013 at 17:05
0

The key is to set the alpha to float value, say two layer, set alpha to 0.5, three layer, set alpha 0.33, four layer, set alpha 0.25 ... Anyway, here is the code example

try
{
    BufferedImage imgA = ImageIO.read(new File(imgAPath, token));
    BufferedImage imgB = ImageIO.read(new File(imgBPath, token));

    if (imgA.getWidth() == imgB.getWidth() && imgA.getHeight() == imgB.getHeight()) 
    {
        float alpha = 0.5f;
        int compositeRule = AlphaComposite.SRC_OVER;
        AlphaComposite ac;
        int imgW = imgA.getWidth();
        int imgH = imgA.getHeight();
        BufferedImage overlay = new BufferedImage(imgW, imgH, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = overlay.createGraphics();
        ac = AlphaComposite.getInstance(compositeRule, alpha);
        g.drawImage(imgA,0,0,null);
        g.setComposite(ac);
        g.drawImage(imgB,0,0,null);
        g.setComposite(ac);
        ImageIO.write(overlay, "PNG", new File(logFolder, browser+"__"+token));
        g.dispose();
    }
    else
    {
        System.err.println(token+" dymension not match ");
    }
}
catch (IOException e)
{
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.