Font size animation : Animation « 2D Graphics GUI « Java






Font size animation

     

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();
  }
}

   
    
    
    
    
  








Related examples in the same category

1.Is Event Dispatcher ThreadIs Event Dispatcher Thread
2.Timer based animation
3.A rotating and scaling rectangle.
4.Fade out an image: image gradually get more transparent until it is completely invisible.
5.Hypnosis animationHypnosis animation
6.Noise ImageNoise Image
7.How to create Animation: Paint and threadHow to create Animation: Paint and thread
8.How to create animationHow to create animation
9.Animation: bounce
10.Image BouncerImage Bouncer
11.Text animationText animation
12.Buffered Animation DemoBuffered Animation Demo
13.Bouncing CircleBouncing Circle
14.Hypnosis SpiralHypnosis Spiral
15.Animator DemoAnimator Demo
16.Towers of Hanoi
17.Make your own animation from a series of images
18.Composition technique in this animation.
19.Animated Button
20.Animated Message Panel
21.Animated PasswordField
22.Animated TextField
23.A simple spring simulation in one dimension
24.Shows an animated bouncing ball
25.Shows animated bouncing ballsShows animated bouncing balls