package poisson;



import java.awt.*;
import javax.swing.*;
import java.awt.geom.*;
//import edu.csusb.danby.applet.DApplet;

/**
* OceanPanel provides display for poisson distribution applet
* @author Charles Stanton
* @version	Sun Jul 28 09:25:23 PDT 2002
*/
class OceanPanel extends JPanel{
    int nsharks=3; 
    int shark_x;
    int shark_y;
    JApplet applet;  //calling applet
    Image sharkgif, sharkgif1;
    SharkModel model;
    private int SMALL=0;
    private int LARGE=1;
    
    /**
    * constructor for OceanPanel
    * @param applet the calling SGApplet
    * @param aModel the model keeping the data
    */
    public OceanPanel( JApplet applet, SharkModel aModel){
         java.net.URL url = this.getClass ().getResource( "shk02c.gif" );
         java.net.URL urlone = this.getClass ().getResource( "shk32a.gif" );
        try{            
            sharkgif = createImage( (java.awt.image. ImageProducer) url.getContent () );
            sharkgif1 = createImage( (java.awt.image. ImageProducer) urlone.getContent () );
        }
        catch (java.io.IOException ioe){
            System.err.println("couldn't find shark.gif");
        }
        MediaTracker tracker=new MediaTracker(this);
        tracker.addImage(sharkgif,SMALL);
        tracker.addImage(sharkgif1,LARGE);
        try{tracker.waitForAll();} catch (InterruptedException e){;}
             if ( tracker.isErrorAny() ) {
                applet.showStatus("error loading images");
            }   
        model = aModel;
        setBackground(Color.blue);
        }
        
    /**
    * paint method for panel
    */
    public void paint(Graphics g){
        Graphics2D g2 = (Graphics2D)g;
        //super.paint(g);
        Dimension r=getSize();
        Rectangle2D rect = new Rectangle2D.Float(0,0,r.width,r.height);
        GradientPaint gp = new GradientPaint(0,0, Color.blue, 50,50, new Color(128, 200,250), true);
        g2.setPaint(gp);
        g2.fill(rect);
        nsharks = model.getNSharks();
        for (int i=0; i< nsharks; i++){
            shark_x= (int)(Math.random()*(r.width-40));
            shark_y= (int)(Math.random()*(r.height-40));	
            if (nsharks==1) {
                g2.drawImage(sharkgif1, shark_x+20, shark_y+20, this);
                }
            else {
                g2.drawImage(sharkgif,shark_x+20, shark_y+20, this);
                }
            }
        }

    /**
    * update panel to current statistics
    * @param model the model holding the data
    */
    public void updatePanel(SharkModel model) {
        nsharks=model.getNSharks();
        repaint();
        }

    public Dimension getPreferredSize(){
        Dimension d = new Dimension(350, 250);
        return d;
        }

    public void clearOcean(){
        this.nsharks=0;
        repaint();
        }
}
