// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class CrossPanel extends JFrame {
    public CrossPanel() {
        setTitle("Cross panel");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        getContentPane().setLayout(new BorderLayout());
        ColorPanel cp1 = new ColorPanel(1);
        ColorPanel cp2 = new ColorPanel(2);
        ColorPanel cp3 = new ColorPanel(3);
        ColorPanel cp4 = new ColorPanel(4);
        JPanel allfour = new JPanel();
        allfour.setLayout(new GridLayout(2,2));
        allfour.add(cp1);
        allfour.add(cp2);
        allfour.add(cp3);
        allfour.add(cp4);
        getContentPane().add(allfour, BorderLayout.CENTER);
        ChangePanel cp = new ChangePanel(cp1, cp2, cp3, cp4);
        getContentPane().add(cp, BorderLayout.SOUTH);
        pack();
    }
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame f = new CrossPanel();
                f.setVisible(true);
            }
        });
    }
}

class ColorPanel extends JPanel {
    private static final Color[] cols = { Color.RED, Color.GREEN, Color.BLUE, Color.WHITE, Color.YELLOW, Color.BLACK };
    private int colix;
    public ColorPanel(int colix) {
        this.colix = colix;
        setPreferredSize(new Dimension(100, 100));
        changeColor();
    }
    public void changeColor() {
        setBackground(cols[colix]);
        colix = (colix + 1) % cols.length;
    }
}

class ChangePanel extends JPanel {
    private ColorPanel cp1; 
    private ColorPanel cp2; 
    private ColorPanel cp3; 
    private ColorPanel cp4;
    public ChangePanel(ColorPanel cp1, ColorPanel cp2, ColorPanel cp3, ColorPanel cp4) {
        this.cp1 = cp1;
        this.cp2 = cp2;
        this.cp3 = cp3;
        this.cp4 = cp4;
        setLayout(new BorderLayout());
        JButton btn = new JButton("Change colors");
        btn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                ChangePanel.this.cp1.changeColor();
                ChangePanel.this.cp2.changeColor();
                ChangePanel.this.cp3.changeColor();
                ChangePanel.this.cp4.changeColor();
            }
        });
        add(btn, BorderLayout.CENTER);
    } 
}
