Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.desktop/share/classes/java/awt/Dimension.java
41152 views
1
/*
2
* Copyright (c) 1995, 2021, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package java.awt;
27
28
import java.awt.geom.Dimension2D;
29
import java.beans.Transient;
30
import java.io.Serial;
31
32
/**
33
* The {@code Dimension} class encapsulates the width and
34
* height of a component (in integer precision) in a single object.
35
* The class is
36
* associated with certain properties of components. Several methods
37
* defined by the {@code Component} class and the
38
* {@code LayoutManager} interface return a
39
* {@code Dimension} object.
40
* <p>
41
* Normally the values of {@code width}
42
* and {@code height} are non-negative integers.
43
* The constructors that allow you to create a dimension do
44
* not prevent you from setting a negative value for these properties.
45
* If the value of {@code width} or {@code height} is
46
* negative, the behavior of some methods defined by other objects is
47
* undefined.
48
*
49
* @author Sami Shaio
50
* @author Arthur van Hoff
51
* @see java.awt.Component
52
* @see java.awt.LayoutManager
53
* @since 1.0
54
*/
55
public class Dimension extends Dimension2D implements java.io.Serializable {
56
57
/**
58
* The width dimension; negative values can be used.
59
*
60
* @serial
61
* @see #getSize
62
* @see #setSize
63
* @since 1.0
64
*/
65
public int width;
66
67
/**
68
* The height dimension; negative values can be used.
69
*
70
* @serial
71
* @see #getSize
72
* @see #setSize
73
* @since 1.0
74
*/
75
public int height;
76
77
/**
78
* Use serialVersionUID from JDK 1.1 for interoperability.
79
*/
80
@Serial
81
private static final long serialVersionUID = 4723952579491349524L;
82
83
/**
84
* Initialize JNI field and method IDs
85
*/
86
private static native void initIDs();
87
88
static {
89
/* ensure that the necessary native libraries are loaded */
90
Toolkit.loadLibraries();
91
if (!GraphicsEnvironment.isHeadless()) {
92
initIDs();
93
}
94
}
95
96
/**
97
* Creates an instance of {@code Dimension} with a width
98
* of zero and a height of zero.
99
*/
100
public Dimension() {
101
this(0, 0);
102
}
103
104
/**
105
* Creates an instance of {@code Dimension} whose width
106
* and height are the same as for the specified dimension.
107
*
108
* @param d the specified dimension for the
109
* {@code width} and
110
* {@code height} values
111
*/
112
public Dimension(Dimension d) {
113
this(d.width, d.height);
114
}
115
116
/**
117
* Constructs a {@code Dimension} and initializes
118
* it to the specified width and specified height.
119
*
120
* @param width the specified width
121
* @param height the specified height
122
*/
123
public Dimension(int width, int height) {
124
this.width = width;
125
this.height = height;
126
}
127
128
/**
129
* {@inheritDoc}
130
* @since 1.2
131
*/
132
public double getWidth() {
133
return width;
134
}
135
136
/**
137
* {@inheritDoc}
138
* @since 1.2
139
*/
140
public double getHeight() {
141
return height;
142
}
143
144
/**
145
* Sets the size of this {@code Dimension} object to
146
* the specified width and height in double precision.
147
* Note that if {@code width} or {@code height}
148
* are larger than {@code Integer.MAX_VALUE}, they will
149
* be reset to {@code Integer.MAX_VALUE}.
150
*
151
* @param width the new width for the {@code Dimension} object
152
* @param height the new height for the {@code Dimension} object
153
* @since 1.2
154
*/
155
public void setSize(double width, double height) {
156
this.width = (int) Math.ceil(width);
157
this.height = (int) Math.ceil(height);
158
}
159
160
/**
161
* Gets the size of this {@code Dimension} object.
162
* This method is included for completeness, to parallel the
163
* {@code getSize} method defined by {@code Component}.
164
*
165
* @return the size of this dimension, a new instance of
166
* {@code Dimension} with the same width and height
167
* @see java.awt.Dimension#setSize
168
* @see java.awt.Component#getSize
169
* @since 1.1
170
*/
171
@Transient
172
public Dimension getSize() {
173
return new Dimension(width, height);
174
}
175
176
/**
177
* Sets the size of this {@code Dimension} object to the specified size.
178
* This method is included for completeness, to parallel the
179
* {@code setSize} method defined by {@code Component}.
180
* @param d the new size for this {@code Dimension} object
181
* @see java.awt.Dimension#getSize
182
* @see java.awt.Component#setSize
183
* @since 1.1
184
*/
185
public void setSize(Dimension d) {
186
setSize(d.width, d.height);
187
}
188
189
/**
190
* Sets the size of this {@code Dimension} object
191
* to the specified width and height.
192
* This method is included for completeness, to parallel the
193
* {@code setSize} method defined by {@code Component}.
194
*
195
* @param width the new width for this {@code Dimension} object
196
* @param height the new height for this {@code Dimension} object
197
* @see java.awt.Dimension#getSize
198
* @see java.awt.Component#setSize
199
* @since 1.1
200
*/
201
public void setSize(int width, int height) {
202
this.width = width;
203
this.height = height;
204
}
205
206
/**
207
* Checks whether two dimension objects have equal values.
208
*/
209
public boolean equals(Object obj) {
210
if (obj instanceof Dimension) {
211
Dimension d = (Dimension)obj;
212
return (width == d.width) && (height == d.height);
213
}
214
return false;
215
}
216
217
/**
218
* Returns the hash code for this {@code Dimension}.
219
*
220
* @return a hash code for this {@code Dimension}
221
*/
222
public int hashCode() {
223
int sum = width + height;
224
return sum * (sum + 1)/2 + width;
225
}
226
227
/**
228
* Returns a string representation of the values of this
229
* {@code Dimension} object's {@code height} and
230
* {@code width} fields. This method is intended to be used only
231
* for debugging purposes, and the content and format of the returned
232
* string may vary between implementations. The returned string may be
233
* empty but may not be {@code null}.
234
*
235
* @return a string representation of this {@code Dimension}
236
* object
237
*/
238
public String toString() {
239
return getClass().getName() + "[width=" + width + ",height=" + height + "]";
240
}
241
}
242
243