/* rsriddle2.java
**
** Author: Eric Laroche
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
**
*/

import java.applet.Applet;
import java.awt.*;
import java.io.PrintStream;

/** rsriddle2.
**
** @author Eric Laroche
** @version @(#)$Id: rsriddle2.java,v 1.1 1999/02/28 18:21:14 laroche Exp $
*/
public class rsriddle2
	extends rsriddle
{
	/** version. */
	private final static String version =
		"Version: @(#)$Id: rsriddle2.java,v 1.1 1999/02/28 18:21:14 laroche Exp $";

	/** applet usage. */
	private final static String[][] usage = {
	};

	/** disclaimer. */
	private final static String disclaimer =
		"This program is distributed in the hope that it will be useful,\nbut WITHOUT ANY WARRANTY; without even the implied warranty of\nMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.";

	/** author. */
	private final static String author =
		"Author: Eric Laroche";

	/** indicator that applet has already been painted. */
	private boolean painted = false;

	/** last mouse position. */
	private Point oldMouse = null;

	/** entry point for standalone application.
	*/
	public static void main(String[] args)
	{
		// usage
		System.err.println("Sorry, you must run this as an applet.");
		System.err.println("");

		// version info, disclaimer, etc.
		printInfo(System.err);
		System.err.println("");

		// base class info
		rsriddle.printInfo(System.err);
	}

	/** print version info, disclaimer, etc.
	*/
	protected static void printInfo(PrintStream out)
	{
		out.println(version);
		out.println(author);
		out.println("");
		out.println(disclaimer);
	}

	/** called by the browser.
	** returns info on this applet.
	*/
	public String getAppletInfo()
	{
		return version + "\n" + author + "\n" +
			super.getAppletInfo();
	}

	/** called by the browser.
	** returns info on the parameters of this applet.
	*/
	public String[][] getParameterInfo()
	{
		return usage;
	}

	/** paint the component.
	*/
	public void paint(Graphics g)
	{
		super.paint(g);

		Point olderMouse = oldMouse;
		oldMouse = null;
		// simulate first mouse move
		if (!painted)
		{
			painted = true;
			if (olderMouse != null)
				mouseMoveOrExit(olderMouse.x, olderMouse.y, false);
		}
	}

	/** handle mouse move event.
	*/
	public boolean mouseMove(Event e, int x, int y)
	{
		return mouseMoveOrExit(x, y, false);
	}

	/** handle mouse exit event.
	*/
	public boolean mouseExit(Event e, int x, int y)
	{
		return mouseMoveOrExit(x, y, true);
	}

	/** handle mouse move or exit events.
	*/
	public boolean mouseMoveOrExit(int x, int y, boolean exited)
	{
		if (oldMouse != null)
			// don't reverse before the thing has been painted
			// though save the coordinate for use after paint()
			if (painted)
				inverse(oldMouse);

		oldMouse = null;

		if (!exited)
			oldMouse = new Point(x, y);

		if (oldMouse != null)
			if (painted)
				inverse(oldMouse);

		return true;
	}

	/** inverse a vertical line in the upper half part.
	*/
	private void inverse(Point p)
	{
		Graphics g = getGraphics();
		// set inverse mode
		g.setXORMode(Color.white);
		// upper half
		g.drawLine(p.x, 0, p.x, size().height / 2);
		g.dispose();
	}

	/** handle mouse down event.
	*/
	public boolean mouseDown(Event e, int x, int y)
	{
		// get drawing area information
		// note that this duplicates some of the base class code
		Rectangle upper = bounds();
		upper.height /= 2;

		try
		{
			addBorder(upper);
		}
		catch (IllegalArgumentException ex)
		{
			return true;
		}

		Dimension d = new Dimension(upper.width - upper.x, upper.height - upper.y);
		Point o = new Point(upper.x, upper.y);

		int xu = 0;
		if (transformX(d, o, xu) > x)
			return true;
		// todo: use something more reasonable here, while still working with integers
		while (transformX(d, o, xu) < x)
			xu++;

		int w = d.width;

		try
		{
			// xu is 3*a1+2*a2, width is 5*a1+3*a2, a2 is ratio*a1, so
			setRatio(5*xu-3*w, 2*w-3*xu);
			// note that this works only on a very small interval
		}
		catch (IllegalArgumentException ex)
		{
			// ignore
		}

		return true;
	}
}
