0N/A/*
2362N/A * Copyright (c) 1997, 2000, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.awt.print;
0N/A
0N/Aimport java.awt.geom.AffineTransform;
0N/Aimport java.awt.geom.Point2D;
0N/Aimport java.awt.geom.Rectangle2D;
0N/A
0N/A/**
0N/A * The <code>PageFormat</code> class describes the size and
0N/A * orientation of a page to be printed.
0N/A */
0N/Apublic class PageFormat implements Cloneable
0N/A{
0N/A
0N/A /* Class Constants */
0N/A
0N/A /**
0N/A * The origin is at the bottom left of the paper with
0N/A * x running bottom to top and y running left to right.
0N/A * Note that this is not the Macintosh landscape but
0N/A * is the Window's and PostScript landscape.
0N/A */
0N/A public static final int LANDSCAPE = 0;
0N/A
0N/A /**
0N/A * The origin is at the top left of the paper with
0N/A * x running to the right and y running down the
0N/A * paper.
0N/A */
0N/A public static final int PORTRAIT = 1;
0N/A
0N/A /**
0N/A * The origin is at the top right of the paper with x
0N/A * running top to bottom and y running right to left.
0N/A * Note that this is the Macintosh landscape.
0N/A */
0N/A public static final int REVERSE_LANDSCAPE = 2;
0N/A
0N/A /* Instance Variables */
0N/A
0N/A /**
0N/A * A description of the physical piece of paper.
0N/A */
0N/A private Paper mPaper;
0N/A
0N/A /**
0N/A * The orientation of the current page. This will be
0N/A * one of the constants: PORTRIAT, LANDSCAPE, or
0N/A * REVERSE_LANDSCAPE,
0N/A */
0N/A private int mOrientation = PORTRAIT;
0N/A
0N/A /* Constructors */
0N/A
0N/A /**
0N/A * Creates a default, portrait-oriented
0N/A * <code>PageFormat</code>.
0N/A */
0N/A public PageFormat()
0N/A {
0N/A mPaper = new Paper();
0N/A }
0N/A
0N/A /* Instance Methods */
0N/A
0N/A /**
0N/A * Makes a copy of this <code>PageFormat</code> with the same
0N/A * contents as this <code>PageFormat</code>.
0N/A * @return a copy of this <code>PageFormat</code>.
0N/A */
0N/A public Object clone() {
0N/A PageFormat newPage;
0N/A
0N/A try {
0N/A newPage = (PageFormat) super.clone();
0N/A newPage.mPaper = (Paper)mPaper.clone();
0N/A
0N/A } catch (CloneNotSupportedException e) {
0N/A e.printStackTrace();
0N/A newPage = null; // should never happen.
0N/A }
0N/A
0N/A return newPage;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns the width, in 1/72nds of an inch, of the page.
0N/A * This method takes into account the orientation of the
0N/A * page when determining the width.
0N/A * @return the width of the page.
0N/A */
0N/A public double getWidth() {
0N/A double width;
0N/A int orientation = getOrientation();
0N/A
0N/A if (orientation == PORTRAIT) {
0N/A width = mPaper.getWidth();
0N/A } else {
0N/A width = mPaper.getHeight();
0N/A }
0N/A
0N/A return width;
0N/A }
0N/A
0N/A /**
0N/A * Returns the height, in 1/72nds of an inch, of the page.
0N/A * This method takes into account the orientation of the
0N/A * page when determining the height.
0N/A * @return the height of the page.
0N/A */
0N/A public double getHeight() {
0N/A double height;
0N/A int orientation = getOrientation();
0N/A
0N/A if (orientation == PORTRAIT) {
0N/A height = mPaper.getHeight();
0N/A } else {
0N/A height = mPaper.getWidth();
0N/A }
0N/A
0N/A return height;
0N/A }
0N/A
0N/A /**
0N/A * Returns the x coordinate of the upper left point of the
0N/A * imageable area of the <code>Paper</code> object
0N/A * associated with this <code>PageFormat</code>.
0N/A * This method takes into account the
0N/A * orientation of the page.
0N/A * @return the x coordinate of the upper left point of the
0N/A * imageable area of the <code>Paper</code> object
0N/A * associated with this <code>PageFormat</code>.
0N/A */
0N/A public double getImageableX() {
0N/A double x;
0N/A
0N/A switch (getOrientation()) {
0N/A
0N/A case LANDSCAPE:
0N/A x = mPaper.getHeight()
0N/A - (mPaper.getImageableY() + mPaper.getImageableHeight());
0N/A break;
0N/A
0N/A case PORTRAIT:
0N/A x = mPaper.getImageableX();
0N/A break;
0N/A
0N/A case REVERSE_LANDSCAPE:
0N/A x = mPaper.getImageableY();
0N/A break;
0N/A
0N/A default:
0N/A /* This should never happen since it signifies that the
0N/A * PageFormat is in an invalid orientation.
0N/A */
0N/A throw new InternalError("unrecognized orientation");
0N/A
0N/A }
0N/A
0N/A return x;
0N/A }
0N/A
0N/A /**
0N/A * Returns the y coordinate of the upper left point of the
0N/A * imageable area of the <code>Paper</code> object
0N/A * associated with this <code>PageFormat</code>.
0N/A * This method takes into account the
0N/A * orientation of the page.
0N/A * @return the y coordinate of the upper left point of the
0N/A * imageable area of the <code>Paper</code> object
0N/A * associated with this <code>PageFormat</code>.
0N/A */
0N/A public double getImageableY() {
0N/A double y;
0N/A
0N/A switch (getOrientation()) {
0N/A
0N/A case LANDSCAPE:
0N/A y = mPaper.getImageableX();
0N/A break;
0N/A
0N/A case PORTRAIT:
0N/A y = mPaper.getImageableY();
0N/A break;
0N/A
0N/A case REVERSE_LANDSCAPE:
0N/A y = mPaper.getWidth()
0N/A - (mPaper.getImageableX() + mPaper.getImageableWidth());
0N/A break;
0N/A
0N/A default:
0N/A /* This should never happen since it signifies that the
0N/A * PageFormat is in an invalid orientation.
0N/A */
0N/A throw new InternalError("unrecognized orientation");
0N/A
0N/A }
0N/A
0N/A return y;
0N/A }
0N/A
0N/A /**
0N/A * Returns the width, in 1/72nds of an inch, of the imageable
0N/A * area of the page. This method takes into account the orientation
0N/A * of the page.
0N/A * @return the width of the page.
0N/A */
0N/A public double getImageableWidth() {
0N/A double width;
0N/A
0N/A if (getOrientation() == PORTRAIT) {
0N/A width = mPaper.getImageableWidth();
0N/A } else {
0N/A width = mPaper.getImageableHeight();
0N/A }
0N/A
0N/A return width;
0N/A }
0N/A
0N/A /**
0N/A * Return the height, in 1/72nds of an inch, of the imageable
0N/A * area of the page. This method takes into account the orientation
0N/A * of the page.
0N/A * @return the height of the page.
0N/A */
0N/A public double getImageableHeight() {
0N/A double height;
0N/A
0N/A if (getOrientation() == PORTRAIT) {
0N/A height = mPaper.getImageableHeight();
0N/A } else {
0N/A height = mPaper.getImageableWidth();
0N/A }
0N/A
0N/A return height;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns a copy of the {@link Paper} object associated
0N/A * with this <code>PageFormat</code>. Changes made to the
0N/A * <code>Paper</code> object returned from this method do not
0N/A * affect the <code>Paper</code> object of this
0N/A * <code>PageFormat</code>. To update the <code>Paper</code>
0N/A * object of this <code>PageFormat</code>, create a new
0N/A * <code>Paper</code> object and set it into this
0N/A * <code>PageFormat</code> by using the {@link #setPaper(Paper)}
0N/A * method.
0N/A * @return a copy of the <code>Paper</code> object associated
0N/A * with this <code>PageFormat</code>.
0N/A * @see #setPaper
0N/A */
0N/A public Paper getPaper() {
0N/A return (Paper)mPaper.clone();
0N/A }
0N/A
0N/A /**
0N/A * Sets the <code>Paper</code> object for this
0N/A * <code>PageFormat</code>.
0N/A * @param paper the <code>Paper</code> object to which to set
0N/A * the <code>Paper</code> object for this <code>PageFormat</code>.
0N/A * @exception <code>NullPointerException</code>
0N/A * a null paper instance was passed as a parameter.
0N/A * @see #getPaper
0N/A */
0N/A public void setPaper(Paper paper) {
0N/A mPaper = (Paper)paper.clone();
0N/A }
0N/A
0N/A /**
0N/A * Sets the page orientation. <code>orientation</code> must be
0N/A * one of the constants: PORTRAIT, LANDSCAPE,
0N/A * or REVERSE_LANDSCAPE.
0N/A * @param orientation the new orientation for the page
0N/A * @throws IllegalArgumentException if
0N/A * an unknown orientation was requested
0N/A * @see #getOrientation
0N/A */
0N/A public void setOrientation(int orientation) throws IllegalArgumentException
0N/A {
0N/A if (0 <= orientation && orientation <= REVERSE_LANDSCAPE) {
0N/A mOrientation = orientation;
0N/A } else {
0N/A throw new IllegalArgumentException();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns the orientation of this <code>PageFormat</code>.
0N/A * @return this <code>PageFormat</code> object's orientation.
0N/A * @see #setOrientation
0N/A */
0N/A public int getOrientation() {
0N/A return mOrientation;
0N/A }
0N/A
0N/A /**
0N/A * Returns a transformation matrix that translates user
0N/A * space rendering to the requested orientation
0N/A * of the page. The values are placed into the
0N/A * array as
0N/A * {&nbsp;m00,&nbsp;m10,&nbsp;m01,&nbsp;m11,&nbsp;m02,&nbsp;m12} in
0N/A * the form required by the {@link AffineTransform}
0N/A * constructor.
0N/A * @return the matrix used to translate user space rendering
0N/A * to the orientation of the page.
0N/A * @see java.awt.geom.AffineTransform
0N/A */
0N/A public double[] getMatrix() {
0N/A double[] matrix = new double[6];
0N/A
0N/A switch (mOrientation) {
0N/A
0N/A case LANDSCAPE:
0N/A matrix[0] = 0; matrix[1] = -1;
0N/A matrix[2] = 1; matrix[3] = 0;
0N/A matrix[4] = 0; matrix[5] = mPaper.getHeight();
0N/A break;
0N/A
0N/A case PORTRAIT:
0N/A matrix[0] = 1; matrix[1] = 0;
0N/A matrix[2] = 0; matrix[3] = 1;
0N/A matrix[4] = 0; matrix[5] = 0;
0N/A break;
0N/A
0N/A case REVERSE_LANDSCAPE:
0N/A matrix[0] = 0; matrix[1] = 1;
0N/A matrix[2] = -1; matrix[3] = 0;
0N/A matrix[4] = mPaper.getWidth(); matrix[5] = 0;
0N/A break;
0N/A
0N/A default:
0N/A throw new IllegalArgumentException();
0N/A }
0N/A
0N/A return matrix;
0N/A }
0N/A}