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

/**
 * A panel showing the main part of the screen for Calaveras Crossing.
 *
 * @author Jim Glenn
 * @version 0.2 1/8/2009 changed for x-coordinates measured in frog widths
 * @version 0.1 10/7/2008
 */

public class CalaverasCrossingPanel extends JPanel
{
    /**
     * The game this panel displays.
     */

    private CalaverasCrossingGame game;

    /**
     * The painter for the game displayed in this panel.
     */

    private CalaverasCrossingPainter painter;

    /**
     * Creates a new panel displaying the given game.
     *
     * @param g a CalaverasCrossing game
     */

    public CalaverasCrossingPanel(CalaverasCrossingGame g)
    {
	game = g;
	painter = new CalaverasCrossingPainter(g);
    }

    /**
     * Paints this game.
     *
     * @param g the graphics context to paint in
     */

    public void paint(Graphics g)
    {
	// determine the area to paint in based on whether the aspect
	// ratio of this panel is greater than or less than the aspect
	// ratio of the game

	double aspect = (double)Lane.SCREEN_WIDTH / Level.NUM_LANES;

	int w, h;

	if ((double)getWidth() / getHeight() > aspect)
	    {
		h = getHeight();
		w = (int)(h * aspect);
	    }
	else
	    {
		w = getWidth();
		h = (int)(w / aspect);
	    }

	Graphics clipRegion = g.create((getWidth() - w) / 2,
				       (getHeight() - h) / 2,
				       w,
				       h);

	painter.paint(g, w, h);
    }
}

