import java.awt.*;
import javax.swing.*;

public class PruebaGrafica3{
    public static void main(String args[]){
        new PruebaGrafica3();
    }
    PruebaGrafica3(){
        JFrame ventana=new JFrame("Probando");
        ventana.setSize(500,600);
        ventana.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Pizarrin p=new Pizarrin();
        ventana.add(p);
        ventana.setVisible(true);
    }
}

class Pizarrin extends Canvas{
    Pelota p1;
    Pelota p2;
    Pizarrin(){
        p1=new Pelota(this);
        p1.x=10;p1.y=10;
        p1.start();
        p2=new Pelota(this);
        p2.x=400;p2.y=500;
        p2.start();
    }
    public void paint(Graphics g){
        g.fillOval(p1.x,p1.y,30,30);
        g.fillOval(p2.x,p2.y,50,35);
    }
}

class Pelota extends Thread{
    int x;
    int y;
    
    int sentidoX;//1=derecha, -1=izquierda
    int sentidoY;//1=abajo, -1=arriba
    Canvas c;
    Pelota(Canvas a){
        c=a;
        sentidoX=1;
        sentidoY=1;
    }
        
    public void run(){
        // Esta pelota bota y rebota constantemente
        while(true){
            x=x+(sentidoX)*10;
            y=y+(sentidoY)*15;
            if(x>=450 || x<=0) sentidoX=-sentidoX;
            if(y>=550 || y<=0) sentidoY=-sentidoY;
            
            try{Thread.sleep(100);}catch(Exception e){}
            c.repaint();
        }
    }
}
    