Link to home
Start Free TrialLog in
Avatar of InteractiveMind
InteractiveMindFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Draw image at 50% opacity

Hey,

I am doing something like the following:


            BufferedImage src = null;            // Class Instance
            ...
            Rectangle area = new Rectangle( getX(), getY(), getWidth(), getHeight() );
            try
            {
                Robot robot = new Robot();
                scr = robot.createScreenCapture( area );
            } catch ( Exception e )
            {
                e.printStackTrace();
            }
           
            JPanel pane = new JPanel()
            {
                public void paintComponent( Graphics g )
                {
                    super.paintComponent( g );
                    g.drawImage( scr, 0, 0, this );
                    g.drawImage( new ImageIcon( "bg.gif" ).getImage(), 0, 0, this );
                }
            };
            setContentPane( pane );


I works great, whoo! :-)  However, now, I wish to draw the image - which I draw on with this line:

     g.drawImage( new ImageIcon( "bg.gif" ).getImage(), 0, 0, this );

with an opacity of 50%. How to specify the opactiy of it?

Thanks very much in advance.

Regards;
Avatar of limaideal
limaideal

ASKER CERTIFIED SOLUTION
Avatar of limaideal
limaideal

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of InteractiveMind

ASKER

limaideal,
how would I use this with my program?
Ah... figured it, this is what I need to do:

            JPanel pane = new JPanel()
            {
                public void paintComponent( Graphics g )
                {
                    super.paintComponent( g );
                    g.drawImage( scr, 0, 0, this );
                   
                    Graphics2D g2 = (Graphics2D) g;
                    AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f );
                    g2.setComposite( ac );
                   
                    g2.drawImage( new ImageIcon( "bg.gif" ).getImage(), 0, 0, this );
                }
            };


Cheers.