1:
48:
49: package ;
50:
51: import ;
52: import ;
53:
54: import ;
55:
56:
62: public class RectangleInsets implements Serializable {
63:
64:
65: private static final long serialVersionUID = 1902273207559319996L;
66:
67:
70: public static final RectangleInsets ZERO_INSETS = new RectangleInsets(
71: UnitType.ABSOLUTE, 0.0, 0.0, 0.0, 0.0);
72:
73:
74: private UnitType unitType;
75:
76:
77: private double top;
78:
79:
80: private double left;
81:
82:
83: private double bottom;
84:
85:
86: private double right;
87:
88:
93: public RectangleInsets() {
94: this(1.0, 1.0, 1.0, 1.0);
95: }
96:
97:
105: public RectangleInsets(final double top, final double left,
106: final double bottom, final double right) {
107: this(UnitType.ABSOLUTE, top, left, bottom, right);
108: }
109:
110:
120: public RectangleInsets(final UnitType unitType,
121: final double top, final double left,
122: final double bottom, final double right) {
123: if (unitType == null) {
124: throw new IllegalArgumentException("Null 'unitType' argument.");
125: }
126: this.unitType = unitType;
127: this.top = top;
128: this.bottom = bottom;
129: this.left = left;
130: this.right = right;
131: }
132:
133:
139: public UnitType getUnitType() {
140: return this.unitType;
141: }
142:
143:
148: public double getTop() {
149: return this.top;
150: }
151:
152:
157: public double getBottom() {
158: return this.bottom;
159: }
160:
161:
166: public double getLeft() {
167: return this.left;
168: }
169:
170:
175: public double getRight() {
176: return this.right;
177: }
178:
179:
186: public boolean equals(final Object obj) {
187: if (obj == this) {
188: return true;
189: }
190: if (!(obj instanceof RectangleInsets)) {
191: return false;
192: }
193: final RectangleInsets that = (RectangleInsets) obj;
194: if (that.unitType != this.unitType) {
195: return false;
196: }
197: if (this.left != that.left) {
198: return false;
199: }
200: if (this.right != that.right) {
201: return false;
202: }
203: if (this.top != that.top) {
204: return false;
205: }
206: if (this.bottom != that.bottom) {
207: return false;
208: }
209: return true;
210: }
211:
212:
217: public int hashCode() {
218: int result;
219: long temp;
220: result = (this.unitType != null ? this.unitType.hashCode() : 0);
221: temp = this.top != +0.0d ? Double.doubleToLongBits(this.top) : 0L;
222: result = 29 * result + (int) (temp ^ (temp >>> 32));
223: temp = this.bottom != +0.0d ? Double.doubleToLongBits(this.bottom) : 0L;
224: result = 29 * result + (int) (temp ^ (temp >>> 32));
225: temp = this.left != +0.0d ? Double.doubleToLongBits(this.left) : 0L;
226: result = 29 * result + (int) (temp ^ (temp >>> 32));
227: temp = this.right != +0.0d ? Double.doubleToLongBits(this.right) : 0L;
228: result = 29 * result + (int) (temp ^ (temp >>> 32));
229: return result;
230: }
231:
232:
238: public String toString() {
239: return "RectangleInsets[t=" + this.top + ",l=" + this.left
240: + ",b=" + this.bottom + ",r=" + this.right + "]";
241: }
242:
243:
256: public Rectangle2D createAdjustedRectangle(final Rectangle2D base,
257: final LengthAdjustmentType horizontal,
258: final LengthAdjustmentType vertical) {
259: if (base == null) {
260: throw new IllegalArgumentException("Null 'base' argument.");
261: }
262: double x = base.getX();
263: double y = base.getY();
264: double w = base.getWidth();
265: double h = base.getHeight();
266: if (horizontal == LengthAdjustmentType.EXPAND) {
267: final double leftOutset = calculateLeftOutset(w);
268: x = x - leftOutset;
269: w = w + leftOutset + calculateRightOutset(w);
270: }
271: else if (horizontal == LengthAdjustmentType.CONTRACT) {
272: final double leftMargin = calculateLeftInset(w);
273: x = x + leftMargin;
274: w = w - leftMargin - calculateRightInset(w);
275: }
276: if (vertical == LengthAdjustmentType.EXPAND) {
277: final double topMargin = calculateTopOutset(h);
278: y = y - topMargin;
279: h = h + topMargin + calculateBottomOutset(h);
280: }
281: else if (vertical == LengthAdjustmentType.CONTRACT) {
282: final double topMargin = calculateTopInset(h);
283: y = y + topMargin;
284: h = h - topMargin - calculateBottomInset(h);
285: }
286: return new Rectangle2D.Double(x, y, w, h);
287: }
288:
289:
296: public Rectangle2D createInsetRectangle(final Rectangle2D base) {
297: return createInsetRectangle(base, true, true);
298: }
299:
300:
309: public Rectangle2D createInsetRectangle(final Rectangle2D base,
310: final boolean horizontal,
311: final boolean vertical) {
312: if (base == null) {
313: throw new IllegalArgumentException("Null 'base' argument.");
314: }
315: double topMargin = 0.0;
316: double bottomMargin = 0.0;
317: if (vertical) {
318: topMargin = calculateTopInset(base.getHeight());
319: bottomMargin = calculateBottomInset(base.getHeight());
320: }
321: double leftMargin = 0.0;
322: double rightMargin = 0.0;
323: if (horizontal) {
324: leftMargin = calculateLeftInset(base.getWidth());
325: rightMargin = calculateRightInset(base.getWidth());
326: }
327: return new Rectangle2D.Double(
328: base.getX() + leftMargin,
329: base.getY() + topMargin,
330: base.getWidth() - leftMargin - rightMargin,
331: base.getHeight() - topMargin - bottomMargin
332: );
333: }
334:
335:
342: public Rectangle2D createOutsetRectangle(final Rectangle2D base) {
343: return createOutsetRectangle(base, true, true);
344: }
345:
346:
355: public Rectangle2D createOutsetRectangle(final Rectangle2D base,
356: final boolean horizontal,
357: final boolean vertical) {
358: if (base == null) {
359: throw new IllegalArgumentException("Null 'base' argument.");
360: }
361: double topMargin = 0.0;
362: double bottomMargin = 0.0;
363: if (vertical) {
364: topMargin = calculateTopOutset(base.getHeight());
365: bottomMargin = calculateBottomOutset(base.getHeight());
366: }
367: double leftMargin = 0.0;
368: double rightMargin = 0.0;
369: if (horizontal) {
370: leftMargin = calculateLeftOutset(base.getWidth());
371: rightMargin = calculateRightOutset(base.getWidth());
372: }
373: return new Rectangle2D.Double(
374: base.getX() - leftMargin,
375: base.getY() - topMargin,
376: base.getWidth() + leftMargin + rightMargin,
377: base.getHeight() + topMargin + bottomMargin
378: );
379: }
380:
381:
388: public double calculateTopInset(final double height) {
389: double result = this.top;
390: if (this.unitType == UnitType.RELATIVE) {
391: result = (this.top * height);
392: }
393: return result;
394: }
395:
396:
403: public double calculateTopOutset(final double height) {
404: double result = this.top;
405: if (this.unitType == UnitType.RELATIVE) {
406: result = (height / (1 - this.top - this.bottom)) * this.top;
407: }
408: return result;
409: }
410:
411:
418: public double calculateBottomInset(final double height) {
419: double result = this.bottom;
420: if (this.unitType == UnitType.RELATIVE) {
421: result = (this.bottom * height);
422: }
423: return result;
424: }
425:
426:
433: public double calculateBottomOutset(final double height) {
434: double result = this.bottom;
435: if (this.unitType == UnitType.RELATIVE) {
436: result = (height / (1 - this.top - this.bottom)) * this.bottom;
437: }
438: return result;
439: }
440:
441:
448: public double calculateLeftInset(final double width) {
449: double result = this.left;
450: if (this.unitType == UnitType.RELATIVE) {
451: result = (this.left * width);
452: }
453: return result;
454: }
455:
456:
463: public double calculateLeftOutset(final double width) {
464: double result = this.left;
465: if (this.unitType == UnitType.RELATIVE) {
466: result = (width / (1 - this.left - this.right)) * this.left;
467: }
468: return result;
469: }
470:
471:
478: public double calculateRightInset(final double width) {
479: double result = this.right;
480: if (this.unitType == UnitType.RELATIVE) {
481: result = (this.right * width);
482: }
483: return result;
484: }
485:
486:
493: public double calculateRightOutset(final double width) {
494: double result = this.right;
495: if (this.unitType == UnitType.RELATIVE) {
496: result = (width / (1 - this.left - this.right)) * this.right;
497: }
498: return result;
499: }
500:
501:
508: public double trimWidth(final double width) {
509: return width - calculateLeftInset(width) - calculateRightInset(width);
510: }
511:
512:
519: public double extendWidth(final double width) {
520: return width + calculateLeftOutset(width) + calculateRightOutset(width);
521: }
522:
523:
530: public double trimHeight(final double height) {
531: return height
532: - calculateTopInset(height) - calculateBottomInset(height);
533: }
534:
535:
542: public double extendHeight(final double height) {
543: return height
544: + calculateTopOutset(height) + calculateBottomOutset(height);
545: }
546:
547:
552: public void trim(final Rectangle2D area) {
553: final double w = area.getWidth();
554: final double h = area.getHeight();
555: final double l = calculateLeftInset(w);
556: final double r = calculateRightInset(w);
557: final double t = calculateTopInset(h);
558: final double b = calculateBottomInset(h);
559: area.setRect(area.getX() + l, area.getY() + t, w - l - r, h - t - b);
560: }
561:
562: }