/** Line Drawing Demonstrations
*/


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



public class LineDraw extends JPanel implements MouseListener, MouseMotionListener {	

	LineDraw()	{	


	//	-----	Events
	//  Adds the specified mouse motion listener to receive mouse motion events from this component.
		addMouseMotionListener(this);
        addMouseListener(this);

   	//	-----	Various Variables   
        
		lineType = 0;
		mouseP = false;
		algoType  = new String[] {"Simple Line","Improved Simple","DDA","Bresenham", "Two Step"};
		algoColor = new Color[] {Color.blue,Color.black,Color.yellow,Color.red,Color.magenta,Color.orange};
		nbType = algoType.length;

	//  -----	Frame and Panel things
	
		setBackground(Color.blue);
        ApplicationFrame frame = new ApplicationFrame("Line Drawing Show");
        visuR = new VisuRaster(20,20,19.);

		setSize(visuR.getRWidth(),visuR.getRHeight());
		frame.setSize(visuR.getRWidth(),visuR.getRHeight()+70);

		width = getWidth();
		height = getHeight();
//        frame.contentPane.add(this,BorderLayout.CENTER);
		frame.add(this);


	//	----	Button on a different sub panel with a simple Layout
        label = new JLabel("Default : " + algoType[lineType], SwingConstants.CENTER);

        button1 = new JButton("N: " + algoType[lineType+1]);
        button1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            	
                button2.setText("P: " + algoType[lineType]);

                lineType = (lineType+1)%nbType;
                olineType = (lineType+1)%nbType;
                
                System.out.println("lineType : "+lineType+"  olineType : "+olineType);
                
                label.setText("Now : " + algoType[lineType]);
                button1.setText("N: " + algoType[olineType]);
            }
        });

        button2 = new JButton("P: " + algoType[nbType-1]);
        button2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            
            	button1.setText("N: " + algoType[lineType]);
            	
                if(lineType>0)	{
                	lineType--;
                }	else	{
                	lineType = nbType-1;
                }
                if (lineType>0)	{
                	olineType = lineType-1;
                }	else	{
                	olineType = nbType-1;
                }

                System.out.println("lineType : "+lineType+"  olineType : "+olineType);
                
                label.setText("Now : " + algoType[lineType]);
                button2.setText("P: " + algoType[olineType]);
            }
        });

        button3 = new JButton("E");
        button3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            
            	visuR.fill(Color.white);
            	visuR.reset();
            	mouseP = false;
            	repaint();
            }
        });

        button4 = new JButton("D");
        button4.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            
            	visuR.fill(Color.white);
            	visuR.exportResult();
            	mouseP = false;
            	repaint();
            }
        });
        
        

 		bPanel  = new JPanel();
 		bPanel.setLayout(new FlowLayout());
 		bPanel.add(button2);
 		bPanel.add(button3);
// 		bPanel.add(button4);
 		bPanel.add(button1);
 		
 	//	----	subpanel and label
		frame.add(bPanel,BorderLayout.NORTH);
		frame.add(label,BorderLayout.SOUTH);

	//	---- Let's show the result...
		frame.show();
	}
	

	public static void main(String[] args)	{		
		LineDraw panel = new LineDraw();
	}

	
/**
	Main draw method
	*/
	
	public void paintComponent(Graphics g)	{
		super.paintComponent(g);
		Graphics2D g2 = (Graphics2D) g;
		
		
		visuR.paint(g2);
		
		if (mouseP)	{
			g2.setPaint(Color.green);
			g2.draw(new Line2D.Float(P1,P2));
		}
	}
	
//	----	Mouses events occuring in the raster visualisation
//	----------------------------------------------------------------------

		/** Handles the event of the user pressing down the mouse button.	
		get the beginning of the line, in the real raster and in zoomed one */
    public void mousePressed(MouseEvent e)	{
    	double zoom = visuR.getZoom();
    	
		rP1 = new Point((int)(e.getX()/zoom), (int)((height-e.getY())/zoom));
		System.out.println("rP1 : " + rP1.getX() + " / " + rP1.getY());
		P1 = new Point2D.Double(zoom*(rP1.getX()+0.5),height-zoom*(rP1.getY()+0.5));
		rP2 = new Point(rP1);
		P2 = new Point2D.Double(P1.getX(),P1.getY());
	
		mouseP = true;
    }
    
	/** Handles the event of a user dragging the mouse while holding
    	down the mouse button.
    	draw an intermediary line
    	*/
    public void mouseDragged(MouseEvent e)	{
    	double zoom = visuR.getZoom();
		rP2 = new Point((int)(e.getX()/zoom), (int)((height-e.getY())/zoom));
		P2 = new Point2D.Double(zoom*(rP2.getX()+0.5),height-zoom*(rP2.getY()+0.5));
		repaint();
    }

	/** Handles the event of a user releasing the mouse button.	
		Get the end od the line
		*/
	public void mouseReleased(MouseEvent e)	{	
    	double zoom = visuR.getZoom();
		rP2 = new Point((int)(e.getX()/zoom), (int)((height-e.getY())/zoom));
		System.out.println("rP2 : " + rP2.getX() + " / " + rP2.getY());
		P2 = new Point2D.Double(zoom*(rP2.getX()+0.5),height-zoom*(rP2.getY()+0.5));
		
		
		switch(lineType)	{
		case 0:
			visuR.lineSimple(rP1, rP2 , algoColor[lineType]);
			break;
		case 1:
			visuR.lineImproved(rP1, rP2 , algoColor[lineType]);
			break;
		case 2:
			visuR.lineDDA(rP1, rP2 , algoColor[lineType]);
			break;
		case 3:
			visuR.lineBresenham(rP1, rP2 , algoColor[lineType]);
			break;
		case 4:
			visuR.lineTwoStep(rP1, rP2 , algoColor[lineType]);
			break;
		}
		repaint();
	}
	
	/** This method is required by MouseListener.	*/
	public void mouseMoved(MouseEvent e)	{}
	
	/** These methods are required by MouseMotionListener.	*/
	public void mouseClicked(MouseEvent e)	{}
	public void mouseExited(MouseEvent e)	{}
	public void mouseEntered(MouseEvent e)	{}
	
	
//	----	Fields declaration	------------------------------------------
//	----------------------------------------------------------------------
	private		String[]			algoType;
	private		Color[]				algoColor;
	private		JLabel 				label;	
	private		JButton 			button1,button2,button3,button4;		
	private		JPanel 				bPanel;
	private 	Point2D				P1,P2;
	private 	Point				rP1,rP2;
	private 	ApplicationFrame	frame;
	private 	VisuRaster			visuR;	
	private 	boolean				mouseP;
	private 	int					lineType, olineType, nbType;
	private		float				width, height;
	
}