1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
|
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class ArrowFadeIn extends JPanel implements ActionListener{
private BufferedImage im;
private Timer timer;
private float alpha = 0f;
private int delay = 10;
private float alphaIncrement = 0.01f;
public float getAlphaIncrement() {
return alphaIncrement;
}
public void setAlphaIncrement(float alphaIncrement) {
this.alphaIncrement = alphaIncrement;
}
public int getDelay() {
return delay;
}
public void setDelay(int delay) {
this.delay = delay;
}
/**
*
*/
public ArrowFadeIn(BufferedImage im ) {
super();
this.im = im;
timer = new Timer(delay,this);
// TODO Raccord de constructeur auto-généré
}
@Override
protected void paintComponent(Graphics g) {
// TODO Raccord de méthode auto-généré
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
g2d.drawImage(im, 0, 0, null);
}
public void startAnim() {
timer.start();
}
public void actionPerformed(ActionEvent e) {
// TODO Raccord de méthode auto-généré
System.out.println("alpha="+alpha);
if(alpha>=1.0f - alphaIncrement) {
alpha = 1.0f;
repaint();
timer.stop();
} else {
repaint();
alpha+=alphaIncrement;
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Raccord de méthode auto-généré
JFrame f = new JFrame();
f.setSize(100, 100);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
try {
ArrowFadeIn fade = new ArrowFadeIn(ImageIO.read(new File("arrow_right.png")));
fade.setBackground(Color.black);
fade.setAlphaIncrement(0.05f);
fade.setDelay(50);
f.add(fade);
f.setVisible(true);
fade.startAnim();
} catch (IOException e) {
// TODO Bloc catch auto-généré
e.printStackTrace();
}
}
} |
Partager