JOGL v2.6.0-rc-20250712
JOGL, High-Performance Graphics Binding for Java™ (public API).
Dimension.java
Go to the documentation of this file.
1/**
2 * Copyright 2010 JogAmp Community. All rights reserved.
3 * Copyright (c) 2010 JogAmp Community. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification, are
6 * permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this list of
9 * conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 * of conditions and the following disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
16 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
21 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
22 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
23 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *
25 * The views and conclusions contained in the software and documentation are those of the
26 * authors and should not be interpreted as representing official policies, either expressed
27 * or implied, of JogAmp Community.
28 */
29
30package com.jogamp.nativewindow.util;
31
32public class Dimension implements Cloneable, DimensionImmutable {
33 int width;
34 int height;
35
36 public Dimension() {
37 this(0, 0);
38 }
39
40 public Dimension(final int[] size) {
41 this(size[0], size[1]);
42 }
43
44 public Dimension(final int width, final int height) {
45 if(width<0 || height<0) {
46 throw new IllegalArgumentException("width and height must be within: ["+0+".."+Integer.MAX_VALUE+"]");
47 }
48 this.width=width;
49 this.height=height;
50 }
51
52 @Override
53 public Object cloneMutable() {
54 return clone();
55 }
56
57 @Override
58 public Object clone() {
59 try {
60 return super.clone();
61 } catch (final CloneNotSupportedException ex) {
62 throw new InternalError();
63 }
64 }
65
66 @Override
67 public final int getWidth() { return width; }
68 @Override
69 public final int getHeight() { return height; }
70
71 public final void set(final int width, final int height) {
72 this.width = width;
73 this.height = height;
74 }
75 public final void setWidth(final int width) {
76 this.width = width;
77 }
78 public final void setHeight(final int height) {
79 this.height = height;
80 }
81 public final Dimension scale(final int s) {
82 width *= s;
83 height *= s;
84 return this;
85 }
86 public final Dimension add(final Dimension pd) {
87 width += pd.width ;
88 height += pd.height ;
89 return this;
90 }
91
92 @Override
93 public String toString() {
94 return width + " x " + height;
95 }
96
97 @Override
98 public int compareTo(final DimensionImmutable d) {
99 final int tsq = width*height;
100 final int xsq = d.getWidth()*d.getHeight();
101
102 if(tsq > xsq) {
103 return 1;
104 } else if(tsq < xsq) {
105 return -1;
106 }
107 return 0;
108 }
109
110 @Override
111 public boolean equals(final Object obj) {
112 if(this == obj) { return true; }
113 if (obj instanceof Dimension) {
114 final Dimension p = (Dimension)obj;
115 return height == p.height &&
116 width == p.width ;
117 }
118 return false;
119 }
120
121 @Override
122 public int hashCode() {
123 // 31 * x == (x << 5) - x
124 final int hash = 31 + width;
125 return ((hash << 5) - hash) + height;
126 }
127}
128
final void setWidth(final int width)
Definition: Dimension.java:75
boolean equals(final Object obj)
Checks whether two dimensions objects are equal.
Definition: Dimension.java:111
int compareTo(final DimensionImmutable d)
Definition: Dimension.java:98
final Dimension add(final Dimension pd)
Definition: Dimension.java:86
final Dimension scale(final int s)
Definition: Dimension.java:81
Dimension(final int width, final int height)
Definition: Dimension.java:44
final void setHeight(final int height)
Definition: Dimension.java:78
Immutable Dimension Interface, consisting of it's read only components: