Title: Animation Techniques
Course: CPS 261: Advanced Java Concepts ~ DL & MM MASTER

Animation Techniques

Animation_Review reviews the techniques we have already used to create animated screens.

A lot of my examples in this section will use a new technique:

Timer - constructor:
Timer(int timeInterval, ActionListener listener)
timeInterval is in milliseconds

Other methods:

start - start the timer thread

stop - stop the timer thread

setInitialDelay(int initialDelay) - time in milliseconds

Animation_Review.java



import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.JFrame;

public class Animation_Review extends JFrame implements Runnable{
    int current_x=0;
    boolean keep_running=true;

    Animation_Review()
    {
        setSize(600, 200);
        setTitle("Animation_Review");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);

        Thread updateThread = new Thread(this);
        updateThread.start();
    }
    public void paint(Graphics g)
    {
        g.setColor(Color.white);
        Dimension d = getSize();
        g.fillRect(0, 0, d.width, d.height);

        g.setColor(Color.red);
        g.drawOval(current_x, 50, 60, 100);
        if (current_x > d.width)
            keep_running= false; // ask thread to quit
        System.out.println("paint");

    }

    public void run()
    {
        while (keep_running)
        {
            try {
                Thread.sleep(100);
                current_x += 10;
                repaint();
            } catch (InterruptedException e) {
                System.out.println("Must have been interrupted");
                break;
            }
        }
        System.out.println("Thread is done.");
    }
    public static void main(String[] args)
    {
        Animation_Review ar = new Animation_Review();
    }

}



Animation_Timer.java



import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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

public class Animation_Timer extends JFrame implements ActionListener{

    int current_x=0;
    Timer timer;

    Animation_Timer()
    {
        setSize(600, 200);
        setTitle("Animation_Review");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);

        timer = new Timer(100, this);
        timer.setInitialDelay(50);
        timer.start();
    }
    public void paint(Graphics g)
    {
        g.setColor(Color.white);
        Dimension d = getSize();
        g.fillRect(0, 0, d.width, d.height);

        g.setColor(Color.red);
        g.drawOval(current_x, 50, 60, 100);
        if (current_x > d.width)
            timer.stop(); // stop updates
        System.out.println("paint");

    }



    @Override
    public void actionPerformed(ActionEvent e) {
        current_x += 10;
        repaint();
    }

    public static void main(String[] args)
    {
        Animation_Timer ar = new Animation_Timer();
    }



}


Last Updated: June 17, 2015 2:04 PM