import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

/**
 * Program to continuously draw to the screen. Used to test what happens
 * when an RDP connection is made and the window dimension is resized.
 */
public class BusyWindow extends JFrame implements Runnable {

    Random rand;
    Graphics2D g2d;

    public BusyWindow() {
        setTitle("Keep drawing");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        getContentPane().setLayout(new BorderLayout());

        JPanel canvas = new JPanel();
        canvas.setToolTipText("A very busy canvas");
        getContentPane().add(canvas, BorderLayout.CENTER);

        setSize(new Dimension(500, 500));
        setVisible(true);

        rand = new Random();
        g2d = (Graphics2D) canvas.getGraphics();

        SwingUtilities.invokeLater(this);
    }

    @Override
    public void run() {
        int width = getWidth();
        int height = getHeight();
        g2d.setPaint(Color.WHITE);
        g2d.fillRect(0,0, width, height);
        for (int i=0; i<20; i++) {
            int x = rand.nextInt(width);
            int y = rand.nextInt(height);
            int w = rand.nextInt(100);
            int h = rand.nextInt(100);
            Color c = new Color(rand.nextInt(16777216));
            int op = rand.nextInt(4);
            g2d.setPaint(c);
            if (op == 0)
                g2d.fillOval(x, y, w, h);
            else if (op == 1)
                g2d.fillRect(x, y, w, h);
            else if (op == 2)
                g2d.fillRoundRect(x, y, w, h, 10, 10);
            else
                g2d.drawString("Busy", x, y);
        }
        SwingUtilities.invokeLater(this);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(BusyWindow::new);
    }
}