import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

class MyDrawPanel extends JPanel {

	public void paintComponent(Graphics g) {

		Color backgroundColor, circleColor;
		
		// clear background
		
		backgroundColor = new Color(0, 0, 0);
		g.setColor(backgroundColor);
		g.fillRect(0, 0, this.getWidth(), this.getHeight());

		if (MouseDrag.grabbed)
			circleColor = new Color(0, 255, 0);
		else
			circleColor = new Color(255, 0, 0);

		g.setColor(circleColor);
		
		// draw circle

		
		g.fillOval(MouseDrag.circle_x-MouseDrag.circle_radius,MouseDrag.circle_y-MouseDrag.circle_radius, 
					     2*MouseDrag.circle_radius, 2*MouseDrag.circle_radius);
	}

}

public class MouseDrag { // implements ActionListener {

	JFrame frame;
	JLabel label;
	JButton colorButton;
	JButton labelButton;
	
	
	boolean ouch = false;
	static int circle_radius = 20;
	static int circle_x = 50;
	static int circle_y = 50;
	static boolean grabbed = false;
	static int grabbed_dx;
	static int grabbed_dy;
	int window_width = 300;
	int window_height = 300;
	
	public static void main (String[] args) {
		MouseDrag gui = new MouseDrag();
		gui.go();
	}


	public void go() {
		
		frame = new JFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


		MyDrawPanel drawPanel = new MyDrawPanel();
		
		CircleMouseListener circleMListener = new CircleMouseListener();
		CircleMouseMotionListener circleMMListener = new CircleMouseMotionListener();

		drawPanel.addMouseListener(circleMListener);
		drawPanel.addMouseMotionListener(circleMMListener);
		
		frame.getContentPane().add(BorderLayout.CENTER, drawPanel);
		

		frame.setSize(window_width, window_height);  
		frame.setVisible(true);
	}
	
	
	
	
	
	
	class CircleMouseMotionListener implements MouseMotionListener {

		public void mouseDragged(MouseEvent event) {
//			System.out.println("dragged " + event.getPoint().x + " " + event.getPoint().y);
			if (grabbed) {
				
			
				int cx = event.getPoint().x - grabbed_dx;
				int cy = event.getPoint().y - grabbed_dy;
				
				//System.out.println("cx = " + cx + " cy = " + cy);
				
				// don't allow circle to be dragged outside of window
				// this is slightly wrong because of title bar height, frame width...
				
				if (cx >= 0 && cx < window_width && cy >= 0 && cy < window_height) {
					circle_x = cx;
					circle_y = cy;
					frame.repaint();
				}
				
			}
		}

		public void mouseMoved(MouseEvent event) {
//			System.out.println("moved");
		}
	}

	
	class CircleMouseListener implements MouseListener {
			
		public void mouseClicked(MouseEvent event) {
//			System.out.println("clicked");
		}
		
		public void mouseExited(MouseEvent event) {
//			System.out.println("exited");
		}

		public void mousePressed(MouseEvent event) {
//			System.out.println("pressed " + event.getPoint().x + " " + event.getPoint().y);
			
			double x = event.getPoint().x;
			double y = event.getPoint().y;
			double cx = circle_x;
			double cy = circle_y;
			double r = circle_radius;
			
		// is click inside circle?
			
			if (Math.sqrt((x - cx)*(x - cx) + (y - cy)*(y - cy)) <= r) {
//				System.out.println("got it!");
				grabbed = true;
				grabbed_dx = event.getPoint().x - circle_x;
				grabbed_dy = event.getPoint().y - circle_y;
				frame.repaint();
			}
			
		}

		public void mouseReleased(MouseEvent event) {
//			System.out.println("released");
			grabbed = false;
			frame.repaint();
		}

		public void mouseEntered(MouseEvent event) {
//			System.out.println("entered");
		}
		
	} 

}

