0

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);
        
    }
    
}

Edited by javaprog200: n/a

2
Contributors
2
Replies
4
Views
5 Years
Discussion Span
Last Post by javaprog200
1

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

Edited by JamesCherrill: n/a

0

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.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.