InteractiveMind
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;
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(
} 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;
Try http://java.sun.com/j2se/1.4.2/docs/api/java/awt/AlphaComposite.html
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
limaideal,
how would I use this with my program?
how would I use this with my program?
ASKER
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 (AlphaComp osite.SRC_ OVER, 0.5f );
g2.setComposite( ac );
g2.drawImage( new ImageIcon( "bg.gif" ).getImage(), 0, 0, this );
}
};
Cheers.
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
g2.setComposite( ac );
g2.drawImage( new ImageIcon( "bg.gif" ).getImage(), 0, 0, this );
}
};
Cheers.