/*
 * Asteroide.java
 *
 * Created on 27 de Janeiro de 2006, 15:45
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */

package exemplodethreads;

/**
 *
 * @author Alexandre
 */
import java.awt.*;
public class Asteroide extends Thread  {
    
    public int AsterX = 0, AsterY = 0, AsterTam = 40;
    protected int X = 1, Y = 1;
    
    /** Creates a new instance of Asteroide */
    public Asteroide(int MaxX, int MaxY) {
        AsterX = (int)(Math.random() * MaxX);
        AsterY = (int)(Math.random() * MaxY);
        AsterTam = (int)(Math.random() * 20) + 5;
    }
    
    public void Pausa( int MiliSeg ) {
        long TempoInicial = System.currentTimeMillis();
        long TempoFinal = System.currentTimeMillis() + MiliSeg;
        
        while( TempoInicial < TempoFinal ){
            TempoInicial = System.currentTimeMillis();
        }
    }
    
    public void MostraAsteroide( Graphics g ) {
        g.drawRect(AsterX, AsterY, AsterTam, AsterTam );
    }
    
    public void run(){
        
        while (true) {
            
            AsterX = AsterX + X; AsterY = AsterY + Y;
            
            try {
                this.sleep(100);
            } catch (InterruptedException e) {}
                        
            if( AsterX > 200 - AsterTam ) X = -1;
            else if( AsterX < 1 ) X = 1;
            if( AsterY > 200 - AsterTam ) Y = -1;
            else if( AsterY < 1 ) Y = 1;
        }
    }
}
