JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
Rect.java
Go to the documentation of this file.
1/*
2 * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * - Redistribution of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * - Redistribution in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * Neither the name of Sun Microsystems, Inc. or the names of
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * This software is provided "AS IS," without a warranty of any kind. ALL
20 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
21 * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
22 * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN
23 * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR
24 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
25 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR
26 * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR
27 * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
28 * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
29 * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF
30 * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
31 *
32 * You acknowledge that this software is not designed or intended for use
33 * in the design, construction, operation or maintenance of any nuclear
34 * facility.
35 *
36 * Sun gratefully acknowledges that this software was originally authored
37 * and developed by Kenneth Bradley Russell and Christopher John Kline.
38 */
39
40package com.jogamp.opengl.util.packrect;
41
42/** Represents a rectangular region on the backing store. The edges of
43 the rectangle are the infinitely thin region between adjacent
44 pixels on the screen. The origin of the rectangle is its
45 upper-left corner. It is inclusive of the pixels on the top and
46 left edges and exclusive of the pixels on the bottom and right
47 edges. For example, a rect at position (0, 0) and of size (1, 1)
48 would include only the pixel at (0, 0). <P>
49
50 Negative coordinates and sizes are not supported, since they make
51 no sense in the context of the packer, which deals only with
52 positively sized regions. <P>
53
54 This class contains a user data field for efficient hookup to
55 external data structures as well as enough other hooks to
56 efficiently plug into the rectangle packer. */
57
58public class Rect {
59 private int x;
60 private int y;
61 private int w;
62 private int h;
63
64 // The level we're currently installed in in the parent
65 // RectanglePacker, or null if not hooked in to the table yet
66 private Level level;
67
68 // The user's object this rectangle represents.
69 private Object userData;
70
71 // Used transiently during re-layout of the backing store (when
72 // there is no room left due either to fragmentation or just being
73 // out of space)
74 private Rect nextLocation;
75
76 public Rect() {
77 this(null);
78 }
79
80 public Rect(final Object userData) {
81 this(0, 0, 0, 0, userData);
82 }
83
84 public Rect(final int x, final int y, final int w, final int h, final Object userData) {
85 setPosition(x, y);
86 setSize(w, h);
87 setUserData(userData);
88 }
89
90 public int x() { return x; }
91 public int y() { return y; }
92 public int w() { return w; }
93 public int h() { return h; }
94 public Object getUserData() { return userData; }
95 public Rect getNextLocation() { return nextLocation; }
96
97 public void setPosition(final int x, final int y) {
98 if (x < 0)
99 throw new IllegalArgumentException("Negative x");
100 if (y < 0)
101 throw new IllegalArgumentException("Negative y");
102 this.x = x;
103 this.y = y;
104 }
105
106 public void setSize(final int w, final int h) throws IllegalArgumentException {
107 if (w < 0)
108 throw new IllegalArgumentException("Negative width");
109 if (h < 0)
110 throw new IllegalArgumentException("Negative height");
111 this.w = w;
112 this.h = h;
113 }
114
115 public void setUserData(final Object obj) { userData = obj; }
116 public void setNextLocation(final Rect nextLocation) { this.nextLocation = nextLocation; }
117
118 // Helpers for computations.
119
120 /** Returns the maximum x-coordinate contained within this
121 rectangle. Note that this returns a different result than Java
122 2D's rectangles; for a rectangle of position (0, 0) and size (1,
123 1) this will return 0, not 1. Returns -1 if the width of this
124 rectangle is 0. */
125 public int maxX() {
126 if (w() < 1)
127 return -1;
128 return x() + w() - 1;
129 }
130
131 /** Returns the maximum y-coordinate contained within this
132 rectangle. Note that this returns a different result than Java
133 2D's rectangles; for a rectangle of position (0, 0) and size (1,
134 1) this will return 0, not 1. Returns -1 if the height of this
135 rectangle is 0. */
136 public int maxY() {
137 if (h() < 1)
138 return -1;
139 return y() + h() - 1;
140 }
141
142 public boolean canContain(final Rect other) {
143 return (w() >= other.w() &&
144 h() >= other.h());
145 }
146
147 @Override
148 public String toString() {
149 return "[Rect x: " + x() + " y: " + y() + " w: " + w() + " h: " + h() + "]";
150 }
151
152 // Unclear whether it's a good idea to override hashCode and equals
153 // for these objects
154 /*
155 public boolean equals(Object other) {
156 if (other == null || (!(other instanceof Rect))) {
157 return false;
158 }
159
160 Rect r = (Rect) other;
161 return (this.x() == r.x() &&
162 this.y() == r.y() &&
163 this.w() == r.w() &&
164 this.h() == r.h());
165 }
166
167 public int hashCode() {
168 return (x + y * 13 + w * 17 + h * 23);
169 }
170 */
171}
Represents a rectangular region on the backing store.
Definition: Rect.java:58
void setPosition(final int x, final int y)
Definition: Rect.java:97
boolean canContain(final Rect other)
Definition: Rect.java:142
int maxX()
Returns the maximum x-coordinate contained within this rectangle.
Definition: Rect.java:125
void setUserData(final Object obj)
Definition: Rect.java:115
int maxY()
Returns the maximum y-coordinate contained within this rectangle.
Definition: Rect.java:136
Rect(final Object userData)
Definition: Rect.java:80
void setNextLocation(final Rect nextLocation)
Definition: Rect.java:116
Rect(final int x, final int y, final int w, final int h, final Object userData)
Definition: Rect.java:84
void setSize(final int w, final int h)
Definition: Rect.java:106