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
.