Image fade to black

I would like my Canvas and fade everything that is painted on it and fade it to black. When the character goes to move off the screen, i.e. the road leads off eastward, I want to just fade out and then fade in with the new screen. I've seen some posts using the java.awt.AlphaComposite.getInstance(SCR_OVER) and modifying the alpha value to control the fade, but Im a little hazy on how I would make it fade to black.

publicvoid paint(Graphics g)

{

if( fadingIn || fadingOut )

((Graphics2D)g).setComposite( AlphaComposite.getInstance( AlphaComposite.SCR_OVER, x) ;

g.drawImage( buffer, 0, 0,null ) ;

}

Im modifying the x value( from 0f -> 1f ) during my processing when it is time to fade. The above is what I've tried but it doesnt do anything, but I don't know where to proceed.

[1022 byte] By [yoda23] at [2007-9-27 17:08:51]
# 1

Well, I figured it out. Code posted below in case anyone wants the info.

public void paint(java.awt.Graphics g)

{

if( buffer == null )

{

buffer = new java.awt.image.BufferedImage(300, 450, java.awt.image.BufferedImage.TYPE_INT_ARGB) ;

offScreen = buffer.createGraphics() ;

black = new java.awt.image.BufferedImage(300,450, java.awt.image.BufferedImage.TYPE_INT_RGB );

java.awt.Graphics graph = black.createGraphics() ;

graph.setColor( java.awt.Color.black );

graph.fillRect(0,0,300,450) ;

}

offScreen.drawImage(backgroundImage, 0, 0, null ) ;

offScreen.setComposite( java.awt.AlphaComposite.getInstance( java.awt.AlphaComposite.SRC_OVER, x) );

offScreen.drawImage(black, 0, 0, null ) ;

g.drawImage(buffer, 0, 0, null);

}

In this example, incrementing x from 0 -> 1, fades from the background into black.Use the following formula for calculating ammount of change per frame for the fade. fadeChng = 1/(fps*durationOfFade)

yoda23 at 2007-7-6 11:42:51 > top of Java-index,Other Topics,Java Game Development...
# 2
fadeChng = 1/(fps*durationOfFade) Got chopped off.
yoda23 at 2007-7-6 11:42:51 > top of Java-index,Other Topics,Java Game Development...