/*
 *   Copyright (C) 2001 Bryce Allen
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.util.*;
import javax.swing.*;

public class MatrixPanel extends JComponent
{
	private double[] matrix = new double[9];
	
	public MatrixPanel()
	{
		for(int i=0; i < matrix.length; ++i)
		{
			matrix[i] = (i % 4) == 0 ? 1 : 0;
		}
	}
	
	public MatrixPanel(double[] aMatrix)
	{
		this();
		setMatrix(aMatrix);
	}

	public Dimension getMinimumSize()
	{
		return new Dimension(200, 150);
	}

	public Dimension getPreferredSize()
	{
		return getMinimumSize();
	}

	public void setMatrix(double[] aMatrix)
	{
		matrix[0] = aMatrix[0];
		matrix[1] = aMatrix[2];
		matrix[2] = aMatrix[4];
		matrix[3] = aMatrix[1];
		matrix[4] = aMatrix[3];
		matrix[5] = aMatrix[5];
		matrix[6] = 0;
		matrix[7] = 0;
		matrix[8] = 1;

/*
		for(int i=0; i < matrix.length; ++i)
		{
			if(i >= aMatrix.length)
			{
				matrix[i] = (i % 4) == 0 ? 1 : 0;
			}
			else
			{
				matrix[i] = aMatrix[i];
			}
		}
*/
		repaint();
	}

	public void setMatrix(AffineTransform trans)
	{
		double[] transMatrix = new double[6];
		trans.getMatrix(transMatrix);
		setMatrix(transMatrix);
	}

	public double[] getMatrix()
	{
		return matrix;
	}

	public void paintComponent(Graphics g)
	{
		Graphics2D g2 = (Graphics2D)g;
//		g2.draw(new Rectangle2D.Double(0, 0, getWidth(), getHeight()));
		String[] strings = new String[matrix.length];
		int maxLength = 5;
		for(int i=0; i < matrix.length; ++i)
		{
			strings[i] = String.valueOf(matrix[i]);
			if(strings[i].length() > maxLength)
			{
				strings[i] = strings[i].substring(0, 5);
			}
		}
		g2.drawString(strings[0], 10, 10);
		g2.drawString(strings[1], 70, 10);
		g2.drawString(strings[2], 130, 10);
		g2.drawString(strings[3], 10, 40);
		g2.drawString(strings[4], 70, 40);
		g2.drawString(strings[5], 130, 40);
		g2.drawString(strings[6], 10, 70);
		g2.drawString(strings[7], 70, 70);
		g2.drawString(strings[8], 130, 70);
	}
}

