// 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.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class LMFun extends JFrame implements ChangeListener, ActionListener {
    private static final int MIN_W = 100;
    private static final int MIN_H = 100;
    private static final int DEF_W = 200;
    private static final int DEF_H = 200;
    private static final int MAX_W = 500;
    private static final int MAX_H = 500;
    public LMFun() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("Arnes LM fun");
        setLayout(new FlowLayout());
        for(int i = 1; i <= 12; i++) {
            getContentPane().add(createPanel(i));
        }
        pack();
    }
    private static final Color[] cols = { Color.GRAY, Color.WHITE, Color.RED, Color.GREEN, Color.BLUE,
                                          Color.YELLOW, Color.CYAN, Color.MAGENTA, Color.ORANGE, Color.PINK };
    private JPanel createPanel(int no) {
        JPanel res = new JPanel();
        if(no % 2 == 0) {
            res.setLayout(new GridLayout(2, 2));
            res.add(createButton(cols[no % cols.length]));
            res.add(createButton(cols[no % cols.length]));
            res.add(createButton(cols[no % cols.length]));
            res.add(createButton(cols[no % cols.length]));
        } else {
            res.setLayout(new BorderLayout());
            res.add(createButton(cols[no % cols.length]), BorderLayout.CENTER);
            res.add(createButton(cols[no % cols.length]), BorderLayout.NORTH);
            res.add(createButton(cols[no % cols.length]), BorderLayout.SOUTH);
            res.add(createButton(cols[no % cols.length]), BorderLayout.WEST);
            res.add(createButton(cols[no % cols.length]), BorderLayout.EAST);
        }
        JSlider verjs = new JSlider(JSlider.VERTICAL, MIN_H, MAX_H, DEF_H);
        verjs.addChangeListener(this);
        JSlider horjs = new JSlider(JSlider.HORIZONTAL, MIN_W, MAX_W, DEF_W);
        horjs.addChangeListener(this);
        JPanel wrapper = new JPanel();
        wrapper.setLayout(new BorderLayout());
        wrapper.add(res, BorderLayout.CENTER);
        wrapper.add(verjs, BorderLayout.EAST);
        wrapper.add(horjs, BorderLayout.SOUTH);
        wrapper.setBorder(BorderFactory.createTitledBorder("Panel #" + no));
        wrapper.setPreferredSize(new Dimension(DEF_W, DEF_H));
        return wrapper;
    }
    private JButton createButton(Color c) {
        JButton res = new JButton();
        res.setBackground(c);
        res.setText("Bla");
        res.addActionListener(this);
        return res;
    }
    @Override
    public void stateChanged(ChangeEvent e) {
        JSlider js = (JSlider)e.getSource();
        JPanel jp = (JPanel)js.getParent();
        if(!js.getValueIsAdjusting()) {
            if(js.getOrientation() == JSlider.VERTICAL) {
                jp.setPreferredSize(new Dimension(jp.getWidth(), js.getValue()));
            } else if (js.getOrientation() == JSlider.HORIZONTAL) {
                jp.setPreferredSize(new Dimension(js.getValue(), jp.getHeight()));
            }
            jp.revalidate();
        }
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        JOptionPane.showMessageDialog(this, "Du må ikke klikke her");
    }
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame f = new LMFun();
                f.setVisible(true);
            }
        });
    }
}
