Hi!
I’m using the following code to do a fade transition effect between two images:
public void render(Graphics g, Image imageFrom, Image imageTo, int width, int height) {
BufferedImage buffer=gc.createCompatibleImage(width,height);
Graphics2D g2 = (Graphics2D)buffer.getGraphics();
int counter=0;
while (counter<90) {
counter=counter+2;
double alphaScalar = Math.sin(Math.toRadians(counter));
g2.setComposite(AlphaComposite.SrcOver);
g2.drawImage(imageFrom, 0, 0, null);
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, (float)alphaScalar));
g2.drawImage(imageTo, 0, 0, null);
g.drawImage(buffer, 0, 0, null);
try {
Thread.sleep(5);
}
catch(InterruptedException ex){}
}
g.dispose();
g2.dispose();
buffer.flush();
}
My main problem is that using a buffer to save the image composite before drawing it over the panel’s Graphic context slows the effect considerably. I have other effects (scrolling, sliding and some more) which don’t require the use of an intermediate buffer and they are way faster without it. I’m looking for a way to improve the perfomance of my fade effect. I’ve tried to draw the image composite over the panel’s Graphic context directly (no buffer) but the screen flickers. Can anyone help me, please ?
Regards