Sindbad~EG File Manager

Current Path : /usr/home/beeson/public_html/dynamicgeometry/org/dynamicgeometry/diagrammer/
Upload File :
Current File : /usr/home/beeson/public_html/dynamicgeometry/org/dynamicgeometry/diagrammer/Arc.java

/**
 * This is a Geometric Object representing an Arc.
 * File: Arc.java
 *
 * This code is in the public domain.
 *
 * @author Brian Chan
 */
// 12.24.06  Beeson changed color to GREEN

package org.dynamicgeometry.diagrammer;

import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.font.TextLayout;
import java.awt.geom.Point2D;

public class Arc extends Circle {

	// The start and end angle in degrees of where to draw a circle
	//  0 is in the three o'clock direction increasing counter-clockwise
	protected int start_angle,
	              end_angle;

	/**
	 * A constructor for an Arc
	 *
	 */
	public Arc()
	{
		// calls the constructor of Circle
		super();
	}

	/**
	 * A constructor for an Arc.
	 * The Points being passed are used to calculate the start and end angles,
	 * relative to the center of the Circle.
	 *
	 * @param C the Circle on which the Arc will lie
	 * @param A the Point used to calculate the start angle
	 * @param B the Point used to calculate the end angle
	 */
	public Arc(Circle C, Point A, Point B)
	{
		this();

		// checks if the parameters are valid
		if(C != null && A != null && B != null && C.Arguments.size() > 1)
		{
			// Adds four points as the Arguments of an Arc
			this.Arguments.add( C.Arguments.get(0));
			this.Arguments.add( C.Arguments.get(1));
			this.Arguments.add( A );
			this.Arguments.add( B );
		}
	}
	
	/**
	 * A constructor for an Arc.
	 * The Points being passed are used to calculate a circle and the
	 * relative start and end angles of the Arc.
	 * 
	 * @param A the Center of the circle where the Arc will lie
	 * @param B a Point used to calculate the radius of the circle
	 * @param C the Point used to calculate the start angle
	 * @param D the Point used to calculate the end angle
	 */
	public Arc(Point A, Point B, Point C, Point D)
	{
		this();
		
		// check if the parameters are valid
		if(A != null && B != null && C != null && D != null)
		{
			// Adds four points as the Arguments of an Arc
			this.Arguments.add( A );
			this.Arguments.add( B );
			this.Arguments.add( C );
			this.Arguments.add( D );
		}
	}

	/**
	 * Draws an Arc using the Graphics2D parameter.
	 *
	 * @param g the Object used to draw an Arc
	 */
	public void draw(Graphics2D g)
	{
		Point center,
              end,
              A,
              B;

		// Checks if Arguments contains the proper values to draw an Arc
		//  draw if visible
		if(this.visible &&
		   this.Arguments.size() == 4 &&
		   (center = (Point)Arguments.get(0)) instanceof Point &&
		   (end = (Point)Arguments.get(1)) instanceof Point &&
		   (A = (Point)Arguments.get(2)) instanceof Point &&
		   (B = (Point)Arguments.get(3)) instanceof Point)
		{
			double radius = Math.sqrt( (center.x - end.x) * (center.x - end.x) +
					                   (center.y - end.y) * (center.y - end.y));
			this.start_angle = calcAngle(center, A);
			this.end_angle = calcAngle(center, B);

			if(this.start_angle > this.end_angle)
				this.end_angle = 360 - this.start_angle + this.end_angle;
			else
				this.end_angle = this.end_angle - this.start_angle;

			// if this Object is input
			if(this.isInput != 0)
			{
				// if this Object is selected and is not input
				if(this.selected)
					g.setColor(Color.GREEN);

				// if this Object is not selected and is not input
				else
					g.setColor(Color.BLUE);
			}

			// if this Object is not input
			else
			{
				// if this Object is selected and is input
				if(this.selected)
					g.setColor(Color.RED);

				// if this Object is not selected but is input
				else
					g.setColor(Color.BLACK);
			}

			// set the line thickness
			g.setStroke( new BasicStroke(lineThickness) );

			g.drawArc((int)(center.x - radius),
					  (int)(center.y - radius),
					  (int)(radius * 2),
					  (int)(radius * 2),
					  start_angle,
					  end_angle);

			// Draws the Label of this Arc if it has one set
			if(this.label != null && this.label.getVisible())
			{
				// Draws the label in the middle of the Arc
				double average = start_angle + ((end_angle < 0)?(end_angle / -2):(end_angle / 2));
				average = average / 360 * 2 * Math.PI;

				FontRenderContext frc = g.getFontRenderContext();
				TextLayout layout = new TextLayout(this.label.toString(),
						                           this.label.getFont(),
                                                   frc);
				this.label.setLocation(
							new Point2D.Double(
								center.x + Math.cos(average) * (radius - 2 * EPSILON),
						        center.y + Math.sin(average) * (radius - 2 * EPSILON) * -1.0));
				layout.draw(g,
						    (float)this.label.getLocation().getX(),
						    (float)this.label.getLocation().getY() );
			}
		}
	}

	/**
	 * Sets the end angle of the Arc.
	 *
	 * @deprecated a Point should be added to index 3 of Arguments
	 * @param end_angle the new value of end_angle
	 */
	public void setEndAngle(int end_angle)
	{
		this.end_angle = end_angle;
	}

	/**
	 * Sets the start angle of the Arc.
	 *
	 * @deprecated a Point should be added to index 2 of Arguments
	 * @param start_angle the new value of start_angle
	 */
	public void setStartAngle(int start_angle)
	{
		this.start_angle = start_angle;
	}

	/**
	 * Determines if a mouse click is "close" enough to constitute a mouse
	 * click for this Object.
	 *
	 * @param x the x coordinate of the mouse click
	 * @param y the y coordinate of this mouse click
	 * @return true if this Object was clicked, false if this Object was not clicked
	 */
	public boolean clickedOn(double x, double y)
	{
		Point center,
		      end;
		double tempAngle,
		       distance;

        // Checks if Arguments contains the proper values to draw an Arc
		if(this.visible &&
				   this.Arguments.size() == 4 &&
				   (center = (Point)Arguments.get(0)) instanceof Point &&
				   (end = (Point)Arguments.get(1)) instanceof Point)
				{
					double radius = Math.sqrt( (center.x - end.x) * (center.x - end.x) +
							                   (center.y - end.y) * (center.y - end.y));

			distance = Math.sqrt( (center.x - x) * (center.x - x) +
                    (center.y - y) * (center.y - y) );

			tempAngle = calcAngle(center, new Point(x, y));

			// accepts only if the tempangle lies between start_angle and end_angle
			if(start_angle < end_angle)
			{
				return tempAngle >= start_angle &&
		           tempAngle <= end_angle &&
		           Math.abs(distance - radius) < (EPSILON + this.lineThickness);
			}
			else
			{
				return (tempAngle) >= start_angle &&
			           (tempAngle) <= (end_angle + 360)&&
			           Math.abs(distance - radius) < (EPSILON + this.lineThickness);
			}
		}
		else
			return false;
	}

	/**
	 * Calculates the angle Point t forms, relative to Point c.
	 *
	 * @param c the Point used as the frame of reference
	 * @param t the Point that is used to calculate the angle
	 * @return the angle formed in degrees (0 - 359)
	 */
	private int calcAngle(Point c, Point t)
	{
		double result = Math.atan( Math.abs(c.y - t.y) / Math.abs(c.x - t.x) );
		result = result / (2 * Math.PI) * 360;

		// Quadrant 3
		if(t.x < c.x && t.y < c.y)
			result = 180 - result;

		// Quadrant 2
		else if(t.x < c.x)
			result += 180;

		// Quadrant 4
		else if(t.y < c.y)
			;

		// Quadrant 1
		else
			result = 360 - result;

		return (int)result;
	}
}

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists