Timer based animation : Swing Timer « Swing « Java Tutorial






import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

public class TimerBasedAnimation extends JPanel implements ActionListener {
  private Ellipse2D.Float ellipse = new Ellipse2D.Float();

  private double esize;

  private double maxSize = 0;

  private boolean initialize = true;

  Timer timer;

  ActionListener updateProBar;

  public TimerBasedAnimation() {
    setXY(20 * Math.random(), 200, 200);

    timer = new Timer(20, this);
    timer.setInitialDelay(190);
    timer.start();
  }

  public void setXY(double size, int w, int h) {
    esize = size;
    ellipse.setFrame(10, 10, size, size);
  }

  public void reset(int w, int h) {
    maxSize = w / 10;
    setXY(maxSize * Math.random(), w, h);
  }

  public void step(int w, int h) {
    esize++;
    if (esize > maxSize) {
      setXY(1, w, h);
    } else {
      ellipse.setFrame(ellipse.getX(), ellipse.getY(), esize, esize);
    }
  }

  public void render(int w, int h, Graphics2D g2) {
    g2.setColor(Color.BLUE);
    g2.draw(ellipse);
  }

  public void paint(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;

    RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING,
        RenderingHints.VALUE_ANTIALIAS_ON);

    rh.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);

    g2.setRenderingHints(rh);
    Dimension size = getSize();

    if (initialize) {
      reset(size.width, size.height);
      initialize = false;
    }
    this.step(size.width, size.height);
    render(size.width, size.height, g2);
  }

  public void actionPerformed(ActionEvent e) {
    repaint();
  }

  public static void main(String[] args) {
    JFrame frame = new JFrame("TimerBasedAnimation");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(new TimerBasedAnimation());
    frame.setSize(350, 250);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }
}








14.114.Swing Timer
14.114.1.Swing Timers
14.114.2.Timer Properties: coalesce
14.114.3.Timer Class
14.114.4.To turn on log messages
14.114.5.Timer based animation
14.114.6.A rotating and scaling rectangle.
14.114.7.Font size animation
14.114.8.Fade out an image: image gradually get more transparent until it is completely invisible.
14.114.9.Swing Timer actionSwing Timer action