Panel07.java

package com.bozoid.cis370.hw07;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.prefs.Preferences;


/**
A content pane that 'owns' an Animator object and asks it to 
draw a frame periodically.
*/
public class Panel07 extends JPanel
{
    /**
    @param msDelay animation frame rate in milliseconds
    */
    public Panel07(int msDelay,ActionTest07 parent)
    {
        this.parent = parent;
        setBackground(Color.BLUE);
        // Set up a timer to trigger our repaint.
        ticker = new Timer(msDelay,new ActionListener() {
            public void actionPerformed(ActionEvent e)
                {  repaint();  }
            });
        ticker.start();
        parent.addWindowListener(new Panel07WindowListener());
        prefs = Preferences.userRoot().node("/com/bozoid/cis370/hw07");
    }

    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        if (animator != null)
            animator.oneFrame((Component)this,g);
    }

    public void setAnimator(Animator a)
    {
        animator = a;
    }

    public Animator getAnimator()
    {
        return animator;
    }

    /**
    To pause the animation when the window is not active, listen
    for window events.
    */
    private class Panel07WindowListener extends WindowAdapter
    {
        public void windowActivated(WindowEvent e)
            { ticker.start(); }
        public void windowDeactivated(WindowEvent e)
            { ticker.stop(); }
        public void windowDeiconified(WindowEvent e)
            { ticker.start(); }
        public void windowIconified(WindowEvent e)
            { ticker.stop(); }
        public void windowClosing(WindowEvent e)
        {
            ticker.stop();
            parent.saveState();
        }
    }

    private Preferences prefs;
    private ActionTest07 parent;
    private Animator animator = null;
    private Timer ticker;
}