0

I am trying to make a game for my semester project. I want to show a transition when user clicks on options button of my game menu or when user clicks on credits button. I want to show transition when one panel replaces another. Is it even possible? I am using java swing library.

1 Answers1

3

You should use a CardLayout to swap views (JPanels) in your GUI. To get a fade effect is not the most simple thing to do, but it can be done with a little work. I've done it successfully here where I create a special class called SwappingImgPanel that extends JPanel, and that fades one image into another using a Swing Timer. Specifically, the program does this:

  • The program adds all the swapping components to the CardLayout using JPanel.
  • It also adds a single SwappingImgPanel, a JPanel created to draw two images, one of the component that is fading out, and one of the component that is fading in.
  • When you swap components, you create images of the two components, the one currently visible, and the one that will next be visible.
  • You send the images to the SwappingImgPanel instance
  • You call swap() on the SwappingImgPanel instance.
  • The SwappingImgPanel will then draw both images but uses a Swing Timer to change the Graphic object's composite value. This is what causes an image to be partially visible.
  • When the SwappingImgPanel's Timer is done, a done() method is called which sets the SwappingImgPanel's State to State.DONE.
  • The main GUI is listening to the SwappingImgPanel's state value, and when it achieves State.DONE, the main GUI shows the actual next component (and not an image of it).
Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373