The following applet displays two .jpg files (ImageIcon objects - x and y) on a JPanel. I would like to display the second ImageIcon object (y) as transparent. Is that possible?

Thank you!

import javax.swing.*;
import java.awt.*;

public class PicPanel extends JApplet
    ImageIcon x, y;
    JPanel panel;
    public void init()
        x = new ImageIcon("image0.jpg");
        y = new ImageIcon ("image1.jpg");
        panel = new JPanel();
    public void paint(Graphics g)
        g.drawImage (x.getImage(), 0, 0, x.getIconHeight(), x.getIconWidth(), panel);
        g.drawImage (y.getImage(), 100, 100, y.getIconHeight(), y.getIconWidth(), panel);

Edited 4 Years Ago by javaprog200: n/a

You can set a compositing rule then draw the second icon with an alpha between 0.0 (totally transparent (invisible)) and 1.0 (not transparent at all (opaque))

Graphics2D g2d = (Graphics2D) g;
// draw image 1
composite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha);
// draw image 2

Have a look at the API doc for AlphaComposite

Edited 4 Years Ago by JamesCherrill: n/a

Hello James,

That was exactly what I needed. It worked very well!!

Thank you!

This question has already been answered. Start a new discussion instead.