1 7 8 package java.awt.print; 9 10 import java.awt.geom.AffineTransform ; 11 import java.awt.geom.Point2D ; 12 import java.awt.geom.Rectangle2D ; 13 14 18 public class PageFormat implements Cloneable 19 { 20 21 22 23 29 public static final int LANDSCAPE = 0; 30 31 36 public static final int PORTRAIT = 1; 37 38 43 public static final int REVERSE_LANDSCAPE = 2; 44 45 46 47 50 private Paper mPaper; 51 52 57 private int mOrientation = PORTRAIT; 58 59 60 61 65 public PageFormat() 66 { 67 mPaper = new Paper (); 68 } 69 70 71 72 77 public Object clone() { 78 PageFormat newPage; 79 80 try { 81 newPage = (PageFormat ) super.clone(); 82 newPage.mPaper = (Paper )mPaper.clone(); 83 84 } catch (CloneNotSupportedException e) { 85 e.printStackTrace(); 86 newPage = null; } 88 89 return newPage; 90 } 91 92 93 99 public double getWidth() { 100 double width; 101 int orientation = getOrientation(); 102 103 if (orientation == PORTRAIT) { 104 width = mPaper.getWidth(); 105 } else { 106 width = mPaper.getHeight(); 107 } 108 109 return width; 110 } 111 112 118 public double getHeight() { 119 double height; 120 int orientation = getOrientation(); 121 122 if (orientation == PORTRAIT) { 123 height = mPaper.getHeight(); 124 } else { 125 height = mPaper.getWidth(); 126 } 127 128 return height; 129 } 130 131 141 public double getImageableX() { 142 double x; 143 144 switch (getOrientation()) { 145 146 case LANDSCAPE: 147 x = mPaper.getHeight() 148 - (mPaper.getImageableY() + mPaper.getImageableHeight()); 149 break; 150 151 case PORTRAIT: 152 x = mPaper.getImageableX(); 153 break; 154 155 case REVERSE_LANDSCAPE: 156 x = mPaper.getImageableY(); 157 break; 158 159 default: 160 163 throw new InternalError ("unrecognized orientation"); 164 165 } 166 167 return x; 168 } 169 170 180 public double getImageableY() { 181 double y; 182 183 switch (getOrientation()) { 184 185 case LANDSCAPE: 186 y = mPaper.getImageableX(); 187 break; 188 189 case PORTRAIT: 190 y = mPaper.getImageableY(); 191 break; 192 193 case REVERSE_LANDSCAPE: 194 y = mPaper.getWidth() 195 - (mPaper.getImageableX() + mPaper.getImageableWidth()); 196 break; 197 198 default: 199 202 throw new InternalError ("unrecognized orientation"); 203 204 } 205 206 return y; 207 } 208 209 215 public double getImageableWidth() { 216 double width; 217 218 if (getOrientation() == PORTRAIT) { 219 width = mPaper.getImageableWidth(); 220 } else { 221 width = mPaper.getImageableHeight(); 222 } 223 224 return width; 225 } 226 227 233 public double getImageableHeight() { 234 double height; 235 236 if (getOrientation() == PORTRAIT) { 237 height = mPaper.getImageableHeight(); 238 } else { 239 height = mPaper.getImageableWidth(); 240 } 241 242 return height; 243 } 244 245 246 260 public Paper getPaper() { 261 return (Paper )mPaper.clone(); 262 } 263 264 273 public void setPaper(Paper paper) { 274 mPaper = (Paper )paper.clone(); 275 } 276 277 286 public void setOrientation(int orientation) throws IllegalArgumentException 287 { 288 if (0 <= orientation && orientation <= REVERSE_LANDSCAPE) { 289 mOrientation = orientation; 290 } else { 291 throw new IllegalArgumentException (); 292 } 293 } 294 295 300 public int getOrientation() { 301 return mOrientation; 302 } 303 304 316 public double[] getMatrix() { 317 double[] matrix = new double[6]; 318 319 switch (mOrientation) { 320 321 case LANDSCAPE: 322 matrix[0] = 0; matrix[1] = -1; 323 matrix[2] = 1; matrix[3] = 0; 324 matrix[4] = 0; matrix[5] = mPaper.getHeight(); 325 break; 326 327 case PORTRAIT: 328 matrix[0] = 1; matrix[1] = 0; 329 matrix[2] = 0; matrix[3] = 1; 330 matrix[4] = 0; matrix[5] = 0; 331 break; 332 333 case REVERSE_LANDSCAPE: 334 matrix[0] = 0; matrix[1] = 1; 335 matrix[2] = -1; matrix[3] = 0; 336 matrix[4] = mPaper.getWidth(); matrix[5] = 0; 337 break; 338 339 default: 340 throw new IllegalArgumentException (); 341 } 342 343 return matrix; 344 } 345 } 346 | Popular Tags |