/*
 * Created on Feb 8, 2005
 *
 */
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.font.TextLayout;
import java.awt.image.ImageObserver;
import java.text.DecimalFormat;
import java.net.URL;

import javax.swing.JPanel;

/**
 * @author knorton
 */
public class BallPanel extends JPanel implements Runnable {
    private Dimension			size = null;
    private float			time = 0f;
    private Ball				ball = null;
    private Thread			simThread = null;
    
    /**
     * @param width
     * @param height
     */
    public BallPanel(int width,int height) {
        this.setPreferredSize(new Dimension(width,height));
    }
     
    public Ball getBall() {
        return this.ball;
    }
    
    public void resetBall() {
        ball.reset();
        float r = ball.getRadius();
        ball.setX(r+10f);
        ball.setY(r+10f);
   }
    
    public synchronized void start() {
        time = 0f;
        simThread = new Thread(this);
        simThread.start();
    }
    
    public synchronized void stop() {
        if (simThread != null)
            this.simThread.interrupt();
    }
    
    /**
     * 
     */
    public void init() {
        //Begin loading image
        Image st = java.awt.Toolkit.getDefaultToolkit().getImage(
            getClass().getResource(Constants.STATIC_IMAGE));
        Image dy = java.awt.Toolkit.getDefaultToolkit().getImage(
            getClass().getResource(Constants.DYNAMIC_IMAGE));
        
        prepareImage(st,this);
        prepareImage(dy,this);
        
        //Finish loading image if necessary
        while ((st==null) || (dy==null) 
                || ((checkImage(st,this)&ImageObserver.ALLBITS)==0)
                || ((checkImage(dy,this)&ImageObserver.ALLBITS)==0)) {
            
            if (((checkImage(st,this) & ImageObserver.ERROR)!=0)
                    || ((checkImage(dy,this) & ImageObserver.ERROR)!=0))
                return;
            try { Thread.sleep(100); }
            catch (InterruptedException e) {}
        }
        
        this.ball = new Ball();
        this.ball.setDynamicImage(dy);
        this.ball.setStaticImage(st);
        this.resetBall();
        this.repaint();
    }
    
    /* (non-Javadoc)
     * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
     */
    public void paintComponent(Graphics g) {
        if (ball == null)
            return;
        
        Graphics2D	g2 = (Graphics2D)g;
        
        g2.setRenderingHint(
                RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        
        g2.setBackground(Color.WHITE);
        g2.clearRect(0,0,this.getWidth(),this.getHeight());
        
        ball.draw(g2);
        
        String ts = "time: "+new DecimalFormat("0.00").format(time);
        g2.setColor(Color.GRAY);
        g2.drawString(ts,
                2,
                2+new TextLayout(
                        ts,
                        this.getFont(),g2.getFontRenderContext()).getBounds().getBounds().height);
    }
    
    /* (non-Javadoc)
     * @see java.lang.Runnable#run()
     */
    public void run() {
        try {
            for (;;) {
    		    		time += Constants.DELTA_T;
    		    		this.ball.update(
    					Constants.DELTA_T,
					this.getWidth(),
					this.getHeight());
                
                this.repaint();
                Thread.sleep(75);
            }
        }
        catch (InterruptedException e) {
            return;
        }
    }
}
