import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

// This application shows a flying bird and allows the user to 
// control its flying.
public class FlyingBirdApp extends JFrame implements ActionListener {

	private static int	WIDTH = 600;
	private static int	HEIGHT = 300;

	private	FlyingBird	aBird;
	private	Timer		aTimer;
	private Image		background;

    public FlyingBirdApp (String title, FlyingBird theBird) {
		super(title);

		// Store the bird
		aBird = theBird;

		// Make a white background
		background = Toolkit.getDefaultToolkit().getImage("beach.jpg");

		// Add a MousePressed event handler
		addMouseListener(new MouseAdapter() {
 			public void mousePressed(MouseEvent e) {
				aBird.flapWings();
		}});

		// Start the timer so that the bird comes to life
		aTimer = new Timer(100, this);
		aTimer.start();

		// Set the size of the window and then show it
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setSize(WIDTH, HEIGHT);
	}
	 
	 // This is the timer event handler
	public void actionPerformed(ActionEvent e) {
		aBird.advance();
		if (aBird.getCurrentLocation().x > WIDTH)
			aBird.getCurrentLocation().x = -1 * FlyingBird.WIDTH;
		repaint();
	}

	public void paint(Graphics g) {
		g.drawImage(background, -200, -150, null);
		g.drawImage(aBird.appearance(), aBird.getCurrentLocation().x, 
					aBird.getCurrentLocation().y, this);
	}

	// Create main method to execute the application
	public static void main(String args[]) {
		new FlyingBirdApp("Flying Bird Application", new FlyingBird()).setVisible(true);
	}   
}