Я переопределяю альфа-композит компонента рисования, но кажется, что он абсолютно ничего не делает.
Код: Выделить всё
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;
if (opacity >= 1.0f) {
opacity = 1.0f;
timer.stop();
target.setVisible(true); // Make the target interactable
//AllowCustomPainting = false;
//parent.remove(fade_animation.this);
// parent.revalidate(); // Refresh the layout
// parent.repaint(); // Repaint the parent container
}else if(opacity==0.1f){
target.setVisible(true);
}
} else {
opacity -= 0.1f;
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;
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 paintComponent(Graphics g) {
if(AllowCustomPainting){
super.paintComponent(g); // Paint the background (transparent)
if (HasStarted) {
Graphics2D g2 = (Graphics2D) g.create();
// Render the parent content only once
if (!isPaintingParent) {
isPaintingParent = true; // Set the flag to avoid recursion
parent.paintAll(g); // Paint the parent panel content
isPaintingParent = false; // Reset the flag after painting
}
// Apply opacity and render the target component
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, opacity));
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// Calculate and paint the target component area
SwingUtilities.calculateInnerArea(this, innerArea);
SwingUtilities.paintComponent(g2, target, parent, innerArea);
g2.dispose();
}
}
}
}
}
Я пробовал повторно проверять и обновлять методы пользовательского интерфейса для целевого компонента, но это не дало никакого эффекта< /п>
Подробнее здесь: https://stackoverflow.com/questions/793 ... oesnt-work
Мобильная версия