So I'm making a stopmotion application - the live feed from my webcam is inside a JPanel (with setOpaque(false)) that is inside the top (10th) layer in a JLayeredPane - and basically, when I take a picture, I want to add it to one of the lower layers so that a trace of the previous pictures you've taken shows up on screen. Here's how I'm trying to do that now:
EDIT: this is my new code based off the answer below - this now does nothing, as opposed to just adding the opaque image as before - if I add this to a JPanel, though, and add the JPanel to the JLayeredPane, then all I get is gray
BufferedImage img2 = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g = img2.createGraphics();
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .5f));
g.drawImage(img2, 0, 0, null);
g.dispose();
ImageIcon imgIcon = new ImageIcon(new ImageIcon(img2).getImage().getScaledInstance(img2.getWidth(), img2.getHeight(), Image.SCALE_SMOOTH));
JLabel showPic = new JLabel(imgIcon);
showPic.setSize(layers.getSize());
showPic.setBounds(layers.getX() + 18, layers.getY(), img2.getWidth(), img2.getHeight());
layers.add(showPic, new Integer(1)); //layers is my JLayeredPane
layers.repaint();
layers.revalidate();
img is the picture I've just captured from my webcam, and I'm trying to make it semi transparent, then add it to a JLabel. How can I make this work? Or is there a better way to do this?
I don't know if this will solve the problem but the image you are looking for is
java.awt.Transparency.TRANSLUCENT has the value of 3 and corresponds to BufferedImage.TYPE_INT_ARGB_PRE which only god knows what it's doing.
With BufferedImage.TYPE_INT_ARGB you can create a transparent image and apply Composite etc
You also need to correct this
to
--
Heres how transparency works for me:
I have two images bim and bim2 and I draw one on top of the other:
Then I can display it on a JPanel Jframe or whatever.
I can even create the Label: