
import java.awt.AWTException;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JMenu;

import java.awt.BorderLayout;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Robot;
import java.awt.Shape;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.font.FontRenderContext;
import java.awt.font.GlyphVector;
import java.awt.event.MouseAdapter;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.StringTokenizer;

import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JTextArea;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.CubicCurve2D;
import java.awt.image.BufferedImage;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;


public class Graph04 
{
 
	private ArrayList<LineObj>		lines;	
	private ArrayList<CircleObj>	circles;	
	
	
    public Graph04() 
    { 
    	this(new ArrayList<LineObj>(), new ArrayList<CircleObj>()); 
  	}

    public Graph04(ArrayList<LineObj> initialLine, ArrayList<CircleObj> initialCircle) 
    {
    	lines = initialLine;
    	circles = initialCircle;
    }
    
    public void addLines(LineObj aLine) 
    {
    	lines.add(aLine);
    }
    
    public LineObj LineAt(double x, double y) 
    {
   	
    	int k = 30;
    	
        for (int i = 0;  i< lines.size() ; i++)
        {
            LineObj line = lines.get(i); 
         
            Rectangle net = new Rectangle(k,k);
    		
  		  	Line2D.Double aqw = new Line2D.Double(line.getX1() , line.getY1() , line.getX2() , line.getY2());
  		  
  		  	net.setFrameFromCenter(x, y, x+(k/2), y+(k/2) );
            if(aqw.intersects(net)) 
            {
            	return line;
            }
            
        }
        
        return null;
    }
  
    public ArrayList<LineObj> selectedLines() 
    {
        ArrayList<LineObj>   selected_lines = new ArrayList<LineObj>();
        for (LineObj item:  lines)
            if (item.isSelected()) selected_lines.add(item);
        return selected_lines;
    }
   
    public void effacer_tout() 
    {
    	lines.clear();
    	circles.clear();
    }
    
    public void deleteLine(LineObj l) 
    {
    	lines.remove(l); 
    }

    public ArrayList<LineObj> getLines() 
    { 
    	return lines; 
   	}
    
    /**********************************************************/
    
    public CircleObj CircleAt(double x, double y) 
    {
        for (int i = circles.size()-1; i>=0; i--)
        {
        	CircleObj n = circles.get(i);

        	int diametre = n.get_diametre();
        	int rayon = diametre / 2;
        	
        	int xcentre = n.getX1() + rayon;
        	int ycentre = n.getY1() + rayon;
        	        	
   		    System.out.println(" Centre " + xcentre + " " + ycentre );	
        	
            double d = ( (x - xcentre) * (x - xcentre) +  (y - ycentre) * (y - ycentre) ) ;
            if (d <= (rayon * rayon))
            {	
                return n;
            }    
        }
        return null;
    }
    
    
    public CircleObj new_CircleAt(double x, double y) 
    {
   	
    	for (int i = circles.size()-1; i>=0; i--)
        {
        	CircleObj n = circles.get(i);
        	int diametre = n.get_diametre();
        	int rayon = diametre / 2;
         
            int xmin = n.getX1() - 20;
   		 	int ymin = n.getY1() - 20;
   		 	int xmax = xmin + diametre + 20;
   		 	int ymax = ymin + diametre +20;
   		 
   		 	if(x >= xmin && x <= xmax && y >= ymin && y <= ymax  )
   		 	{
   		 		System.out.println("Circle pressed.. "); 
   		 		return n;
   		 	}
            
        }
        
        return null;
    }
    
    public void addcircle(CircleObj aCircle) 
    {
    	circles.add(aCircle);
    }
    
    public void circle_effacer_tout() 
    {
    	circles.clear();
    }
    
    public void deleteCircles(CircleObj l) 
    {
    	circles.remove(l); 
    }

    
    public ArrayList<CircleObj> getCircles() 
    { 
    	return circles; 
   	}
    
    public ArrayList<CircleObj> selectedcircles() 
    {
        ArrayList<CircleObj>   selected_circles = new ArrayList<CircleObj>();
        for (CircleObj item:  circles)
            if (item.isSelected()) selected_circles.add(item);
        return selected_circles;
    }
    
  
    
}

