How can I make my Java Swing application 'support' image transparency?

I'm making a minesweeper game in java. For this, I'm using a JFrame with custom JButtons. At the end of the game, I disable the JFrame and I want to add an animation (say, of the exploding mine s/he just clicked) at a point on the board.

The player is forced to watch this until it finishes, and then make the decision to play again. Now, I've made a class for this animation, it takes the relevant images and stores them in an ArrayList and appropriately uses my JFrame Graphics context to drawImage().

Currently, it's drawing over itself, and I don't like this behaviour. Other than changing the way my whole game works, is there some way I can grab a rectangular box of what's painted on screen and use this as the background for my animation?

1 Answer

Relevance
  • 9 years ago
    Favorite Answer

    What you describe is animation. You would need BufferedImage and transfer the soft image to the JPanel using paintComponent( Graphics g ) and Java2D. Bulky code, but boilerplate. "Filthy Rich Clients" is the best book for describing flashy Swing apps.

    Or, you can wait for the 2.2 version of JavaFX, the scripting framework to make java perform as a 'stage' with an animation loop.

Still have questions? Get your answers by asking now.