// MattsColorsV2.java
// Modifies Figure 12.19 to allow for colors
// Demonstrates mouseDragged method
// Adjusts brush size with TextFields
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MattsColorsV2 extends JApplet implements ActionListener, ItemListener {
	// GUI components
	private Container con;
	private String backNames[ ] = { "White Background", "Blue Background", "Black Background", "Cyan Background" };
	private String shapeNames[ ] = { "Circle", "Triangle", "Square" };
	private String colorNames[ ] = { "Red", "Blue", "Magenta", "Green", "Yellow", "Cyan", "White", "Black" };
	private JRadioButton backColors[ ];
	private JRadioButton shapes[ ];
	private JButton drawColors[ ];
	private JLabel brushShapeLabel, labelWidth, labelHeight;
	private JTextField brushWidth, brushHeight;
	private ButtonGroup brushShapesGroup, backgroundsGroup;
	private JPanel p1;
	private MattPanel p2;
	private int passColor, passShape;

	public void init( ) {
		con = getContentPane( );
		con.setLayout( new BorderLayout( 5, 5 ) );

		p1 = new JPanel( );
		p1.setLayout( new GridLayout( 5, 4, 3, 3 ) );

		brushShapeLabel = new JLabel( "Brush Shape" );
		p1.add( brushShapeLabel );
		
		// create 3 brush shape radio buttons
		// add listener and register each button with group
		brushShapesGroup = new ButtonGroup( );
		shapes = new JRadioButton[ shapeNames.length ];

		for ( int i = 0; i < shapeNames.length; i++ ) {
			shapes[ i ] = new JRadioButton( shapeNames[ i ], ( i == 0 ? true : false ) );
			shapes[ i ].addItemListener( this );
			brushShapesGroup.add( shapes[ i ] );
			p1.add( shapes[ i ] );
		}

		// create and add labels and text fields for brush width and height
		// register applet as the action listener 
		labelWidth = new JLabel( "Brush Width" );
		p1.add( labelWidth );
		brushWidth = new JTextField( "30", 3 );
		brushWidth.addActionListener( this );
		p1.add( brushWidth );
		labelHeight = new JLabel( "Brush Height" );
		p1.add( labelHeight );
		brushHeight = new JTextField( "30" , 3 );
		brushHeight.addActionListener( this );
		p1.add( brushHeight );

		// create and add buttons for drawing colors
		drawColors = new JButton[ colorNames.length ];
		for ( int i = 0; i < colorNames.length; i++ ) {
			drawColors[ i ] = new JButton( colorNames[ i ] );
			drawColors[ i ].addActionListener( this );
			p1.add( drawColors[ i ] );
		}

		// create and add background radio buttons
		backgroundsGroup = new ButtonGroup( );
		backColors = new JRadioButton[ backNames.length ];
		for ( int i = 0; i < backNames.length; i++ ) {
			backColors[ i ] = new JRadioButton( backNames[ i ], ( i == 0 ? true : false ) );
			backColors[ i ].addItemListener( this );
			backgroundsGroup.add( backColors[ i ] );
			p1.add( backColors[ i ] );
		}

		con.add( p1, BorderLayout.SOUTH );

		p2 = new MattPanel( );
		con.add( p2, BorderLayout.CENTER );

		passColor = 7;  // black is default
		passShape = 0;  // circle shape is default
	}

	public void actionPerformed( ActionEvent e ) {
		for ( int i = 0; i < drawColors.length; i++ )
			if ( e.getSource( ) == drawColors[ i ] ) passColor = i;

		p2.drawInfo( passColor, passShape, Integer.parseInt( brushWidth.getText( ) ), Integer.parseInt( brushHeight.getText( ) ) );
	}

	public void itemStateChanged( ItemEvent e ) {
		// check if background radio button clicked
		if ( e.getSource( ) == backColors[ 0 ] ) p2.setBackground( Color.white );
		else if ( e.getSource( ) == backColors[ 1 ] ) p2.setBackground( Color.blue );
		else if ( e.getSource( ) == backColors[ 2 ] ) p2.setBackground( Color.black );
		else if ( e.getSource( ) == backColors[ 3 ] ) p2.setBackground( Color.cyan );

		// check if shapes radio button clicked
		for ( int i = 0; i < shapes.length; i++ )
			if ( e.getSource( ) == shapes[ i ] ) passShape = i;

		p2.drawInfo( passColor, passShape, Integer.parseInt( brushWidth.getText( ) ), Integer.parseInt( brushHeight.getText( ) ) );
	}
}