import java.awt.*;
import Raster;
import javax.swing.*;
import java.awt.image.*;
import java.awt.geom.*;
import java.awt.event.*;



public class Rastest extends JPanel implements MouseListener, MouseMotionListener {	

	Rastest()	{	
		setBackground(Color.blue);
	

		addMouseMotionListener(this);
        addMouseListener(this);
        
        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);
		System.out.println(" get...WH" + getWidth() + " / " + getHeight());
		System.out.println(" F1 get...WH" + frame.getWidth() + " / " + frame.getHeight());

		width = getWidth();
		height = getHeight();
        frame.contentPane.add(this,BorderLayout.CENTER);
        
	
		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;
        
 /////////////       

        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();
            }
        });

 		bPanel  = new JPanel();
 		bPanel.setLayout(new FlowLayout());
 		bPanel.add(button2);
 		bPanel.add(button3);
 		bPanel.add(button1);
		frame.contentPane.add(bPanel,BorderLayout.NORTH);
		frame.contentPane.add(label,BorderLayout.SOUTH);
///////////////////

		lineType = 0;
		mouseP = false;
		frame.show();
		System.out.println(" F3 get...WH" + frame.getWidth() + " / " + frame.getHeight());
	}
	
	public static void main(String[] args)	{		
		Rastest panel = new Rastest();
		

	}


	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));
		}
	}
	
	
	
		/** Handles the event of the user pressing down the mouse button.	*/
    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.
    	*/
    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.	*/
	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)	{}
					 

int					lineType, olineType,nbType;
String[]			algoType;
Color[]				algoColor;
JLabel 				label;	
JButton 			button1,button2,button3;		
JPanel 				bPanel;
Point2D				P1,P2;
Point				rP1,rP2;
ApplicationFrame	frame;
VisuRaster			visuR;	
boolean				mouseP;
float				width, height;
}