// MattPanel.java
// Customized JPanel class for MattsColors
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MattPanel extends JPanel {
	// instance variables
	private int drawColor, drawShape, drawWidth, drawHeight;
	private Color colors[ ] = { Color.red, Color.blue, Color.magenta, Color.green, Color.yellow, Color.cyan, Color.white, Color.black };
	private boolean drawOK = false;	// to prevent drawing of brush upon start up

	private int x1, y1;		// mouse coordinates

	// constructor
	public MattPanel( ) {
		addMouseListener(
			new MouseAdapter( ) {
				public void mousePressed( MouseEvent e ) {
					x1 = e.getX( );
					y1 = e.getY( );
					repaint( );	// calls paintComponent
				}

				public void mouseReleased( MouseEvent e ) {
					x1 = e.getX( );
					y1 = e.getY( );
				}
			}
		);

		addMouseMotionListener(
			new MouseMotionAdapter( ) {
				public void mouseDragged( MouseEvent e ) {
					x1 = e.getX( );
					y1 = e.getY( );
					repaint( );
				}
			}
		);
	}
	
	public void paintComponent( Graphics g ) {
		super.paintComponent( g );
		if ( drawOK ) {
			g.setColor( colors[ drawColor ] );			

			if ( drawShape == 0 ) {
				// draw with circular brush with mouse pointer in center
				g.fillOval( x1 - drawWidth / 2, y1 - drawHeight / 2, drawWidth, drawHeight );
			} else if ( drawShape == 1 ) {
				// draw with triangular brush with mouse pointer in center
				int xTri[ ] = { x1 - drawWidth / 2, x1, x1 + drawWidth / 2 };
				int yTri[ ] = { y1 + drawHeight / 2, y1 - drawHeight / 2, y1 + drawHeight / 2 };
				int pts = xTri.length;
				Polygon poly = new Polygon( xTri, yTri, pts );
				g.fillPolygon( poly );
			} else if ( drawShape == 2 ) {
				// draw with rectangular brush with mouse pointer in center
				g.fillRect( x1 - drawWidth / 2, y1 - drawHeight / 2, drawWidth, drawHeight );
			}
		}
	}

	public void drawInfo( int c, int s, int w, int h ) {
		drawOK = true;	// toggle to true to enable drawing
		drawColor = c;
		drawShape = s;
		drawWidth = w;
		drawHeight = h;
	}
}