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

public class test4 extends JPanel
{
	int x = 0, y = 0;
	Palla palletta;
	Color[] colori;
	
	public test4()
	{
		palletta = new Palla(50,50,30);
		colori = new Color[255];
		for (int k = 0; k < 255; k++)
			colori[k] = new Color((int)(Math.random()*255),(int)(Math.random()*255),(int)(Math.random()*255));
			
		addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e){
			x = e.getX() - palletta.x;
			y = e.getY() - palletta.y;
			palletta.clicked = palletta.isClicked(e.getX(), e.getY());
			}});
		addMouseMotionListener(new MouseMotionAdapter() { public void mouseDragged(MouseEvent e){
			if (palletta.clicked)
				palletta.moveTo(e.getX() - x, e.getY() - y);
			}});
	}
	
	public void paint(Graphics g)
	{
		super.paintComponent(g);
		for (int k = 0; k < 255; k++)
		{
			g.setColor(colori[k]);
			g.drawLine(k,0,k,255);
		}
		palletta.color = colori[palletta.x];
		palletta.mostra(g);
		repaint();
	}
	
	public static void main(String[] a)
	{
		JFrame f = new JFrame();
		f.setTitle("Test 4");
		f.setSize(255,255);
		f.add(new test4());
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.setVisible(true);
	}
}

class Palla
{
	int x, y, dim;
	Color color;
	boolean clicked = false;
	public Palla(int x, int y, int dim)
	{
		this.x = x;
		this.y = y;
		this.dim = dim;
		this.color = new Color((int)(Math.random()*255),(int)(Math.random()*255),(int)(Math.random()*255));
	}
	public void mostra(Graphics g)
	{
		g.setColor(Color.black);
		g.fillOval(x-dim/2, y-dim/2, dim, dim);
		g.setColor(color);
		g.fillOval(x-(dim-6)/2, y-(dim-6)/2, dim-6, dim-6);
	}
	public void moveTo(int x, int y)
	{
		this.x = x;
		this.y = y;
	}
	public boolean isClicked(int cx, int cy)
	{
		if (cx > x-dim/2 && cx < x+dim/2 && cy > y-dim/2 && cy < y+dim/2)
			return true;
		else
			return false;
	}
}
