import java.awt.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Random;

import javax.swing.*;

public class Couleurs extends JPanel {
    int max;
    int index1 = -1;
    int index2 = -1;
    String[] nameList;
    Color[] colorList;

    public static void main(String[] args) {
        JFrame f = new JFrame("OwO");
        f.setSize(300, 300);
        f.setLocation(100, 100);
        f.setResizable(false);
        f.add(new Couleurs());
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
    }

    public Couleurs() {
        super();
        
        try (BufferedReader br = new BufferedReader(new FileReader("rgb.txt"))) {
            br.mark(2000000000);
            this.max = (int)br.lines().count();
            br.reset();
            this.nameList = new String[max];
            this.colorList = new Color[max];
            System.out.println(br.readLine());

            for (int i = 0; i < max-1; i++) {
                String[] infos = br.readLine().trim().split("[ \t]+");
                nameList[i] = infos[3];
                colorList[i] = new Color(Integer.parseInt(infos[0]), Integer.parseInt(infos[1]), Integer.parseInt(infos[2]));
            }

            Random r = new Random();
            this.index1 = r.nextInt(max);
            this.index2 = r.nextInt(max);

            br.close();
        } catch (IOException e) {

        }
    }

    @Override
    protected void paintComponent(Graphics g) {
        Graphics newG = g.create();

        if (this.isOpaque()) {
            newG.setColor(this.getBackground());
            newG.fillRect(0, 0, this.getWidth(), this.getHeight());
        }

        if (index1 != -1 && index2 != -1) {
            newG.setColor(colorList[index1]);
            newG.fillPolygon(new int[] {0, 0, this.getWidth()}, new int[] {0, this.getHeight(), this.getHeight()}, 3);

            newG.setColor(colorList[index2]);
            newG.fillPolygon(new int[] {0, this.getWidth(), this.getWidth()}, new int[] {0, 0, this.getHeight()}, 3);

            newG.setColor(colorList[index1]);
            newG.drawString(nameList[index2], this.getWidth() - 150, 20);

            newG.setColor(colorList[index2]);
            newG.drawString(nameList[index1], 20, this.getHeight() - 20);
        }
    }
}
