import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;

public class Flexible extends java.applet.Applet
{
	public void init()
	{
		FlexibleWindow fen = new FlexibleWindow("Experiments with FlexibleLayout");
		fen.show();
	}

	public static void main(String[] args)
	{
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

			//UIManager.LookAndFeelInfo[] liste = UIManager.getInstalledLookAndFeels();
			//for (int i = 0; i < liste.length; i++)
			//{
			//	System.out.println(liste[i].getName());
			//}
		} catch (Exception e) {
			System.out.println("Unexpected error");
			e.printStackTrace();
		}

		new Flexible().init();
	}

}

class FlexibleWindow extends JFrame implements FlexibleConstants
{
	JButton docButton = new JButton("What's up, Doc?");
	JLabel zorroLabel = new JLabel("Zorro", JLabel.CENTER);
	JSlider alcoholSlider = new JSlider();
	JRadioButton peterspanRadio = new JRadioButton("peter's pan");
	JButton reiButton = new JButton("Eva_Rei.jpg");

	static final int BORDER_SIZE = 1;
	Border yellowBorder = createBorder(BORDER_SIZE, Color.yellow);
	Border redBorder = createBorder(BORDER_SIZE, new Color(255, 150, 150));
	Border blackBorder = createBorder(BORDER_SIZE, new Color(150, 150, 150));

	public FlexibleWindow(String title)
	{
		super(title);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

		try
		{
			reiButton = new JButton(new ImageIcon("Eva_Rei.jpg"));
		}
		catch (Exception e) {
			reiButton = new JButton("Can't load Eva_Rei.jpg");
		}
		initForm();
	}

	public void dispose()
	{
		try
		{
			System.exit(0);
		}
		catch (SecurityException e) {
			System.out.println("Terminated.");
		}
	}

	protected void initForm()
	{
		Container content = getContentPane();
		content.setLayout(new FlexibleLayout(400, 300));

		addComp(docButton, new FlexibleConstraints(250, 100, 100, 40));
		addComp(zorroLabel, new FlexibleConstraints(100, 50, 50, 30, TOPRIGHT|SCALABLE));
		zorroLabel.setBorder(yellowBorder);
		addComp(reiButton, new FlexibleConstraints(250, 140, 100, 40, new Insets(10, 10, 0, 0)));
		addComp(alcoholSlider, new FlexibleConstraints(250, 180, 100, 40, new Insets(10, 0, 0, 10)));
		alcoholSlider.setBorder(yellowBorder);
		addComp(peterspanRadio, new FlexibleConstraints(150, 280, 100, 20, BOTTOMCENTER_RELATIVE));

		pack();
	}

	private void addComp(Component comp, FlexibleConstraints cons)
	{
		Container content = getContentPane();
		content.add(comp, cons);

		Panel ancre = new Panel(new FlexibleLayout(7, 7));
		if ((cons.getRelative() & RELATIVE) != 0)
		{
			ancre.setBackground(Color.red);
			content.add(ancre, new FlexibleConstraints(cons.getLocation(), CENTER_RELATIVE));
		}
		else
		{
			ancre.setBackground(Color.black);
			content.add(ancre, new FlexibleConstraints(cons.getLocation(), CENTER));
		}

		JPanel cadre = new JPanel();
		if ((cons.getRelative() & SCALABLE) != 0)
		{
			cadre.setBorder(redBorder);
		}
		else
		{
			cadre.setBorder(blackBorder);
		}
		content.add(cadre, new FlexibleConstraints(cons.getBounds(), new Insets(-1, -1, -1, -1), cons.getRelative()));
	}

	private static Border createBorder(int thickness, Color color)
	{
		return BorderFactory.createMatteBorder(thickness, thickness, thickness, thickness, color);
	}

}