// AllPermuations.java

// Written by Julian Devlin, 8/97, for the text book
// "Introduction to Probability," by Charles M. Grinstead & J. Laurie Snell


import java.applet.Applet;
import java.awt.*;

public class AllPermutations
	extends java.applet.Applet
{
	TextArea disp;		// Area to display HT
	
	Panel dispArea;
	Panel controls;		// Panel for user controls
	
	Label numl;			// Controls
	TextField num;
	Button go;
	
	GridBagLayout gbl;			// Use GBL, because the other layouts don't work
	GridBagConstraints cc;
	
	Combinatorics myC;
	String[] input;
		
	// Initialize applet
	public void init()
	{	
		numl = new Label("No. of elements =");			// Create controls
		num = new TextField("3", 4);
		go = new Button("Go");
		
		disp = new TextArea(90, 20);		// Create display area
		
		dispArea = new Panel();				// Set up window
		controls = new Panel();
		setLayout(new BorderLayout(5, 5));
		
		add("South", controls);
		add("Center", dispArea);
		
		dispArea.setLayout(new GridLayout(1, 1));
		dispArea.add(disp);
		
		gbl = new GridBagLayout();			// Set up gbl
		controls.setLayout(gbl);
		
		cc = new GridBagConstraints();
		
		cc.gridx = 0;						// layout by hand
		cc.gridy = 0;
		gbl.setConstraints(numl, cc);
		controls.add(numl);
		
		cc.gridx = 1;
		gbl.setConstraints(num, cc);
		controls.add(num);
		
		cc.gridx = 0;
		cc.gridy = 1;
		cc.gridwidth = 2;
		gbl.setConstraints(go, cc);
		controls.add(go);
		
		validate();
	}
	
	// Handle events
	public boolean handleEvent(Event evt)
	{
		if (evt.target instanceof Button)
		{
			if (evt.target == go && evt.id == Event.ACTION_EVENT)	// When button is clicked
			{
				disp.setText("");			// Reset output window
        		simulate(Integer.valueOf(num.getText()).intValue());
        		return true;					// Generate correct number of tosses
			}
		}
		return super.handleEvent(evt);	// Handle other events as usual
	}
	
	// Permute
    public void simulate(int num)
    {
    	String[] input;
    	
    	input = new String[num];
	    for (int i = 0; i < num; i++) {
	    	input[i] = String.valueOf(i + 1);		// label 1 - n
	    }								
    									
    	myC = new Combinatorics(input);
    	myC.allPerm(disp);	
    }

}


