Font size animation : Swing Timer « Swing « Java Tutorial






import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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

public class FontSizeAnimation extends JPanel implements ActionListener {
  Timer timer;
  int x = 1;
  float alpha = 1;

  public FontSizeAnimation() {
    timer = new Timer(8, this);
    timer.setInitialDelay(190);
    timer.start();
  }

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

    Font font = new Font("Dialog", Font.PLAIN, x);
    g2d.setFont(font);

    FontMetrics fm = g2d.getFontMetrics();
    String s = "Java";
    
    int w = (int) getSize().getWidth();
    int h = (int) getSize().getHeight();

    int stringWidth = fm.stringWidth(s);

    g2d.drawString(s, (w - stringWidth) / 2, h / 2);
  }

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

  public void actionPerformed(ActionEvent e) {
    x += 1;
    alpha -= 0.0001;
    repaint();
  }
}








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