Panel05.java
package com.bozoid.cis370.hw05;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.*; // for Ellipse2D
import java.awt.font.*;
import java.util.Random;
/**
A content pane that 'owns' an Animator object and asks it to
draw a frame periodically.
*/
public class Panel05 extends JPanel
{
/**
@param msDelay animation frame rate in milliseconds
*/
public Panel05(int msDelay,JFrame 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 Panel05WindowListener());
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
if (animator != null)
animator.oneFrame((Component)this,g);
}
public void setAnimator(Animator a)
{
animator = a;
}
/**
To pause the animation when the window is not active, listen
for window events.
*/
private class Panel05WindowListener 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(); }
}
private Animator animator = null;
private Timer ticker;
}