//----------------------------------------------------------------------
//----------------------------------------------------------------------
//----------------------------------------------------------------------
//
//		-----------------------
//		 Utilitaire de dessin:
//		-----------------------
//
//----------------------------------------------------------------------
//----------------------------------------------------------------------

//	Java --	Langage de base:
//	
import		java.io.*;

//	Java --	Graphisme de base:
//	
import		java.awt.*;
import		java.awt.event.*;

//	Java --	Graphisme, images:
//	
import		java.awt.image.*;
import		javax.imageio.*;

//----------------------------------------------------------------------
//----------------------------------------------------------------------

//----------------------------------------------------------------------
//----------------------------------------------------------------------
//----------------------------------------------------------------------
//----------------------------------------------------------------------

/**
 *	Any 'MissionExecutor' should be able to process 'clicks'
 *	re-directed to it:
 *
 *	from a 'MyButton' thru method 'executeMission',
 *	from a 'MyCanvas' thru method 'mouseClicked'.
 */
interface	MissionExecutor
	{
// debut d'interface
//----------------------------------------------------------------------

public
void		executeMission		(int no)
	;

public
void		mouseClicked		(MouseEvent evt)
	;

//----------------------------------------------------------------------
// fin d'interface
	}
	
//----------------------------------------------------------------------
//----------------------------------------------------------------------
//----------------------------------------------------------------------
//----------------------------------------------------------------------

/**
 *	A 'Drawing' is an independant graphic component.
 *
 *	The 'main' method is only a functionality test.
 */
public
class		Drawing			extends		Panel
					implements	MissionExecutor
	{
// debut de classe
//----------------------------------------------------------------------

/**
 *	'executeMission'
 *	is part of implementation of 'MissionExecutor'.
 */
public
void		executeMission		(int no)
	{
	switch	(no)
		{
		// shape
		// 
		case	51:
			{
			}
		break;
		case	52:
			{
			}
		break;
		case	53:
			{
			}
		break;

		// color
		//
		case	61:
			{
			}
		break;
		case	62:
			{
			}
		break;
		case	63:
			{
			}
		break;

		// other
		// 
		case	0:
			{
			}
		break;
		default	:
			{
			}
		}
	}

/**
 *	'mouseClicked'
 *	is part of implementation of 'MissionExecutor'.
 */
public
void		mouseClicked		(MouseEvent evt)
	{
	a("...Click en "+evt.getX()+"\t"+evt.getY());
	}

/**
 *	Backstage image, supporting the screen display on 'theCanvas'.
 */ 
BufferedImage	theImage		= new BufferedImage
			(300, 300, BufferedImage.TYPE_INT_ARGB);

/**
 *	Construction of a 'Drawing' object.
 */
public
		Drawing			()
	{
	setLayout(new BorderLayout());
	
	Panel	theButtons	= new Panel();
	theButtons	.setLayout(new GridLayout(0, 1));
	
	Panel	theShapes	= new Panel();
	theShapes	.setLayout(new FlowLayout());
	Panel	theColors	= new Panel();
	theColors	.setLayout(new FlowLayout());
	Panel	theIO		= new Panel();
	theIO		.setLayout(new FlowLayout());

	MyButton	theShapeLine	= new MyButton("line",	this, 51);
	MyButton	theShapeRectangle= new MyButton("rect",	this, 51);
	MyButton	theShapeOval	= new MyButton("oval",	this, 51);
	theShapes.add(theShapeLine);
	theShapes.add(theShapeRectangle);
	theShapes.add(theShapeOval);
	theButtons.add(theShapes);
	
	MyButton	theColorRed	= new MyButton("red",	this, 61);
	MyButton	theColorBlue	= new MyButton("blue",	this, 62);
	MyButton	theColorGreen	= new MyButton("green", this, 63);
	theColors.add(theColorRed);
	theColors.add(theColorBlue);
	theColors.add(theColorGreen);
	theButtons.add(theColors);
	
	TextField	theFName	= new TextField("Enter file name");
	MyButton	theIN		= new MyButton("input", this, 71);
	MyButton	theOUT		= new MyButton("output",this, 72);
	theIO.add(theFName);
	theIO.add(theIN);
	theIO.add(theOUT);
	theButtons.add(theIO);

	MyCanvas	theCanvas	= new MyCanvas(theImage, this);
	theCanvas.setSize(400, 400);
	
	add(theButtons,	BorderLayout.NORTH);
	add(theCanvas,	BorderLayout.CENTER);
	}


/**
 *	Print/display utility.
 */
public
static
void		a			(String s)
	{
	System.out.println(s);
	}

/**
 *	Testing facility.
 */
public
static
void		main			(String[] args)
					throws		Exception
	{
	a(" ");
	a("-------------------------------");
	a(" Drawing component: testing... ");
	a("-------------------------------");
	a("\nB.M.G. version 2014 Hiver \n");

	Frame	theFrame	= new Frame("Drawer (?)");
	Drawing	theAppli	= new Drawing();
	
	theAppli.setSize(200, 200);
	theFrame.setSize(200, 200);
	theFrame.add(theAppli, BorderLayout.CENTER);
	theFrame.validate();
	theFrame.setVisible(true);
	}

//----------------------------------------------------------------------
// fin de classe
	}

//----------------------------------------------------------------------
//----------------------------------------------------------------------
//----------------------------------------------------------------------
//----------------------------------------------------------------------

/**
 *	A 'MyButton' listens to clicks occuring on it, 
 *	and redirect them to a 'MissionExcecutor'.
 */
class		MyButton		extends		Button
					implements	ActionListener
	{
// debut de classe
//----------------------------------------------------------------------

/**
 *	Print/display utility.
 */
public
static
void		a			(String s)
	{
	System.out.println(s);
	}

public
		MyButton		(String s, MissionExecutor me, int n)
	{
	super(s);
	mi = me;
	missionno = n;
	addActionListener(this);
	}

public
void		actionPerformed		(ActionEvent evt)
	{
	mi.executeMission(missionno);
	}

MissionExecutor	mi;
int		missionno;

//----------------------------------------------------------------------
// fin de classe
	}

//----------------------------------------------------------------------
//----------------------------------------------------------------------
//----------------------------------------------------------------------
//----------------------------------------------------------------------

/**
 *	A 'MyCanvas' is in charge of displaying a BufferedImage,
 *	and redirects clicks occuring on itself to a 'MissionExecutor'.
 */
class		MyCanvas		extends		Canvas
					implements	MouseListener
	{
// debut de classe
//----------------------------------------------------------------------

/**
 *	Print/display utility.
 */
public
static
void		a			(String s)
	{
	System.out.println(s);
	}

public
void		paint			(Graphics g)
	{
	g.setColor(Color.BLUE);
	g.fillRect(1, 1, getWidth(), getHeight());
	g.drawImage(theImage, 0, 0, this);
	}

/**
 *	'MouseListener' void implementation.
 */

public
void		mouseEntered		(MouseEvent evt)	{}

public
void		mouseExited		(MouseEvent evt)	{}

public
void		mousePressed		(MouseEvent evt)	{}

public
void		mouseReleased		(MouseEvent evt)	{}

/**
 *	'MouseListener' implementationi to redirect clicks.
 */
public
void		mouseClicked		(MouseEvent evt)
	{
	theExecutor.mouseClicked(evt);
	}

/**
 *	'MissionExecutor' that shall process clicks.
 */
public
MissionExecutor	theExecutor;

/**
 *	Image to be displayed.
 */
public
BufferedImage	theImage;

/**
 *	Construction of a 'MyCanvas'.
 */
public		
		MyCanvas		(BufferedImage bi, MissionExecutor me)
	{
	super();
	addMouseListener(this);
	theImage	= bi;
	theExecutor	= me;
	}

//----------------------------------------------------------------------
// fin de classe
	}

//----------------------------------------------------------------------
//----------------------------------------------------------------------
//----------------------------------------------------------------------
//----------------------------------------------------------------------

