package org.Models;

import java.awt.*;

public class GridLayoutCarre implements LayoutManager {
    private int lignes;
    private int colonnes;
    public GridLayoutCarre(int lignes, int colonnes) {
        this.lignes = lignes;
        this.colonnes = colonnes;
    }

    @Override
    public void addLayoutComponent(String name, Component comp) {
    }

    @Override
    public void removeLayoutComponent(Component comp) {

    }

    @Override
    public Dimension preferredLayoutSize(Container parent) {
        return parent.getPreferredSize();
    }

    @Override
    public Dimension minimumLayoutSize(Container parent) {
        return parent.getMinimumSize();
    }

    @Override
    public void layoutContainer(Container parent) {
        int largeur = parent.getWidth();
        int hauteur = parent.getHeight();
        int tailleCase= Math.min(largeur / colonnes, hauteur / lignes);
        int offSetX = (largeur - tailleCase * colonnes) / 2;
        int offSetY = (hauteur - tailleCase * lignes) / 2;
        int index = 0;

        for (int i = 0; i < lignes; i++) {
            for (int j = 0; j < colonnes; j++) {
                if (index >= parent.getComponentCount()) {
                    return;
                }
                Component comp = parent.getComponent(index++);
                int x = offSetX + j * tailleCase;
                int y = offSetY + i * tailleCase;
                comp.setBounds(x, y, tailleCase, tailleCase);
            }
        }

    }
}
