Hello,

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();
        add(panel);
    }
    
    
    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);
        
    }
    
}

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);
g2d.setComposite(composite);
// draw image 2

Have a look at the API doc for AlphaComposite

Hello James,

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

Thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.