// MouseApplicationInterface.java GUI Application for move a rectangle 
//  around a panel via mouse clicks 
//  Employs direct use of the Mouse Interface

// Written for ICS 45J by Norman Jacobson, August 2012
// Adapted from MouseApplicationInterface.java and MouseApllet0.java
//  from ICS 21 3/09 versions


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class MouseApplicationInterface
{
	public static void main(String[] args)
	{
		// Make a panel with the rectangle in it
		RectanglePanel rectPanel = new RectanglePanel();

		// Make a frame that
		// ...closes on exit
		// ...has rectPanel as its content pane (pane that holds all the UI components)
		// ...pack it so the frame is just the right size to hold the rectangle panel
		// Then display the pane
		JFrame appFrame = new JFrame();
		appFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		appFrame.setContentPane(rectPanel);
		appFrame.pack();
		appFrame.setVisible(true);
	}

}

// Make and paint a panel that contains a rectangle
class RectanglePanel extends JPanel
{
	private Rectangle box;

	public RectanglePanel()
	{
		// When the mouse is pressed, updated the
		// recangle's location and redisplay on screen;
		// rest of mouse events initiate no action
		class MouseHandler implements MouseListener
		{
			public void mousePressed(MouseEvent event)
			{
				int x = event.getX();
				int y = event.getY();
				box.setLocation(x,y);
				repaint();
			}
			public void mouseReleased(MouseEvent event) {}
			public void mouseClicked(MouseEvent event) {}
			public void mouseEntered(MouseEvent event) {}
			public void mouseExited(MouseEvent event) {}
		}

		addMouseListener(new MouseHandler());
		setPreferredSize(new Dimension(300,300));
		box = new Rectangle(100,100,20,30);
	}

	// paints the panel (called via repaint())
	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);		
		Graphics2D g2 = (Graphics2D)g;
		g2.draw(box);
	}
}