001/** 002 * 003 */ 004package icy.type.dimension; 005 006import java.awt.Dimension; 007 008/** 009 * The <code>Dimension2D</code> class is to encapsulate a 2D dimension (X,Y). 010 * 011 * @author Stephane 012 */ 013public abstract class Dimension2D extends java.awt.geom.Dimension2D 014{ 015 /** 016 * Returns the size of the X dimension in double precision.<br> 017 * Same as {@link #getWidth()} 018 * 019 * @return the size of the X dimension. 020 * @see Dimension2D#getWidth() 021 */ 022 public double getSizeX() 023 { 024 return getWidth(); 025 } 026 027 /** 028 * Returns the size of the Y dimension in double precision.<br> 029 * Same as {@link #getHeight()} 030 * 031 * @return the size of the Y dimension. 032 * @see Dimension2D#getHeight() 033 */ 034 public double getSizeY() 035 { 036 return getHeight(); 037 } 038 039 /** 040 * Sets the size of the X dimension of this <code>Dimension2D</code> in <code>double</code> 041 * precision. 042 */ 043 public abstract void setSizeX(double value); 044 045 /** 046 * Sets the size of the Y dimension of this <code>Dimension2D</code> in <code>double</code> 047 * precision. 048 */ 049 public abstract void setSizeY(double value); 050 051 /** 052 * Sets the size of this <code>Dimension2D</code> object. 053 * 054 * @param sizeX 055 * the new size for the X dimension 056 * @param sizeY 057 * the new size for the Y dimension 058 */ 059 @Override 060 public abstract void setSize(double sizeX, double sizeY); 061 062 /** 063 * Returns an integer {@link Dimension} that encloses the double <code>Dimension2D</code>.<br> 064 * The returned <code>Dimension</code> might also fail to completely enclose the original double 065 * <code>Dimension2D</code> if it overflows the limited range of the integer data type. 066 * 067 * @return an integer <code>Dimension</code> that completely encloses 068 * the actual double <code>Dimension2D</code>. 069 */ 070 071 public Dimension getDimInt() 072 { 073 return new Dimension((int) Math.ceil(getSizeX()), (int) Math.ceil(getSizeY())); 074 } 075 076 /** 077 * Returns <code>true</code> if the X dimension should be considered as infinite. 078 */ 079 public abstract boolean isInfiniteX(); 080 081 /** 082 * Returns <code>true</code> if the Y dimension should be considered as infinite. 083 */ 084 public abstract boolean isInfiniteY(); 085 086 @Override 087 public boolean equals(Object obj) 088 { 089 if (obj instanceof Dimension2D) 090 { 091 final Dimension2D dim = (Dimension2D) obj; 092 return (getSizeX() == dim.getSizeX()) && (getSizeY() == dim.getSizeY()); 093 } 094 095 return super.equals(obj); 096 } 097 098 @Override 099 public String toString() 100 { 101 return getClass().getName() + "[" + getSizeX() + "," + getSizeY() + "]"; 102 } 103 104 public static class Double extends Dimension2D 105 { 106 public double sizeX; 107 public double sizeY; 108 109 public Double(double sizeX, double sizeY) 110 { 111 super(); 112 113 this.sizeX = sizeX; 114 this.sizeY = sizeY; 115 } 116 117 public Double(double[] sizeXY) 118 { 119 final int len = sizeXY.length; 120 121 if (len > 0) 122 sizeX = sizeXY[0]; 123 if (len > 1) 124 sizeY = sizeXY[1]; 125 } 126 127 public Double() 128 { 129 this(0d, 0d); 130 } 131 132 @Override 133 public double getWidth() 134 { 135 return sizeX; 136 } 137 138 @Override 139 public double getHeight() 140 { 141 return sizeY; 142 } 143 144 @Override 145 public void setSize(double sizeX, double sizeY) 146 { 147 this.sizeX = sizeX; 148 this.sizeY = sizeY; 149 } 150 151 @Override 152 public void setSizeX(double value) 153 { 154 sizeX = value; 155 } 156 157 @Override 158 public void setSizeY(double value) 159 { 160 sizeY = value; 161 } 162 163 @Override 164 public boolean isInfiniteX() 165 { 166 return (getSizeX() == java.lang.Double.POSITIVE_INFINITY); 167 } 168 169 @Override 170 public boolean isInfiniteY() 171 { 172 return (getSizeY() == java.lang.Double.POSITIVE_INFINITY); 173 } 174 } 175 176 public static class Float extends Dimension2D 177 { 178 public float sizeX; 179 public float sizeY; 180 181 public Float(float sizeX, float sizeY) 182 { 183 super(); 184 185 this.sizeX = sizeX; 186 this.sizeY = sizeY; 187 } 188 189 public Float(float[] sizeXY) 190 { 191 final int len = sizeXY.length; 192 193 if (len > 0) 194 sizeX = sizeXY[0]; 195 if (len > 1) 196 sizeY = sizeXY[1]; 197 } 198 199 public Float() 200 { 201 this(0f, 0f); 202 } 203 204 @Override 205 public double getWidth() 206 { 207 return sizeX; 208 } 209 210 @Override 211 public double getHeight() 212 { 213 return sizeY; 214 } 215 216 @Override 217 public void setSize(double sizeX, double sizeY) 218 { 219 this.sizeX = (float) sizeX; 220 this.sizeY = (float) sizeY; 221 } 222 223 @Override 224 public void setSizeX(double value) 225 { 226 sizeX = (float) value; 227 } 228 229 @Override 230 public void setSizeY(double value) 231 { 232 sizeY = (float) value; 233 } 234 235 @Override 236 public boolean isInfiniteX() 237 { 238 return (getSizeX() == java.lang.Float.POSITIVE_INFINITY); 239 } 240 241 @Override 242 public boolean isInfiniteY() 243 { 244 return (getSizeY() == java.lang.Float.POSITIVE_INFINITY); 245 } 246 } 247}