001: /*
002: * Copyright (C) 2004 NNL Technology AB
003: * Visit www.infonode.net for information about InfoNode(R)
004: * products and how to contact NNL Technology AB.
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
019: * MA 02111-1307, USA.
020: */
021:
022: // $Id: TranslatingShape.java,v 1.4 2005/02/16 11:28:13 jesper Exp $
023: package net.infonode.gui;
024:
025: import java.awt.*;
026: import java.awt.geom.AffineTransform;
027: import java.awt.geom.PathIterator;
028: import java.awt.geom.Point2D;
029: import java.awt.geom.Rectangle2D;
030:
031: /**
032: * @author $Author: jesper $
033: * @version $Revision: 1.4 $
034: */
035: public class TranslatingShape implements Shape {
036: private Shape shape;
037: private double dx;
038: private double dy;
039:
040: public TranslatingShape(Shape shape, double dx, double dy) {
041: this .shape = shape;
042: this .dx = dx;
043: this .dy = dy;
044: }
045:
046: public Rectangle getBounds() {
047: Rectangle r = shape.getBounds();
048: r.translate((int) dx, (int) dy);
049: return r;
050: }
051:
052: public Rectangle2D getBounds2D() {
053: Rectangle2D r = shape.getBounds2D();
054: r.setRect(r.getMinX() + dx, r.getMinY() + dy, r.getWidth(), r
055: .getHeight());
056: return r;
057: }
058:
059: public boolean contains(double x, double y) {
060: return shape.contains(x - dx, y - dy);
061: }
062:
063: public boolean contains(Point2D p) {
064: return contains(p.getX(), p.getY());
065: }
066:
067: public boolean intersects(double x, double y, double w, double h) {
068: return shape.intersects(x - dx, y - dy, w, h);
069: }
070:
071: public boolean intersects(Rectangle2D r) {
072: return intersects(r.getMinX(), r.getMinY(), r.getWidth(), r
073: .getHeight());
074: }
075:
076: public boolean contains(double x, double y, double w, double h) {
077: return shape.contains(x - dx, y - dy, w, h);
078: }
079:
080: public boolean contains(Rectangle2D r) {
081: return contains(r.getMinX() - dx, r.getMinY() - dy, r
082: .getWidth(), r.getHeight());
083: }
084:
085: public PathIterator getPathIterator(AffineTransform at) {
086: return new Iterator(shape.getPathIterator(at));
087: }
088:
089: public PathIterator getPathIterator(AffineTransform at,
090: double flatness) {
091: return new Iterator(shape.getPathIterator(at, flatness));
092: }
093:
094: private class Iterator implements PathIterator {
095: private PathIterator iterator;
096:
097: Iterator(PathIterator iterator) {
098: this .iterator = iterator;
099: }
100:
101: public int getWindingRule() {
102: return iterator.getWindingRule();
103: }
104:
105: public boolean isDone() {
106: return iterator.isDone();
107: }
108:
109: public void next() {
110: iterator.next();
111: }
112:
113: public int currentSegment(float[] coords) {
114: int result = iterator.currentSegment(coords);
115:
116: for (int i = 0; i < coords.length; i++) {
117: coords[i++] += dx;
118: coords[i] += dy;
119: }
120:
121: return result;
122: }
123:
124: public int currentSegment(double[] coords) {
125: int result = iterator.currentSegment(coords);
126:
127: for (int i = 0; i < coords.length; i++) {
128: coords[i++] += dx;
129: coords[i] += dy;
130: }
131:
132: return result;
133: }
134: }
135: }
|