/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package enxame;

/**
 *
 * @author awolf
 */
import javax.swing.JLabel;
import javax.swing.JFrame;

public class Fujao extends Thread {

    private int posX,  posY;
    private JLabel fujao;
    private JFrame tela;
    private JLabel mosca;
    private int radX,  radY;

    public Fujao(JFrame tela, JLabel fujao, JLabel mosca) {
        this.fujao = fujao;
        this.tela = tela;
        this.mosca = mosca;
        posX = fujao.getX();
        posY = fujao.getY();
        radX = (int) (Math.random() * 50) + 5;
        radY = (int) (Math.random() * 50) + 5;
    }

    @Override
    public void run() {
        boolean vivo = true;
        while (vivo) {

            

            if (!(Math.sqrt(Math.pow(mosca.getX() - fujao.getX(), 2)) < radX &&
                    Math.sqrt(Math.pow(mosca.getY() - fujao.getY(), 2)) < radY)) {

                posX += (int) (Math.random() * 11) - 5;
                posY += (int) (Math.random() * 11) - 5;

                if (posX > tela.getWidth()) {
                    posX -= 5;
                } else {
                    if (posX < 1) {
                        posX += 5;
                    }
                }

                if (posY > tela.getHeight()) {
                    posY -= 5;
                } else {
                    if (posY < 1) {
                        posY += 5;
                    }
                }
            } else {
                if (mosca.getX() > fujao.getX()) {
                    posX -= 5;
                } else {
                    posX += 5;
                }
                if (mosca.getY() > fujao.getY()) {
                    posY -= 5;
                } else {
                    posY += 5;
                }
            }

            try {
                sleep(10);
            } catch (Exception e) {
            }

            fujao.setLocation(posX, posY);
        }
        
        fujao.setVisible(false);
        
    }    
}
