Sindbad~EG File Manager
/**
* This class represents a Geometric shape used by Diagrammer.
* This class is inherited by: Arc, Circle, Line, Point, Ray, Segment
* File: GeometricObject.java
*
* This code is in the public domain.
*
* @author Brian Chan
* 12.22.06 Beeson changed deepClone to return GeometricObject
* 12.23.06 Beeson changed value() to setLabelFromString
* 12.23.06 Beeson modified setArgs to handle null
*/
package org.dynamicgeometry.diagrammer;
import java.awt.*;
import java.util.*;
public class GeometricObject implements Cloneable {
protected static final float EPSILON = 3.5f; // tolerance for a mouse click to be "on" this object
protected Color color;
protected boolean visible; // should this object be painted?
protected Label label;
protected int lineThickness;
protected boolean selected; // user selects objects by clicking, this field keeps track of whether this object is selected
protected int isInput; // indicates whether the Object was created by a user or by the Constructor
protected int level; // 1+depth of theStack when this object was created (or after it was returned). Input objects have level 0.
protected String construction; // the Geoscript command used to create this Object
protected ArrayList<GeometricObject> Arguments; // the arguments used to the script used to create this object.
/**
* This is the Constructor for Geometric Objects.
*
*/
public GeometricObject()
{
color = Color.BLACK;
lineThickness = 2;
Arguments = new ArrayList<GeometricObject>();
}
/**
* This method returns the Color of this Object.
*
* @return color
*/
public Color getColor()
{
return color;
}
/**
* This method sets the Color of this Object.
*
* @param color the Color to be set as this Object's color
*/
public void setColor(Color color)
{
this.color = color;
}
/**
* This method returns the thickness of the line used to draw the Object
*
* @return the value of lineThickness
*/
public int getLineThickness()
{
return lineThickness;
}
/**
* Sets the thickness of the line used to draw this Geometric Object.
*
* @param lineThickness the new value of lineThickness
*/
public void setLineThickness(int lineThickness)
{
this.lineThickness = lineThickness;
}
/**
* Determines whether this Object is selected.
* true - this Object is selected
* false - this Object is not selected
*
* @return the value of selected
*/
public boolean isSelected()
{
return selected;
}
/**
* Sets the selected value of this Object.
*
* @param selected the new value of selected
*/
public void setSelected(boolean selected)
{
this.selected = selected;
}
/**
* Determines if this Object is visible.
* true - this Object is visible
* false - this Object is invisible
*
* @return the value of visible
*/
public boolean isVisible()
{
return visible;
}
/**
* Sets the visible field of this Object.
*
* @param visible the new value of visible
*/
public void setVisible(boolean visible)
{
this.visible = visible;
}
/**
* Returns the Label associated with this Geometric Object.
*
* @return the label of this Object
*/
public Label getLabel()
{
return label;
}
/**
* Sets the Label associated with this Geometric Object.
*
* @param label the new label of this Object
*/
public void setLabel(Label label)
{
this.label = label;
}
/**
* Draws this Object using a Graphics2D Object.
*
* @param g used to draw
*/
public void draw(Graphics2D g)
{
// Geometric Object itself has no visual representation
}
/**
* 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)
{
return false;
}
/**
* Adds an argument to the Arguements field of this Object.
*
* @param go the Geometric Object added to Arguments
*/
public void addArg(GeometricObject go)
{
Arguments.add(go);
}
/**
* Removes a specified argument from the Arguments field of this Object.
*
* @param go the Geometric Object being removed from Arguments
* @return true if the Geometric Object was removed successfully, false if not
*/
public boolean removeArg(GeometricObject go)
{
return Arguments.remove(go);
}
/**
* Returns the Arguments of this Geometric Object.
*
* @author Christopher Mathenia
*
* @return Arguments
*/
public ArrayList<GeometricObject> getArgs()
{
return Arguments;
}
/*
* @author Ka Ki Cheung, needed to copy arguments
*/
public void setArgs(ArrayList args) {
if(args == null)
Arguments = null;
else
Arguments = new ArrayList<GeometricObject>(args);
}
/**
* This creates a new Label and automatically sets it as the Label of
* this Geometric Object.
*
* @param s the text displayed as this Geometric Object's Label
*/
public void setLabelFromString(String s)
{
if(s != null)
{
Label lab = new Label();
lab.setVisible(true);
lab.setLabel(s);
lab.setSize(12);
this.label = lab;
}
}
/**
* Sets the value of construction.
*
* @param construction the new value of construction
*/
public void setConstruction(String construction)
{
this.construction = construction;
}
/**
* Returns the value of construction.
*
* @return the value of construction
*/
public String getConstruction()
{
return this.construction;
}
/**
* Returns the value of isInput
*
* @author Christopher Mathenia
*
* @return the value of isInput
*/
public int getIsInput()
{
return isInput;
}
/**
* Sets the value of isInput.
*
* @author Christopher Mathenia
*
* @param n the new value of isInput
*/
public void setIsInput(int n)
{
isInput = n;
}
/**
* Sets the level field of this Geometric Object.
*
* @author Christopher Mathenia
*
* @param level the new value of level
*/
public void setLevel(int level)
{
this.level = level;
}
/**
* Returns the value of the level field.
*
* @return the value of level
*/
public int getLevel()
{
return this.level;
}
/*
* @author Ka Ki Cheung, needed to clone Point
*/
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
/**
* Adding by Thang Dao with Brian's permission
* to allow GeometricObject to create a deep copy instead of shallow copy
*
* comment by Beeson: if we go through an ArrayList<GeometricObject>
* such as theFigure, in which the objects have Arguments occurring earlier
* in the list, and call deepClone on each object, the Arguments will still be
* the original GeometricObjects rather than the cloned ones.
*/
public GeometricObject deepClone() {
try {
GeometricObject copy = (GeometricObject)super.clone();
copy.color = color;
copy.visible = visible;
copy.label = new Label();
copy.label.setLabel( label.toString() );
copy.lineThickness = lineThickness;
copy.isInput = isInput;
copy.level = level;
copy.construction = construction;
copy.Arguments = new ArrayList<GeometricObject>( Arguments );
return copy;
}
catch( CloneNotSupportedException e ) {
throw new Error( "Cloneable: Undetectable behavior - Should not happen" );
}
}
/**
* Checks if a Geometric Object is equal to this Geometric Object.
*
* @param obj the Geoemtric Object to be tested.
* @return true if obj is equal to this Geometric Object, false if not.
*/
public boolean equals(GeometricObject obj)
{
// checks if obj is a valid ClosedPolygon and if the number of Arguments match
if(obj.getClass().equals( this.getClass() ) &&
obj.Arguments.size() == this.Arguments.size())
{
// iterates through all
for(int i = 0; i < this.Arguments.size(); i++)
{
// compares each Point of the Arguments and see if they match
if(!obj.Arguments.get(i).equals( this.Arguments.get(i) ))
return false;
}
return true;
}
return false;
}
}
Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists