// MatrixWindow.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 MatrixWindow	extends java.awt.Frame{	Panel dispArea;	Panel controls;		// Panel for user controls	Panel matPanel;		Label numl;			// Controls	TextField num;		TextField[][] m;	Matrix myMat;		Button go;		GridBagLayout gbl, mgbl;	GridBagConstraints cc, mcc;		int size;		public MatrixWindow(int n) {		super("MatrixPowers");		size = n;	}		// Initialize applet	public void init()	{			numl = new Label("Power = ");			// Create controls		num = new TextField("1", 4);		go = new Button("Go");				if (size == 3) {			m = new TextField[size][size];			m[0][0] = new TextField(".5", 4);			m[0][1] = new TextField(".25", 4);			m[0][2] = new TextField(".25", 4);			m[1][0] = new TextField(".5", 4);			m[1][1] = new TextField("0", 4);			m[1][2] = new TextField(".5", 4);			m[2][0] = new TextField(".25", 4);			m[2][1] = new TextField(".25", 4);			m[2][2] = new TextField(".5", 4);		}		else {			m = new TextField[size][size];			for (int r = 0; r < size; r++) {				for (int c = 0; c < size; c++) {					m[r][c] = new TextField("0", 4);				}				}		}					dispArea = new Panel();				// Set up window		controls = new Panel();		matPanel = new Panel();		setLayout(new BorderLayout(5, 5));				add("South", controls);		add("Center", dispArea);				mgbl = new GridBagLayout();		mcc = new GridBagConstraints();		matPanel.setLayout(mgbl);		for (int r = 0; r < size; r++) {			for (int c = 0; c < size; c++) {				mcc.gridx = c;				mcc.gridy = r;				mgbl.setConstraints(m[r][c], mcc);				matPanel.add(m[r][c]);			}			}				dispArea.setLayout(new GridLayout(1, 2));		dispArea.add(matPanel);				myMat = new Matrix(m);				dispArea.add(myMat);				gbl = new GridBagLayout();		controls.setLayout(gbl);				cc = new GridBagConstraints();				cc.gridx = 0;		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)	{		String minStr, maxStr;		if (evt.target instanceof Button)		{			if (evt.target == go && evt.id == Event.ACTION_EVENT)	// When button is clicked			{        		simulate(Integer.valueOf(num.getText()).intValue());        		return true;					// Generate correct number of tosses			}		}		if (evt.id == Event.WINDOW_DESTROY) {			hide();			dispose();			}		return super.handleEvent(evt);	// Handle other events as usual	}		// Calculate probabilities    public void simulate(int num)    {	    	dispArea.remove(myMat);    	myMat = new Matrix(m);   	 	Matrix temp = myMat;   	 	for (int i = 1; i < num; i++) {	// Do nothing for 1st power   	 		myMat = Matrix.multiply(temp, myMat);	   	 	}   	 	myMat.round(.001f);   	 	dispArea.add(myMat);   	 	validate();	}		public float round(float num, float accuracy) {		return accuracy * Math.round(num / accuracy);	}	}
