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

public class classi extends JPanel
{
	int tempX, tempY;
	Contenitore c;
	int dim = 10;
	public classi()
	{
		String[] nomi = {"salva.jpg","apri.jpg","chiudi.jpg"};
		c = new Contenitore(10,10,300,500, nomi, this);
		
		addMouseListener(new MouseAdapter(){
			public void mousePressed(MouseEvent e){
					c.cliccato(e.getX(), e.getY());
					tempX = e.getX() - c.x;
					tempY = e.getY() - c.y;
					repaint();
				}});
		
		addMouseMotionListener(new MouseMotionAdapter(){
			public void mouseDragged(MouseEvent e){
				c.refreshPosition(e.getX() - tempX, e.getY() - tempY);
				repaint();
				}});
	}
	
	public void paint(Graphics g)
	{
		super.paintComponent(g);
		c.mostra(g);
		g.setColor(Color.black);
		g.fillOval(300,300,dim,dim);
	}
	
	public static void main(String[] a) {JFrame f = new JFrame(); f.setSize(600,600); f.add(new classi()); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);f.setVisible(true);}
}

class Contenitore
{
	int x, y, w, h;
	int tempX, tempY;
	Trascina[] rettangoli;
	boolean clicked = false;
	classi padre;
	
	public Contenitore(int x, int y, int w, int h, String[] nomi, classi p)
	{
		padre = p;
		this.x = x; this.y = y; this.w = w; this.h = h;
		rettangoli = new Trascina[nomi.length];
		for (int k = 0; k < rettangoli.length; k++)
			rettangoli[k] = new Trascina(10,10+150*k,Toolkit.getDefaultToolkit().getImage(nomi[k]), 150,150, this);
	}
	public void mostra(Graphics g)
	{
		g.setColor(Color.blue);
		g.fillRect(x,y,w,h);
		for (int k = 0; k < rettangoli.length; k++)
			rettangoli[k].mostra(g);	
	}
	
	public void cliccato(int cx, int cy)
	{
		boolean almenoUnoCliccato = false;
		for (int k = 0; k < rettangoli.length; k++)
			if (rettangoli[k].cliccato(cx-x, cy-y))
			{
				rettangoli[k].clicked = true;
				almenoUnoCliccato = true;
				tempX = cx - rettangoli[k].x;
				tempY = cy - rettangoli[k].y;
				switch(k)
				{
					case 0: padre.dim++; break;
					case 1: padre.dim--; break;
					case 2: System.exit(0);
				}
			}
			else
			{
				rettangoli[k].clicked = false;
			}
			
		if (!almenoUnoCliccato)
			if (cx > x && cx < x+w && cy > y && cy < y+h)
			{
				clicked = true;
			}
			else
			{
				clicked = false;
			}
	}
	public void refreshPosition(int x, int y)
	{
		if (clicked) // sposta il contenitore e tutti gli items di conseguenza
		{
			this.x = x;
			this.y = y;	
		}
	}	
}

class Trascina
{
	int x, y, w, h;
	Image img;
	boolean clicked = false;
	Contenitore padre;
	
	public Trascina(int x, int y, Image img, int w, int h, Contenitore p)
	{
		padre = p;
		this.x = x;
		this.y = y;
		this.img = img;
		this.w = w;
		this.h = h;
	}
	
	public void mostra(Graphics g)
	{
		g.drawImage(img, padre.x + x, padre.y+y, w, h, null);
	}
	
	public boolean cliccato(int cx, int cy)
	{
		if (cx > x && cx < x+w && cy > y && cy < y+h)
			return true;
		else
			return false;
	}
	public void setPosition(int x, int y)
	{
		this.x = x;
		this.y = y;
	}
}

