• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:Crossfadeで画像の切り替え
#navi(../)
RIGHT:Posted by [[terai]] at 2007-03-12
*Crossfadeで画像の切り替え [#o6388e71]
>編集者:[[Terai Atsuhiro>terai]]~
作成日:2007-03-12~
更新日:&lastmod;

#contents

**概要 [#l463e8d1]
Crossfadeアニメーションで画像の切り替えを行います。

-&jnlp;
-&jar;
-&zip;

#screenshot

**サンプルコード [#s306a237]
#code{{
 class Crossfade extends JComponent implements ActionListener {
   private final javax.swing.Timer animator;
   private final ImageIcon icon1;
   private final ImageIcon icon2;
   private int alpha = 10;
   private boolean direction = true;
   public Crossfade(ImageIcon icon1, ImageIcon icon2) {
     this.icon1 = icon1;
     this.icon2 = icon2;
     animator = new javax.swing.Timer(50, this);
   }
   public void animationStart() {
     direction = !direction;
     animator.start();
   }
   public void paintComponent(Graphics g) {
     Graphics2D g2d = (Graphics2D)g;
     g2d.setPaint(getBackground());
     g2d.fillRect(0, 0, getWidth(), getHeight());
     if(direction && alpha<10) {
       alpha = alpha + 1;
     }else if(!direction && alpha>0) {
       alpha = alpha - 1;
     }else{
       animator.stop();
     }
     g2d.setComposite(AlphaComposite.getInstance(
       AlphaComposite.SRC_OVER, 1.0f-alpha*0.1f));
     g2d.drawImage(icon1.getImage(), 0, 0,
       (int)icon1.getIconWidth(), (int)icon1.getIconHeight(), this);
     g2d.setComposite(AlphaComposite.getInstance(
       AlphaComposite.SRC_OVER, alpha*0.1f));
     g2d.drawImage(icon2.getImage(), 0, 0,
       (int)icon2.getIconWidth(), (int)icon2.getIconHeight(), this);
   }
   public void actionPerformed(ActionEvent e) {
     repaint();
   }
 }
class Crossfade extends JComponent implements ActionListener {
  private final javax.swing.Timer animator;
  private final ImageIcon icon1;
  private final ImageIcon icon2;
  private int alpha = 10;
  private boolean direction = true;
  public Crossfade(ImageIcon icon1, ImageIcon icon2) {
    this.icon1 = icon1;
    this.icon2 = icon2;
    animator = new javax.swing.Timer(50, this);
  }
  public void animationStart() {
    direction = !direction;
    animator.start();
  }
  public void paintComponent(Graphics g) {
    Graphics2D g2d = (Graphics2D)g;
    g2d.setPaint(getBackground());
    g2d.fillRect(0, 0, getWidth(), getHeight());
    if(direction && alpha<10) {
      alpha = alpha + 1;
    }else if(!direction && alpha>0) {
      alpha = alpha - 1;
    }else{
      animator.stop();
    }
    g2d.setComposite(AlphaComposite.getInstance(
      AlphaComposite.SRC_OVER, 1.0f-alpha*0.1f));
    g2d.drawImage(icon1.getImage(), 0, 0,
      (int)icon1.getIconWidth(), (int)icon1.getIconHeight(), this);
    g2d.setComposite(AlphaComposite.getInstance(
      AlphaComposite.SRC_OVER, alpha*0.1f));
    g2d.drawImage(icon2.getImage(), 0, 0,
      (int)icon2.getIconWidth(), (int)icon2.getIconHeight(), this);
  }
  public void actionPerformed(ActionEvent e) {
    repaint();
  }
}
}}
-&jnlp;
-&jar;
-&zip;

**解説 [#lc4f6171]
上記のサンプルでは、二枚の画像の描画に使用するAlphaCompositeをそれぞれ変化させながら上書きすることで、Crossfadeによる画像の切り替えを行っています。

上書きの規則には、AlphaComposite.SRC_OVERを使っています。

//**参考リンク
**コメント [#t10dec59]
#comment