TITLE:Borderのアニメーション

Posted by aterai at 2006-05-29

Borderのアニメーション

Borderの描画をアニメーションさせます。

  • &jnlp;
  • &jar;
  • &zip;
RippleBorder.png

サンプルコード

class RippleBorder extends EmptyBorder {
  private final javax.swing.Timer animator;
  private final JComponent comp;
  public RippleBorder(JComponent c, int width) {
    super(width, width, width, width);
    this.comp = c;
    animator = new javax.swing.Timer(80, new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        comp.repaint();
        count+=0.9f;
      }
    });
    comp.addMouseListener(new MouseAdapter() {
      public void mouseEntered(MouseEvent e) {
        comp.setForeground(Color.RED);
        animator.start();
      }
      public void mouseExited(MouseEvent e) {
        comp.setForeground(Color.BLACK);
      }
    });
  }
  private float count = 1.0f;
  public void paintBorder(Component comp, Graphics g, int x, int y, int w, int h) {
    if(!animator.isRunning()) {
      super.paintBorder(comp, g, x, y, w, h);
      return;
    }
    Graphics2D g2 = (Graphics2D) g;
    g2.setPaint(Color.WHITE);
    float a = 1.0f/count;
    if( 0.12f-a>1.0e-2 ) a = 0.0f;
    g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,a));
    Insets i = getBorderInsets();
    int xx = i.left-(int)count;
    int yy = i.top-(int)count;
    int ww = i.left+i.right-(int)(count*2.0f);
    int hh = i.top+i.bottom-(int)(count*2.0f);
    g2.setStroke(new BasicStroke(count*1.2f));
    g2.drawRoundRect(xx, yy, w-ww, h-hh, 10, 10);
    if(xx<0 && animator.isRunning()) {
      count = 1.0f;
      animator.stop();
    }
  }
}

解説

コンポーネント上にカーソルがきた場合、Borderをアニメーションさせることで、波紋状の効果を描画しています。

コメント