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 is used to represent an area of the viewport. It supports both
032: * clipping areas, in viewport coordinates (which is why it uses integers), and
033: * dirty area management.
034: *
035: * @author <a href="mailto:kevin.wong@sun.com">Kevin Wong</a>
036: * @version $Id: Tile.java,v 1.6 2006/04/21 06:35:19 st125089 Exp $
037: */
038: public class Tile {
039: /**
040: * Start and end x tile coordinates.
041: */
042: public int x, maxX;
043:
044: /**
045: * Start and end y tile coordinates.
046: */
047: public int y, maxY;
048:
049: /**
050: * Default constructor.
051: */
052: public Tile() {
053: }
054:
055: /**
056: * Copy constructor
057: *
058: * @param t the Tile to copy. Should not be null.
059: */
060: public Tile(Tile t) {
061: this ();
062: setTile(t);
063: }
064:
065: /**
066: * Constructor with the Tile's coordinates.
067: *
068: * @param x the tile's origin along the x-axis
069: * @param y the tile's origin along the y-axis
070: * @param width the tile's width
071: * @param height the tile's height
072: */
073: public Tile(int x, int y, int width, int height) {
074: setTile(x, y, width, height);
075: }
076:
077: /**
078: * Sets the tile's dimension and origin.
079: *
080: * @param x the tile's origin along the x-axis
081: * @param y the tile's origin along the y-axis
082: * @param width the tile's width
083: * @param height the tile's height
084: */
085: public void setTile(int x, int y, int width, int height) {
086: this .x = x;
087: this .y = y;
088: this .maxX = x + width - 1;
089: this .maxY = y + height - 1;
090: }
091:
092: /**
093: * Sets this tile to the same x/y/maxX/maxY as t.
094: *
095: * @param t the Tile to copy.
096: */
097: public void setTile(final Tile t) {
098: this .x = t.x;
099: this .y = t.y;
100: this .maxX = t.maxX;
101: this .maxY = t.maxY;
102: }
103:
104: /**
105: * Sets this tile to be empty. Actually, this sets the tile to a one pixel tile
106: * in the top left corner of the integer coordinate grid (i.e, in Integer.MIN_VALUE,
107: * Integer.MIN_VALUE). This tile will never intersect with any other tile.
108: */
109: public void setEmptyTile() {
110: this .x = Integer.MIN_VALUE;
111: this .y = Integer.MIN_VALUE;
112: this .maxX = Integer.MIN_VALUE;
113: this .maxY = Integer.MIN_VALUE;
114: }
115:
116: /**
117: * Adds the input tile to this tile.
118: *
119: * @param tile the Tile to add.
120: */
121: public void addTile(final Tile t) {
122: addTile(t.x, t.y, t.maxX, t.maxY);
123: }
124:
125: /**
126: * Adds the input tile, defined by its x, y, maxX, maxY
127: * values, to this tile.
128: *
129: * @param tx the tile's x-axis origin
130: * @param ty the tile's y-axis origin
131: * @param tmaxX the tile's x-axis bottom right coordinate.
132: * @param tmaxY the tile's y-axis bottom right coordinate.
133: */
134: public void addTile(final int tx, final int ty, final int tmaxX,
135: final int tmaxY) {
136: if (tx < x) {
137: x = tx;
138: }
139: if (ty < y) {
140: y = ty;
141: }
142: if (tmaxX > maxX) {
143: maxX = tmaxX;
144: }
145: if (tmaxY > maxY) {
146: maxY = tmaxY;
147: }
148: }
149:
150: /**
151: * Adds the input Box to the tile, after snapping it to the integer grid.
152: *
153: * @param b the Box instance to snap to the grid and add to the tile. Should
154: * not be null.
155: */
156: public void addSnapBox(final Box b) {
157: b.snap();
158: addTile((int) b.x, (int) b.y, (int) (b.x + b.width - 1),
159: (int) (b.y + b.height - 1));
160: }
161:
162: /**
163: * Sets the tile so that it snaps to the smallest pixel grid which completely
164: * contains the input Box.
165: *
166: * @param b the Box instance to snap to the grid. If null, the tile is set to
167: * have all its values set to Integer.MIN_VALUE.
168: */
169: public void snapBox(final Box b) {
170: if (b == null) {
171: x = Integer.MIN_VALUE;
172: y = Integer.MIN_VALUE;
173: maxX = Integer.MIN_VALUE;
174: maxY = Integer.MIN_VALUE;
175: } else {
176: b.snap();
177: x = (int) b.x;
178: y = (int) b.y;
179: maxX = (int) (b.x + b.width - 1);
180: maxY = (int) (b.y + b.height - 1);
181: }
182:
183: }
184:
185: /**
186: * @return true if the tile is hit by the input tile.
187: * @param t the tile to check. Should not be null.
188: */
189: public boolean isHit(final Tile t) {
190: return (t.maxX >= x) // Leave out tiles to the left of this tile
191: && (t.maxY >= y) // Leave out tiles to the top of this tile
192: && (t.x <= maxX) // Leave out tiles to the right of this tile
193: && (t.y <= maxY); // Leave out tiles to the bottom of this tile
194: }
195:
196: /**
197: * Debug.
198: */
199: public String toString() {
200: return "minX = " + x + " minY = " + y + " maxX = " + maxX
201: + " maxY = " + maxY;
202: }
203: }
|