/**
 * DragAMac2.java
 * 
 * Allows one to drag a drawing of a Mac around the screen.
 * Demonstrates events and listeners.
 * This version uses the canvas as the listener.
 *
 * @author Scot Drysdale on 1/15/2012
 */
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class DragAMac2 extends JApplet {
	private static final long serialVersionUID = 1L;
	private final int APPLET_WIDTH = 500;
	private final int APPLET_HEIGHT = 500;

	public void init() {
		Container cp = getContentPane(); 	// content pane holds components
		Canvas c = new Canvas();
		cp.add(c);                        // the canvas is the only component
		addMouseListener(c);              // the canvas also is a listener
		addMouseMotionListener(c);

		setSize(APPLET_WIDTH, APPLET_HEIGHT);
		setVisible(true);   // makes the applet (and its components) visible
	}

	/**
	 * Defines the canvas that the drawing is done on.
	 */
	private class Canvas extends JPanel implements MouseListener, MouseMotionListener {
		private static final long serialVersionUID = 1L;

		// Instance variable for the point where the mouse is now.
		private Point dragPoint = null;

		/**
		 * Paints what goes on the canvas - an old-style Mac
		 * @param page the graphic object of the panel for drawing on
		 */
		public void paintComponent(Graphics page) {
			super.paintComponent(page);
			if (dragPoint != null)
				drawAMac(dragPoint, page);
		}

		/**
		 * Sets instance variable dragPoint to wherever the mouse located.
		 * Then redraws the component with that as the upper left corner of the Mac.
		 * @param event the event that caused this callback.
		 */
		public void mousePressed(MouseEvent event) {
			dragPoint = event.getPoint();
			repaint(); // will redraw it at the new dragPoint.
		}

		/**
		 * When we drag the mouse, we should just do the same things as when
		 * it's pressed.
		 * @param event the event that caused this callback.
		 */
		public void mouseDragged(MouseEvent event) {
			mousePressed(event);
		}

		//  Provide empty definitions for unused event methods.
		public void mouseClicked(MouseEvent event) {}
		public void mouseReleased(MouseEvent event) {}
		public void mouseEntered(MouseEvent event) {}
		public void mouseExited(MouseEvent event) {}
		public void mouseMoved(MouseEvent event) {}
	}

	/**
	 * Draws a Mac on the page with p as the upper left corner.
	 * This method is declared private because it is called only by paintComponent.
	 * 
	 * @param p point that is the upper left corner of Mac drawn
	 * @param page the graphics object to draw on
	 */
	private void drawAMac(Point p, Graphics page) {
		page.setColor(Color.blue);
		page.drawRoundRect(p.x, p.y, 160, 210, 10, 10); // draw the Mac outline

		page.drawLine(p.x, p.y + 175, p.x + 160, p.y + 175);

		// Draw floppy drive
		page.setColor(Color.red);
		page.drawLine(p.x + 70, p.y + 147, p.x + 70, p.y + 143);
		page.drawLine(p.x + 70, p.y + 143, p.x + 120, p.y + 143);
		page.drawLine(p.x + 120, p.y + 143, p.x + 120, p.y + 138);
		page.drawLine(p.x + 120, p.y + 138, p.x + 145, p.y + 138);
		page.drawLine(p.x + 145, p.y + 138, p.x + 145, p.y + 152);
		page.drawLine(p.x + 145, p.y + 152, p.x + 120, p.y + 152);
		page.drawLine(p.x + 120, p.y + 152, p.x + 120, p.y + 148);
		page.drawLine(p.x + 120, p.y + 148, p.x + 70, p.y + 148);

		page.drawRect(p.x + 15, p.y + 145, 20, 20); // Apple Logo box

		page.setColor(Color.green);
		page.drawRoundRect(p.x + 20, p.y + 15, 120, 100, 20, 20); // screen outline

		page.setColor(Color.gray);
		page.fillRoundRect(p.x + 30, p.y + 25, 100, 80, 20, 20);
	}
}