Guest
JPanel Fade-Animation Java Swing funktioniert nicht
Post
by Guest » 18 Jan 2025, 21:50
Aktualisierter Code:
Code: Select all
package JAnimator;
import java.awt.AlphaComposite;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public class JAnimator {
public static class fade_animation extends JPanel {
private float opacity = 1.0f;
private final Rectangle innerArea = new Rectangle();
private Timer timer;
private JComponent target;
private final JPanel parent;
private int Delay;
private boolean HasStarted = false;
private boolean isIn = true;
boolean isPaintingParent=false;
boolean AllowCustomPainting = true;
public fade_animation(JComponent targetComponent, int SpeedMilisec, int DelayMilisec) {
this.target = targetComponent;
parent = (JPanel) target.getParent();
this.Delay = DelayMilisec;
setSize(target.getSize());
setLocation(target.getLocation());
parent.add(this);
// Timer to control the fade effect
timer = new Timer(SpeedMilisec, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (isIn) {
opacity += 0.1f;
repaint();
if (opacity >= 1.0f) {
opacity = 1.0f;
timer.stop();
AllowCustomPainting = false;
// Revalidate the layout to ensure components are updated
}else if(opacity==0.1f){
}
} else {
opacity -= 0.1f;
repaint();
if (opacity {
try {
Thread.sleep(Delay + 200);
HasStarted = true;
timer.restart();
} catch (InterruptedException ex) {
Logger.getLogger(JAnimator.class.getName()).log(Level.SEVERE, null, ex);
}
}).start();
} catch (Exception e) {
}
}
public void playOut() {
isIn = false;
opacity = 1.0f;
try {
new Thread(() -> {
try {
Thread.sleep(Delay + 200);
HasStarted = true;
target.hide();
timer.restart();
} catch (InterruptedException ex) {
Logger.getLogger(JAnimator.class.getName()).log(Level.SEVERE, null, ex);
}
}).start();
} catch (Exception e) {
}
}
@Override
public Dimension getPreferredSize() {
Dimension size = target.getPreferredSize();
return size;
}
@Override
protected void paintChildren(Graphics g) {
Graphics2D g2 = (Graphics2D) g.create();
if (AllowCustomPainting) {
// Paint the background (transparent)
super.paintComponent(g);
if (HasStarted) {
g2.setComposite(AlphaComposite.SrcOver.derive(opacity));
super.paintChildren(g2);
g2.dispose();
}
} else {
if (isIn) {
repaint();
target.paint(g2); // Directly paint the target component
parent.updateUI(); // Update the UI to reflect any changes
target.setVisible(true);
} else {
target.setVisible(false);
}
}
}
}
}
Wie kann ich alle Komponenten gezielt malen, da dies nur manuell funktioniert? Ich habe bereits versucht super.paintChildren(g2);
1737233445
Guest
[b]Aktualisierter Code:[/b] [code]package JAnimator; import java.awt.AlphaComposite; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; import java.awt.RenderingHints; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JComponent; import javax.swing.JPanel; import javax.swing.SwingUtilities; import javax.swing.Timer; public class JAnimator { public static class fade_animation extends JPanel { private float opacity = 1.0f; private final Rectangle innerArea = new Rectangle(); private Timer timer; private JComponent target; private final JPanel parent; private int Delay; private boolean HasStarted = false; private boolean isIn = true; boolean isPaintingParent=false; boolean AllowCustomPainting = true; public fade_animation(JComponent targetComponent, int SpeedMilisec, int DelayMilisec) { this.target = targetComponent; parent = (JPanel) target.getParent(); this.Delay = DelayMilisec; setSize(target.getSize()); setLocation(target.getLocation()); parent.add(this); // Timer to control the fade effect timer = new Timer(SpeedMilisec, new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (isIn) { opacity += 0.1f; repaint(); if (opacity >= 1.0f) { opacity = 1.0f; timer.stop(); AllowCustomPainting = false; // Revalidate the layout to ensure components are updated }else if(opacity==0.1f){ } } else { opacity -= 0.1f; repaint(); if (opacity { try { Thread.sleep(Delay + 200); HasStarted = true; timer.restart(); } catch (InterruptedException ex) { Logger.getLogger(JAnimator.class.getName()).log(Level.SEVERE, null, ex); } }).start(); } catch (Exception e) { } } public void playOut() { isIn = false; opacity = 1.0f; try { new Thread(() -> { try { Thread.sleep(Delay + 200); HasStarted = true; target.hide(); timer.restart(); } catch (InterruptedException ex) { Logger.getLogger(JAnimator.class.getName()).log(Level.SEVERE, null, ex); } }).start(); } catch (Exception e) { } } @Override public Dimension getPreferredSize() { Dimension size = target.getPreferredSize(); return size; } @Override protected void paintChildren(Graphics g) { Graphics2D g2 = (Graphics2D) g.create(); if (AllowCustomPainting) { // Paint the background (transparent) super.paintComponent(g); if (HasStarted) { g2.setComposite(AlphaComposite.SrcOver.derive(opacity)); super.paintChildren(g2); g2.dispose(); } } else { if (isIn) { repaint(); target.paint(g2); // Directly paint the target component parent.updateUI(); // Update the UI to reflect any changes target.setVisible(true); } else { target.setVisible(false); } } } } } [/code] Wie kann ich alle Komponenten gezielt malen, da dies nur manuell funktioniert? Ich habe bereits versucht super.paintChildren(g2);