Question

Well I've got my game engine running smoothly, and perfectly on all machines! Before I continue adding functionality to the engine, I want to enhance the engine's graphical capabilities. The one I'm focused on is fading and blending images. I'm going to need some smooth transitions, so without using any 3rd party libraries like OpenGL, how does one apply opacity to images when drawing to a graphics object?

thanks for any replies :D

Was it helpful?

Solution

Perhaps using an AlphaComposite could be what you're looking for?

Image image = new Image(...);
float alpha = 0.5;
@Override
public void paintComponent(Graphics g) {
    Graphics2D g2d = (Graphics2D)g;
    AlphaComposite composite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha);
    g2d.setComposite(composite);
    g2d.drawImage(image, 0, 0, null);
}

You would set your alpha to whatever transparency level you desire (between 0.0 and 1.0). In this case, you would probably want to be using AlphaComposite.SRC_OVER to be able to overlay your transparent image and see behind it, unless your going for something else (if so, you can easily see all available constants and their explanations on the first link provided).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top