1 7 package javax.print.attribute.standard; 8 9 import javax.print.attribute.Attribute ; 10 import javax.print.attribute.DocAttribute ; 11 import javax.print.attribute.PrintJobAttribute ; 12 import javax.print.attribute.PrintRequestAttribute ; 13 14 62 63 public final class MediaPrintableArea 64 implements DocAttribute , PrintRequestAttribute , PrintJobAttribute { 65 66 private int x, y, w, h; 67 private int units; 68 69 private static final long serialVersionUID = -1597171464050795793L; 70 71 75 public static final int INCH = 25400; 76 77 81 public static final int MM = 1000; 82 83 96 public MediaPrintableArea(float x, float y, float w, float h, int units) { 97 if ((x < 0.0) || (y < 0.0) || (w <= 0.0) || (h <= 0.0) || 98 (units < 1)) { 99 throw new IllegalArgumentException ("0 or negative value argument"); 100 } 101 102 this.x = (int) (x * units + 0.5f); 103 this.y = (int) (y * units + 0.5f); 104 this.w = (int) (w * units + 0.5f); 105 this.h = (int) (h * units + 0.5f); 106 107 } 108 109 122 public MediaPrintableArea(int x, int y, int w, int h, int units) { 123 if ((x < 0) || (y < 0) || (w <= 0) || (h <= 0) || 124 (units < 1)) { 125 throw new IllegalArgumentException ("0 or negative value argument"); 126 } 127 this.x = x * units; 128 this.y = y * units; 129 this.w = w * units; 130 this.h = h * units; 131 132 } 133 134 146 public float[] getPrintableArea(int units) { 147 return new float[] { getX(units), getY(units), 148 getWidth(units), getHeight(units) }; 149 } 150 151 164 public float getX(int units) { 165 return convertFromMicrometers(x, units); 166 } 167 168 181 public float getY(int units) { 182 return convertFromMicrometers(y, units); 183 } 184 185 196 public float getWidth(int units) { 197 return convertFromMicrometers(w, units); 198 } 199 200 211 public float getHeight(int units) { 212 return convertFromMicrometers(h, units); 213 } 214 215 233 public boolean equals(Object object) { 234 boolean ret = false; 235 if (object instanceof MediaPrintableArea ) { 236 MediaPrintableArea mm = (MediaPrintableArea )object; 237 if (x == mm.x && y == mm.y && w == mm.w && h == mm.h) { 238 ret = true; 239 } 240 } 241 return ret; 242 } 243 244 254 public final Class <? extends Attribute > getCategory() { 255 return MediaPrintableArea .class; 256 } 257 258 268 public final String getName() { 269 return "media-printable-area"; 270 } 271 272 288 public String toString(int units, String unitsName) { 289 if (unitsName == null) { 290 unitsName = ""; 291 } 292 float []vals = getPrintableArea(units); 293 String str = "("+vals[0]+","+vals[1]+")->("+vals[2]+","+vals[3]+")"; 294 return str + unitsName; 295 } 296 297 300 public String toString() { 301 return(toString(MM, "mm")); 302 } 303 304 307 public int hashCode() { 308 return x + 37*y + 43*w + 47*h; 309 } 310 311 private static float convertFromMicrometers(int x, int units) { 312 if (units < 1) { 313 throw new IllegalArgumentException ("units is < 1"); 314 } 315 return ((float)x) / ((float)units); 316 } 317 } 318 | Popular Tags |