package capturatela;

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;

public class Animador extends JFrame implements Runnable {

    private int x1 = 10, y1 = 2, x2 = 440, y2 = 800;
    private int i = 0;
    private final int X1 = 2, X2 = 2, Y1 = 2, Y2 = 1;
    private int stepX1 = X1, stepX2 = X2, stepY1 = Y1, stepY2 = Y2;

    public Animador() {
        setSize(900, 600);
        setTitle("Animador");
        new Thread(this).start();
    }

    public void paint(Graphics g) {

        x1 = x1 + stepX1;
        x2 = x2 + stepX2;
        y1 = y1 + stepY1;
        y2 = y2 + stepY2;

        if (x1 < 1) {
            stepX1 = X1;
        }
        if (x1 > getWidth()) {
            stepX1 = -X1;
        }

        if (x2 < 1) {
            stepX2 = X2;
        }
        if (x2 > getWidth()) {
            stepX2 = -X2;
        }

        if (y1 < 1) {
            stepY1 = Y1;
        }
        if (y1 > getHeight()) {
            stepY1 = -Y1;
        }

        if (y2 < 1) {
            stepY2 = Y2;
        }
        if (y2 > getHeight()) {
            stepY2 = -Y2;
        }

        //int cor = (int) (Math.random() * 255 * 255 * 255 * 255);
        g.setColor(new Color(255 * i));
        i++;
        if(i>255){
            i=0;
        }

        g.drawLine(x1, y1, x2, y2);
        //g.drawOval(x1, y1, x2, y2);
        //g.fillRect(x1, y1, x2,y2);

    }

    @Override
    public void run() {
        try {
            while (true) {
                Thread.sleep(1);
                repaint();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        Animador ani = new Animador();
        ani.setLocationRelativeTo(null);
        ani.setVisible(true);
    }
}
