
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 Graph_circle 
{
 
	private String					label;
	private ArrayList<CircleObj>	circles;	
	
	public Graph_circle() 
    { 
    	this("", new ArrayList<CircleObj>()); 
  	}

    public Graph_circle(String aLabel) 
    { 
    	this(aLabel, new ArrayList<CircleObj>()); 
   	}

    public Graph_circle(String aLabel, ArrayList<CircleObj> initialCircles) 
    {
        label = aLabel;
        circles = initialCircles;
    }
    
    public void addCircle(CircleObj aCircle) 
    {
    	circles.add(aCircle);
    }
    
    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) 
    {
   	
    	int x1, y1;
      	int w = 0, h = 0;
    	
    	for (int i = circles.size()-1; i>=0; i--)
        {
        	CircleObj circle = circles.get(i);
        	
       	     x1 = circle.getX1();
 		     y1 = circle.getY1(); 
		    	
	    	 if(circle.getWidth() > 0 && circle.getHeight() > 0)
		     {
	    		    // on a un cercle
		    		if(circle.getWidth() == circle.getHeight())
		    		{
		    			w =  circle.getWidth();
		    			h =  w;
		    		}
		    		// on a une ellipse
		    		else
		    		{
		    			w =  circle.getWidth();
		    			h =  circle.getHeight();
		    		}
		    }
		    else
		    {
		    	w = circle.get_diametre();
		    	h = circle.get_diametre();
		    }
        	
         
            int xmin = x1 - 20;
   		 	int ymin = y1 - 20;
   		 	int xmax = xmin + w + 20;
   		 	int ymax = ymin + h +20;
   		 
   		 	if(x >= xmin && x <= xmax && y >= ymin && y <= ymax  )
   		 	{
   		 		System.out.println("Circle pressed.. " + xmin + " " + ymin + " " + xmax + " " + ymax); 
   		 		return circle;
   		 	}
            
        }
        
        return null;
    }
    
    public void addcircle(CircleObj aCircle) 
    {
    	circles.add(aCircle);
    }
    
    public void 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;
    }
    
    public void saveTo(PrintWriter aFile) 
    {
        aFile.println(label);
        
        aFile.println(circles.size());
        for (CircleObj item:  circles)
            item.saveTo(aFile);
    }
    
    public static Graph_circle loadFrom(BufferedReader aFile) throws IOException 
    {
       
    	Graph_circle    aGraph = new Graph_circle(aFile.readLine());

        int numCircles = Integer.parseInt(aFile.readLine());
        for (int i=0; i<numCircles; i++) 
        {
        	CircleObj tempCircle = CircleObj.loadFrom(aFile);
          //  Node start = aGraph.nodeAt(tempEdge.getStartNode().getLocation().getX(),  tempEdge.getStartNode().getLocation().getY());
          //  Node end = aGraph.nodeAt(tempEdge.getEndNode().getLocation().getX(),  tempEdge.getEndNode().getLocation().getY());
            aGraph.addcircle(tempCircle); //.addEdge(start, end);
        }
        
        return aGraph;
    }
   
    
}

