001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026: package com.sun.perseus.j2d;
027:
028: import org.w3c.dom.svg.SVGRect;
029:
030: /**
031: * This class represents an "SVGRect" datatype, consisting of a minimum X,
032: * minimum Y, width and height values.
033: *
034: * @author <a href="mailto:kevin.wong@sun.com">Kevin Wong</a>
035: * @version $Id: Box.java,v 1.4 2006/04/21 06:35:00 st125089 Exp $
036: */
037: public class Box implements SVGRect {
038: /**
039: * The smallest x-axis value of the rectangle
040: */
041: public float x;
042:
043: /**
044: * The smallest y-axis value of the rectangle
045: */
046: public float y;
047:
048: /**
049: * The width value of the rectangle
050: */
051: public float width;
052:
053: /**
054: * The height value of the rectangle
055: */
056: public float height;
057:
058: /**
059: * @param x the rect's x-axis origin.
060: * @param y the rect's y-axis origin.
061: * @param w the rect's x-axis width.
062: * @param h the rect's y-axis height.
063: */
064: public Box(final float x, final float y, final float w,
065: final float h) {
066: this .x = x;
067: this .y = y;
068: this .width = w;
069: this .height = h;
070: }
071:
072: /**
073: * Copy constructor
074: *
075: * @param b the Box to copy. Should not be null.
076: */
077: public Box(Box b) {
078: this (b.x, b.y, b.width, b.height);
079: }
080:
081: /**
082: *
083: */
084: public void setX(final float value) {
085: x = value;
086: }
087:
088: /**
089: *
090: */
091: public void setY(final float value) {
092: y = value;
093: }
094:
095: /**
096: *
097: */
098: public void setWidth(final float value) {
099: width = value;
100: }
101:
102: /**
103: *
104: */
105: public void setHeight(final float value) {
106: height = value;
107: }
108:
109: /**
110: *
111: */
112: public float getX() {
113: return x;
114: }
115:
116: /**
117: *
118: */
119: public float getY() {
120: return y;
121: }
122:
123: /**
124: *
125: */
126: public float getWidth() {
127: return width;
128: }
129:
130: /**
131: *
132: */
133: public float getHeight() {
134: return height;
135: }
136:
137: /**
138: * Snaps this Box instance to the integer grid.
139: */
140: public void snap() {
141: float fmaxX = x + width;
142: float fmaxY = y + height;
143:
144: // x and y need to snap to the left of the integer grid.
145: // We cannot simply cast as we need pixel accurate results.
146:
147: // Narrowing rounds towards zero
148: int x = (int) this .x;
149: if (this .x < 0 && (this .x - x != 0)) {
150: x -= 1;
151: }
152:
153: int y = (int) this .y; // Narrowing
154: if (this .y < 0 && (this .y - y) != 0) {
155: y -= 1;
156: }
157:
158: // maxX and maxY need to snap to the right of the integer grid.
159: int maxX = (int) fmaxX;
160: if (fmaxX > 0 && (fmaxX - maxX) != 0) {
161: maxX += 1;
162: }
163:
164: int maxY = (int) fmaxY;
165: if (fmaxY > 0 && (fmaxY - maxY) != 0) {
166: maxY += 1;
167: }
168:
169: this .x = x;
170: this .y = y;
171: this .width = (maxX - x);
172: this .height = (maxY - y);
173: }
174:
175: /**
176: * @return true if the input object is this Box or if x, y, width and height
177: * are equals.
178: */
179: public boolean equals(final Object cmp) {
180: if (cmp == this ) {
181: return true;
182: }
183:
184: if (cmp == null) {
185: return false;
186: }
187:
188: if (cmp instanceof Box) {
189: Box b = (Box) cmp;
190: return b.x == x && b.y == y && b.width == width
191: && b.height == height;
192: }
193:
194: return false;
195: }
196:
197: /**
198: * Debugging helper.
199: */
200: public String toString() {
201: return "Box(" + x + ", " + y + ", " + width + ", " + height
202: + ")";
203: }
204:
205: }
|