[英]Is there a simple way to have a fade/animation change from JButton Icon to Rollover Icon?
波紋管是我的按鈕代碼之一
continueGame = new JButton("");
continueGame.setIcon(new ImageIcon("resources/buttonUI/continueGame.png"));
continueGame.setRolloverEnabled(true);
continueGame.setRolloverIcon(new ImageIcon("resources/buttonUI/continueGameOnHover.png"));
continueGame.setBorderPainted(false);
continueGame.setContentAreaFilled(false);
continueGame.setBounds(frameSize.x/2-343/2, (int)((frameSize.y)*0.28), 343, 85);
continueGame.addActionListener(this);
效果很好,但是當我將按鈕懸停時,它立即從基本圖標更改為RolloverIcon。 有沒有辦法讓它們之間變淡或變化緩慢?
“簡單”是主觀的
“最大”的問題是試圖找出繪畫應該出現的“位置”。
該圖標和滾動圖標由UI委托繪制。 這確實很痛苦,因為您確實不想為可能要使用的每種外觀設計UI委托。
因此,您需要自己接管很多功能。
以下方法基本上使用委托模式來減輕“普通”和“翻轉”圖標的混合,並相應地設置按鈕的icon
屬性(這是一種作弊行為)
基本功能是由Swing Timer
控制的,有關更多詳細信息,請參見如何使用Swing計時器 。
它是基於時間的,也就是說,它是在指定的時間段內運行,而不是一直運行直到獲得一個值。 通常這會產生更好的動畫效果。
import java.awt.AlphaComposite;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ButtonModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class JavaApplication155 {
public static void main(String[] args) {
new JavaApplication155();
}
public JavaApplication155() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
} catch (IOException ex) {
ex.printStackTrace();
}
}
});
}
public class TestPane extends JPanel {
public TestPane() throws IOException {
JButton btn = new JButton();
BufferedImage cat = ImageIO.read(getClass().getResource("cat.png"));
BufferedImage dog = ImageIO.read(getClass().getResource("dog.png"));
btn.setIcon(new ImageIcon(cat));
btn.setRolloverEnabled(true);
RolloverAnimator animator = new RolloverAnimator(btn, cat, dog);
setLayout(new GridBagLayout());
add(btn);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
public class RolloverAnimator {
private JButton btn;
private BufferedImage normalImage;
private BufferedImage rolloverImage;
private double progress;
private Timer timer;
private int totalAnimationTime = 300;
private int animationTime = totalAnimationTime;
private Long startedAt = null;
private boolean wasRolledOver = false;
public RolloverAnimator(JButton btn, BufferedImage normalImage, BufferedImage rolloverImage) {
this.btn = btn;
this.normalImage = normalImage;
this.rolloverImage = rolloverImage;
ButtonModel model = btn.getModel();
model.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
if (model.isArmed()) {
return;
}
if (model.isRollover() && !wasRolledOver) {
wasRolledOver = true;
prepare();
} else if (!model.isRollover() && wasRolledOver) {
wasRolledOver = false;
prepare();
}
}
});
timer = new Timer(5, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (startedAt == null) {
startedAt = System.currentTimeMillis();
}
long duration = System.currentTimeMillis() - startedAt;
progress = (double) duration / (double) animationTime;
if (progress > 1.0) {
progress = 1.0;
startedAt = null;
timer.stop();
}
BufferedImage target = transition(normalImage, rolloverImage, progress);
btn.setIcon(new ImageIcon(target));
if (!timer.isRunning()) {
progress = 0.0;
}
}
});
}
protected void prepare() {
if (timer.isRunning()) {
timer.stop();
animationTime = (int) ((double) totalAnimationTime * progress);
startedAt = System.currentTimeMillis() - (totalAnimationTime - (long)animationTime);
} else {
animationTime = totalAnimationTime;
startedAt = null;
}
progress = 0.0;
timer.start();
}
public BufferedImage transition(BufferedImage from, BufferedImage to, double progress) {
int width = Math.max(from.getWidth(), to.getWidth());
int height = Math.max(from.getHeight(), to.getHeight());
BufferedImage target = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = target.createGraphics();
float normalAlpha = wasRolledOver ? 1.0f - (float) progress : (float) progress;
float rollOverAlpha = wasRolledOver ? (float) progress : 1.0f - (float) progress;
g2d.setComposite(AlphaComposite.SrcOver.derive(normalAlpha));
draw(from, target, g2d);
g2d.setComposite(AlphaComposite.SrcOver.derive(rollOverAlpha));
draw(to, target, g2d);
g2d.dispose();
return target;
}
protected void draw(BufferedImage img, BufferedImage on, Graphics2D g2d) {
int xPos = (img.getWidth() - on.getWidth()) / 2;
int yPos = (img.getHeight() - on.getHeight()) / 2;
g2d.drawImage(img, xPos, yPos, btn);
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.