

import java.awt.*;
import java.awt.event.*;

public class ArtPanel extends Panel {
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	FormeGeometrique[] fg;
    int size;
    boolean selected;
   
    public ArtPanel(int n, int s)
    {
        size = s;
//        fg = new FormeGeometrique[n];
        fg = new FormeGeometrique[1];
        selected = false;
/*        for (int i=0;i<fg.length;i++)
        {
            if (i%2==0)
                fg[i] = new CercleGraph(randInt(0,s-200),randInt(0,s-200),randInt(20,200),randColor());
            else
                fg[i] = new RectangleGraph(randInt(0,s-200),randInt(0,s-200),randInt(20,200),randInt(20,200),randColor());
        }*/
        //fg[0] = new ArbreGraph(randInt(0,s-200),randInt(0,s-300),randInt(10,100),randInt(20,250));
        //fg[0] = new ArbreGraph(randInt(0,s-200),randInt(0,s-300),randInt(70,100),randInt(140,250));
        fg[0] = new CercleGraph(randInt(0,s-200),randInt(0,s-200),randInt(0,s-300),Color.BLUE);
        addMouseListener(new PanelClickListener(this));
        addMouseMotionListener(new PanelDragListener(this));
    }
   
    public void paint(Graphics g)
    {
       paintComponent(g);
    }
   
     
    private int randInt(int min, int max)
    {
        return (int)(min+(max-min)*Math.random());        
    }
    private Color randColor()
    {
        return new Color((float)Math.random(),(float)Math.random(),(float)Math.random());
    }

    
    protected void paintComponent(Graphics gr)
    {
        int w = getWidth();
        int h = getHeight();
        Image buffer = createImage(w, h);
        // get buffer's graphics context
        //Graphics2D g2D = (Graphics2D)buffer.getGraphics();
        Graphics g2D = buffer.getGraphics();

        for (int i=0;i<fg.length;i++)
            fg[i].dessiner(g2D);        
        // draw buffer's graphics context to the parent one
        gr.drawImage(buffer, 0, 0, this);
    }

    
}

class PanelDragListener implements MouseMotionListener
{
    ArtPanel a;
    int x=0;
    int y=0;
    public PanelDragListener(ArtPanel a)
    {
        this.a = a;
    }

    public void mouseMoved(MouseEvent e) {
        x = e.getX();
        y = e.getY();
    }   
    public void mouseDragged(MouseEvent e) {
        int diffx = e.getX() - x;
        int diffy = e.getY() - y;
        if (a.selected)
        {
            FormeGeometrique x = a.fg[a.fg.length-1];
            if (!x.leavesX(diffx,a.getWidth()))  x.dragX(diffx);
            if (!x.leavesY(diffy,a.getHeight())) x.dragY(diffy);
            //x.dragX(diffx);
            //x.dragY(diffy);
        }
        x = e.getX();
        y = e.getY();
        a.paintComponent(a.getGraphics());
    }   
}

class PanelClickListener implements MouseListener
{
    ArtPanel a;
    public PanelClickListener(ArtPanel a)
    {
        this.a = a;
    }
    
    public void mouseExited(MouseEvent e) {
    }
    public void mouseEntered(MouseEvent e) {
    }
    public void mouseReleased(MouseEvent e) {
        a.selected = false;
    }
    public void mouseClicked(MouseEvent e) {
    }
    public void mousePressed(MouseEvent e)
    {
         int found = -1;
         for (int i=a.fg.length-1;i>=0;i--)
            if (a.fg[i].inside(e.getX(),e.getY()))
            {
                found = i;
                break;
            }
        if (found>=0)
        {
            a.selected = true;
            FormeGeometrique x = a.fg[found];
            for (int i=found;i<a.fg.length-1;i++)
                a.fg[i] = a.fg[i+1];
            a.fg[a.fg.length-1] = x;
            a.paintComponent(a.getGraphics());
        }        
    }
}