001: /* ====================================================================
002: * The JRefactory License, Version 1.0
003: *
004: * Copyright (c) 2001 JRefactory. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * 3. The end-user documentation included with the redistribution,
019: * if any, must include the following acknowledgment:
020: * "This product includes software developed by the
021: * JRefactory (http://www.sourceforge.org/projects/jrefactory)."
022: * Alternately, this acknowledgment may appear in the software itself,
023: * if and wherever such third-party acknowledgments normally appear.
024: *
025: * 4. The names "JRefactory" must not be used to endorse or promote
026: * products derived from this software without prior written
027: * permission. For written permission, please contact seguin@acm.org.
028: *
029: * 5. Products derived from this software may not be called "JRefactory",
030: * nor may "JRefactory" appear in their name, without prior written
031: * permission of Chris Seguin.
032: *
033: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
034: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
035: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
036: * DISCLAIMED. IN NO EVENT SHALL THE CHRIS SEGUIN OR
037: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
038: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
039: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
040: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
041: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
042: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
043: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
044: * SUCH DAMAGE.
045: * ====================================================================
046: *
047: * This software consists of voluntary contributions made by many
048: * individuals on behalf of JRefactory. For more information on
049: * JRefactory, please see
050: * <http://www.sourceforge.org/projects/jrefactory>.
051: */
052: package org.acm.seguin.uml.line;
053:
054: import java.awt.Color;
055: import java.awt.Point;
056: import java.awt.Graphics;
057: import java.io.PrintWriter;
058:
059: /**
060: * Vertex
061: *
062: *@author Chris Seguin
063: *@author Mike Atkinson
064: *@created July 28, 1999
065: */
066: public class Vertex {
067: private Point point;
068: private boolean selected;
069: private boolean activated;
070: private boolean rescaled;
071: private double scale;
072: private Point computed;
073: private static double near = 3.0;
074: private static int vertexSizeHalf = 2;
075: private static int vertexSize = 5;
076:
077: /**
078: * Constructor for the Vertex object
079: *
080: *@param init Description of Parameter
081: */
082: public Vertex(Point init) {
083: point = init;
084: computed = new Point();
085: activated = false;
086: selected = false;
087: rescaled = true;
088: scale = 1.0;
089: }
090:
091: /**
092: * Checks if the point is selected
093: *
094: *@return true if the point is selected
095: */
096: public boolean isSelected() {
097: return selected;
098: }
099:
100: /**
101: * Checks if this vertex is the active vertex
102: *
103: *@return true if it is
104: */
105: public boolean isActive() {
106: return activated;
107: }
108:
109: /**
110: * Paints the object
111: *
112: *@param g The graphics context
113: */
114: public void paint(Graphics g) {
115: if (activated) {
116: g.setColor(Color.magenta);
117: g.fillOval((int) (getX() - Vertex.vertexSizeHalf),
118: (int) (getY() - Vertex.vertexSizeHalf),
119: Vertex.vertexSize, Vertex.vertexSize);
120: } else if (selected) {
121: g.setColor(Color.black);
122: g.fillOval((int) (getX() - Vertex.vertexSizeHalf),
123: (int) (getY() - Vertex.vertexSizeHalf),
124: Vertex.vertexSize, Vertex.vertexSize);
125: } else {
126: // Don't paint it
127: //g.setColor(Color.black);
128: //g.drawOval((int) point.getX(), (int) point.getY(), 1, 1);
129: }
130: }
131:
132: /**
133: * Determines if it is hit by a point
134: *
135: *@param p The point
136: *@return true if this point hits this vertex
137: */
138: public boolean hit(Point p) {
139: double diffX = getX() - p.getX();
140: double diffY = getY() - p.getY();
141:
142: double dist = Math.sqrt(diffX * diffX + diffY * diffY);
143:
144: return (dist < Vertex.near);
145: }
146:
147: /**
148: * Moves the vertex to p
149: *
150: *@param p the destination of the move
151: */
152: public void move(Point p) {
153: point.x = (int) (p.x / scale);
154: point.y = (int) (p.y / scale);
155: rescaled = true;
156: }
157:
158: /**
159: * Save the vertex
160: *
161: *@param output the output stream
162: */
163: public void save(PrintWriter output) {
164: output.print("(" + point.x + "," + point.y + ")");
165: }
166:
167: /**
168: * Shifts the point by a certain amount
169: *
170: *@param x the amount in the x coordinate
171: *@param y the amount in the y coordinate
172: */
173: public void shift(int x, int y) {
174: point.x += unscaleInteger(x);
175: point.y += unscaleInteger(y);
176: rescaled = true;
177: }
178:
179: /**
180: * Inverse of the scaleInteger operation
181: *
182: *@param value the input value
183: *@return the result of the unscape operation
184: */
185: private int unscaleInteger(int value) {
186: return (int) (value / scale);
187: }
188:
189: /**
190: * Scales the vertex
191: *
192: *@param value the scaling factor
193: */
194: public void scale(double value) {
195: if (Math.abs(value - scale) > 0.001) {
196: rescaled = true;
197: scale = value;
198: }
199: }
200:
201: /**
202: * Gets the X attribute of the Vertex object
203: *
204: *@return The X value
205: */
206: protected int getX() {
207: if (rescaled) {
208: computed.x = (int) (point.x * scale);
209: computed.y = (int) (point.y * scale);
210: rescaled = false;
211: }
212: return computed.x;
213: }
214:
215: /**
216: * Gets the Y attribute of the Vertex object
217: *
218: *@return The Y value
219: */
220: protected int getY() {
221: if (rescaled) {
222: computed.x = (int) (point.x * scale);
223: computed.y = (int) (point.y * scale);
224: rescaled = false;
225: }
226: return computed.y;
227: }
228:
229: /**
230: * Gets the scaled point
231: *
232: *@return the scaled point
233: */
234: protected Point getPoint() {
235: if (rescaled) {
236: computed.x = (int) (point.x * scale);
237: computed.y = (int) (point.y * scale);
238: rescaled = false;
239: }
240: return computed;
241: }
242:
243: /**
244: * Selects the point
245: *
246: *@param way true if the point is selected
247: */
248: protected void select(boolean way) {
249: selected = way;
250: if (!selected) {
251: activated = false;
252: }
253: }
254:
255: /**
256: * Sets whether this is the active vertex
257: *
258: *@param way true if this vertex is active
259: */
260: protected void active(boolean way) {
261: activated = way;
262: if (activated) {
263: selected = true;
264: }
265: }
266:
267: /**
268: * Sets the Near attribute of the Vertex class
269: *
270: *@param value The new Near value
271: */
272: public static void setNear(double value) {
273: near = value;
274: }
275:
276: /**
277: * Sets the VertexSize attribute of the Vertex class
278: *
279: *@param value The new VertexSize value
280: */
281: public static void setVertexSize(int value) {
282: vertexSizeHalf = value / 2;
283: vertexSize = value;
284: }
285: }
|