0

Is there any way to make a fade animation between two JPanels? I am trying to transition the loading screen into the main menu of the game, this occurs when the loading is fully complete. But for some reason its grey and it also waits until the transperancy (which I am pretty sure I spelled wrong) variable to be equal to zero, and it immediately adds the next JPanel. Here is the code I have come up with ->

package window.title;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JPanel;

import window.DisplayWindow;

public class TitleContents extends JPanel {
    
    
    
    public BufferedImage logo;
    
    public int transperancy = 255;
    
    int arc = 0;

    public boolean loadingCompleted = false;
    
    public TitleContents() {
        
        
        this.setSize(DisplayWindow.WIDTH, DisplayWindow.HEIGHT);
        
        this.setOpaque(false);
        
        this.setBackground(new Color(0, 0, 0, transperancy));
        
        this.setDoubleBuffered(true);
        
        this.setFocusable(true);
        
        
    }
    public void getImages() {
        
        try {
            
            
            logo = ImageIO.read(getClass().getResourceAsStream("/assets/logo/Logo (WHITE).png"));
            
            //
            
            
        } 
        
        catch (IOException e) {
            
            e.printStackTrace();
            
        }
        
    }
    
    public void preparation() {
        
        getImages();
        
    }
    
    public void paintComponent(Graphics g) {
        
        super.paintComponent(g);
        
        
        Graphics2D draw = (Graphics2D) g;
        
        
        draw.setColor(Color.WHITE);
        
        
        draw.setStroke(new BasicStroke(6));
        
        
        draw.drawArc((DisplayWindow.WIDTH/2)-92, (DisplayWindow.HEIGHT/2)-92, 192, 192, 0, arc);
        
        
        draw.drawImage(logo, (DisplayWindow.WIDTH/2)-64, (DisplayWindow.HEIGHT/2)-64, 128, 128, null);
        
    }
    
    public void update() {
        
        this.updateUI();
        
        if(arc < 360) {
        
            arc++;
            
        }
        else {
            
            loadingCompleted  = true;
            
        }
        if(transperancy > 0 && loadingCompleted) {
            
            transperancy--;
            
        }
    }
    
}

It is run in the Display Window File ->

package net.livecrystal.window;

import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

import com.formdev.flatlaf.FlatDarkLaf;

import window.game.GameContents;
import window.title.TitleContents;


public class DisplayWindow implements Runnable{
    
    
    public JFrame frame = new JFrame();
    
    
    public Thread loop = new Thread(this);
    
    
    
    public static final int WIDTH = Toolkit.getDefaultToolkit().getScreenSize().width;
    public static final int HEIGHT = Toolkit.getDefaultToolkit().getScreenSize().height;
    
    public static void main(String[] args) {
        
        FlatDarkLaf.setup();
        
        SwingUtilities.invokeLater(()->new DisplayWindow());

    }
    
    
    TitleContents titleContent = new TitleContents();
    
    GameContents gameContent = new GameContents();


    BufferedImage icon;

    
    public DisplayWindow() {
        
        getRequiredAssets();
        
        frame.setAlwaysOnTop(false);
        
        frame.setResizable(false);
        
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        frame.setTitle("Running Through Life :: Pre-Release");
        
        frame.setIconImage(icon);
        
        frame.setUndecorated(true);
        
        addInSequence();
        
        loop.start();
        
        frame.pack();
        
        frame.setSize(WIDTH,HEIGHT);
        
        frame.setLocationRelativeTo(null);
        
        frame.setVisible(true);
    }

    private void getRequiredAssets() {
        
        try {
            
            
            icon = ImageIO.read(getClass().getResourceAsStream("/net/livecrystal/assets/logo/LiveCrystal Logo (WHITE).png"));
            
            //
            
            
        } 
        
        catch (IOException e) {
            
            e.printStackTrace();
            
        }
        
    }

    private void addInSequence() {
        
        
        titleContent.preparation();
        
        frame.add(titleContent);
        
        
    }

    @Override
    public void run() {
        
        while(loop != null) {
            
            System.out.println(titleContent.transperancy);
            
            updateAllData();
            
            repaintAllPanels();
            
            try {
                
                Thread.sleep(16);
                
            } catch (InterruptedException e) {
                
                e.printStackTrace();
                
            }
            
        }
        
    }

    private void updateAllData() {
        
        if(titleContent.loadingCompleted && titleContent.transperancy == 0) {
            
            frame.add(gameContent);
            
            frame.remove(titleContent);
            
        }
        
        titleContent.update();
        
        gameContent.update();
    }

    private void repaintAllPanels() {
        
        
        titleContent.repaint();
        
        gameContent.repaint();
        
        
    }

}

And for the GameContent class, there isn't really much except its a blue background to further help identifying the difference between TitleContent and GameContent.

  • Do you mean something like [this](https://stackoverflow.com/a/22275584/522444) which uses a Swing Timer and CardLayout to do the transition? – Hovercraft Full Of Eels Apr 21 '23 at 17:22
  • I really wanted to try to make it on my own but I'll give it a try if I have no other choice =) – Grinding For Reputation Apr 21 '23 at 17:31
  • Please define what you mean by "make it on my own". That's what my answer does, only using Swing tools to do so. – Hovercraft Full Of Eels Apr 21 '23 at 17:32
  • oh I see, my bad! But could you maybe try to at least cut out the part that I require and make it into an answer? – Grinding For Reputation Apr 21 '23 at 17:34
  • 1
    It's not that hard to make it on your own, @HovercraftFullOfEels has pointed to a running example you could extract that by: 1) Create a JFrame with 2 panels and a button 2) Make the button do the transition from the answer he has linked in his comment above. – Frakcool Apr 21 '23 at 17:43
  • note: calling updateUI is utterly __wrong__! it's called after changing the ui delegate, nothing else – kleopatra Apr 22 '23 at 09:49
  • btw: please edit the code and format it properly - in particular, the excessive and unneeded vertical whitespace makes it hard to read. Also make sure it's really a [mcve] (no external references, nothing unrelated to the problem) – kleopatra Apr 22 '23 at 09:50

0 Answers0