/*
 *  Swing versiyonu
 */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/*
 * Arthur van Hoff'un animasyon örneği.
 * Bu örnek tüm animasyonlar için örnek olarak kullanılabilir.
*/
public class AnimatorApplicationSW extends JFrame
                                 implements Runnable {
    AnimappPanel animappPanel;
    int frameNumber = -1;
    int delay;
    Thread animatorThread;
    boolean frozen = false;

    AnimatorApplicationSW(int fps, String windowTitle) {
        super(windowTitle);
        delay = (fps > 0) ? (1000 / fps) : 100;

        animappPanel = new AnimappPanel();
        getContentPane().add(animappPanel);

        addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                if (frozen) {
                    frozen = false;
                    startAnimation();
                } else {
                    frozen = true;
                    stopAnimation();
                }
            }
        });

        addWindowListener(new WindowAdapter() {
            public void windowIconified(WindowEvent e) {
                stopAnimation();
            }
            public void windowDeiconified(WindowEvent e) {
                startAnimation();
            }
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

    public void startAnimation() {
        if (frozen) {       }
        else {
            //animasyonu başlat
            if (animatorThread == null) {
                animatorThread = new Thread(this);
            }
            animatorThread.start();
        }
    }
    public void stopAnimation() {
        //animasyon treadini durdur.
        animatorThread = null;
    }

    public void run() {
        //önceliği düşür.
        Thread.currentThread().setPriority(Thread.MIN_PRIORITY);

        //başlama zamanı
        long startTime = System.currentTimeMillis();

        //thread tanımı
        Thread currentThread = Thread.currentThread();

        //animasyon döngüsü
        while (currentThread == animatorThread) {
            //animasyon çerçevesini bir artır
            frameNumber++;

            //göster
            repaint();

            //geciktir
            try {
                startTime += delay;
                Thread.sleep(Math.max(0,
                             startTime-System.currentTimeMillis()));
            } catch (InterruptedException e) {
                break;
            }
        }
    }

class AnimappPanel extends JPanel {

    public AnimappPanel() {}

    //animasyon çerçevesini çiz
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawString("Frame " + frameNumber, 5, 50);
    }
}

    public static void main(String args[]) {
        AnimatorApplicationSW animator = null;
        int fps = 10;

        // Get frames per second from the command line argument
        if (args.length > 0) {
            try {
                fps = Integer.parseInt(args[0]);
            } catch (Exception e) {}
        }
        animator = new AnimatorApplicationSW(fps,"Animasyon swing versiyonu");
        animator.setSize(200, 60);
        animator.setVisible(true);
        animator.startAnimation();
    }
}
