TITLE:Timerの使用数を変更

Posted by at 2014-04-14

Timerの使用数を変更

パネルのタイルアニメーションで使用するjava.swing.Timerの数を変更して動作のテストを行います。

TimerAction.png

サンプルコード

//Timer: 1, ActionListener: 100
class Tile2 extends JComponent {
  private int red;
  public Tile2(final Random rnd, Timer timer) {
    super();
    timer.addActionListener(new ActionListener() {
      @Override public void actionPerformed(ActionEvent e) {
        red = rnd.nextInt(255);
        repaint();
      }
    });
  }
  @Override public Dimension getPreferredSize() {
    return new Dimension(10, 10);
  }
  @Override protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.setColor(new Color(red, 255 - red, 0));
    g.fillRect(0, 0, getWidth(), getHeight());
  }
}
View in GitHub: Java, Kotlin

解説

  • Timer: 100
    • 10x10個のアニメーション用タイルに一つづつTimerを使用
    • JDK 1.7.0_40以降で低速
  • Timer: 1, ActionListener: 100
    • Timer1個、Timer#addActionListener(...)で、100個のActionListenerを追加して使用
  • Timer: 1, ActionListener: 1
    • 1個のTimerを使用し、forループで10x10のラベルの色を変更してアニメーションを実行

参考リンク

コメント