TITLE:JListのセルのアニメーション

JListのセルのアニメーション

Posted by terai at 2006-11-27
  • category: swing folder: ListCellAnimation title: JListのセルのアニメーション tags: [JList, ListCellRenderer, Animation] author: aterai pubdate: 2006-11-27T14:03:02+09:00 description: JListの選択されたセルをアニメーションさせます。 image: https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTPa7B8VkI/AAAAAAAAAd8/uLpJ50Oxwf8/s800/ListCellAnimation.png

概要

JListの選択されたセルをアニメーションさせます。

概要

JListの選択されたセルをアニメーションさせます。
  • &jnlp;
  • &jar;
  • &zip;

#screenshot

サンプルコード

#spanend
#spandel
class AnimeListCellRenderer extends JPanel implements ListCellRenderer, ActionListener {
#spanend
  private static final Color eColor = new Color(240,240,255);
  private static final Color sColor = new Color(230,230,255);
#spanadd
* サンプルコード [#sourcecode]
#spanend
#spanadd
#code(link){{
#spanend
#spanadd
class AnimeListCellRenderer extends JPanel implements ListCellRenderer {
#spanend
  private static final Color selectedColor = new Color(230, 230, 255);
  private final AnimeIcon icon = new AnimeIcon();
  private final ScrollingLabel label = new ScrollingLabel();
  private final MarqueeLabel label = new MarqueeLabel();
  private final javax.swing.Timer animator;
  private final JList jlist;
  private final JList list;
  private boolean isRunning = false;
  public AnimeListCellRenderer(JList list) {
  int animate_index = -1;
#spanadd

#spanend
  public AnimeListCellRenderer(final JList l) {
    super(new BorderLayout());
    jlist = list;
    animator = new javax.swing.Timer(100, this);
    animator.start();
    this.list = l;
    animator = new Timer(80, new ActionListener() {
      @Override public void actionPerformed(ActionEvent e) {
        int i = l.getSelectedIndex();
        if (isRunning = (i >= 0)) {
          l.repaint(l.getCellBounds(i, i));
        }
      }
    });
    setOpaque(true);
    add(icon,  BorderLayout.WEST);
    add(label, BorderLayout.CENTER);
    add(label);
    animator.start();
  }
  public Component getListCellRendererComponent(JList list, Object object,
          int index, boolean isSelected, boolean cellHasFocus) {
    isRunning = isSelected;
    setBackground(isSelected?sColor:index%2==0?eColor:list.getBackground());
    label.setText((object==null) ? "" : object.toString());
#spanadd

#spanend
  @Override public Component getListCellRendererComponent(JList list, Object object,
      int index, boolean isSelected, boolean cellHasFocus) {
    setBackground(isSelected ? selectedColor : list.getBackground());
    label.setText((object == null) ? "" : object.toString());
    animate_index = index;
    return this;
  }
  public void actionPerformed(ActionEvent e) {
    jlist.repaint();
#spanadd

#spanend
  private boolean isAnimatingCell() {
    return isRunning && animate_index == list.getSelectedIndex();
  }
  private class ScrollingLabel extends JLabel {
    public void ScrollingLabel() {
      setOpaque(false);
    }
    public void paintComponent(Graphics g) {
      Graphics2D g2d = (Graphics2D) g;
#spandel
.....
#spanend
#spanadd

#spanend
  private class MarqueeLabel extends JLabel {
    // ...
  }
  // ...
#spanadd
}
#spanend

解説

上記のサンプルでは、セルが選択されると左のアイコンがアニメーションし、文字列がクリップされている場合は、スクロールするようになっています。

解説

上記のサンプルでは、セルが選択されると左のアイコンがアニメーションし、文字列が省略されている場合はスクロールするよう設定しています。 選択されたセルだけ再描画しているのではなく、ActionListener を実装したセルレンダラーを作成してJList全体をrepaintしています。 選択されたセルだけ再描画しているのではなく、ActionListenerを実装したセルレンダラーを作成してJList全体をrepaintしています。 選択されたセルだけ再描画してアニメーションを行っています。

参考リンク

参考リンク

コメント

コメント