martes, 30 de junio de 2015

applets e hilos

import java.awt.Color;
import javax.swing.JApplet;
import java.awt.Graphics;

public class Prueba extends JApplet implements Runnable {
    //variables globales
    int lugarX, lugarY, incX;
    double ang;
    
    public void init() {
        incX = 10;
        lugarX=0;
        lugarY=0;
        ang=0;
    }
    
    public void start() {
            Thread animacion = new Thread(this);
            animacion.start();
    }
    
    public void paint(Graphics g) {
        g.setColor(Color.BLUE);
        g.drawOval(400+lugarX,300-lugarY,50,50);
    }
    
    public void run() {
        double x,y;
        while(true) {
            try {
                 Thread.sleep(50);
                    {
            double angr=Math.PI*ang/180.0;
            x=150*Math.cos(angr);
            y=150*Math.sin(angr);
            lugarX=(int)x;
            lugarY=(int)y;
            System.out.println(x+" "+y+" "+lugarX+" "+lugarY);
            ang+=5;              
        }
             
                repaint();
            } catch(Exception ex) {}
        }
    }}