jdk/src/share/classes/java/awt/print/Paper.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1997-2006 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.awt.print;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.geom.Rectangle2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * The <code>Paper</code> class describes the physical characteristics of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * a piece of paper.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * When creating a <code>Paper</code> object, it is the application's
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * responsibility to ensure that the paper size and the imageable area
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * are compatible.  For example, if the paper size is changed from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * 11 x 17 to 8.5 x 11, the application might need to reduce the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * imageable area so that whatever is printed fits on the page.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @see #setSize(double, double)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @see #setImageableArea(double, double, double, double)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
public class Paper implements Cloneable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 /* Private Class Variables */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private static final int INCH = 72;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private static final double LETTER_WIDTH = 8.5 * INCH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private static final double LETTER_HEIGHT = 11 * INCH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 /* Instance Variables */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * The height of the physical page in 1/72nds
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * of an inch. The number is stored as a floating
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * point value rather than as an integer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * to facilitate the conversion from metric
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * units to 1/72nds of an inch and then back.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * (This may or may not be a good enough reason
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * for a float).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private double mHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * The width of the physical page in 1/72nds
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * of an inch.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private double mWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * The area of the page on which drawing will
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * be visable. The area outside of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * rectangle but on the Page generally
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * reflects the printer's hardware margins.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * The origin of the physical page is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * at (0, 0) with this rectangle provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * in that coordinate system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private Rectangle2D mImageableArea;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 /* Constructors */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * Creates a letter sized piece of paper
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * with one inch margins.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public Paper() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        mHeight = LETTER_HEIGHT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        mWidth = LETTER_WIDTH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        mImageableArea = new Rectangle2D.Double(INCH, INCH,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                                                mWidth - 2 * INCH,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                                                mHeight - 2 * INCH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 /* Instance Methods */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * Creates a copy of this <code>Paper</code> with the same contents
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * as this <code>Paper</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * @return a copy of this <code>Paper</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        Paper newPaper;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            /* It's okay to copy the reference to the imageable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
             * area into the clone since we always return a copy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
             * of the imageable area when asked for it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            newPaper = (Paper) super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        } catch (CloneNotSupportedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            newPaper = null;    // should never happen.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        return newPaper;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * Returns the height of the page in 1/72nds of an inch.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @return the height of the page described by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *          <code>Paper</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    public double getHeight() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        return mHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * Sets the width and height of this <code>Paper</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * object, which represents the properties of the page onto
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * which printing occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * The dimensions are supplied in 1/72nds of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * an inch.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @param width the value to which to set this <code>Paper</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * object's width
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @param height the value to which to set this <code>Paper</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * object's height
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    public void setSize(double width, double height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        mWidth = width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        mHeight = height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * Returns the width of the page in 1/72nds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * of an inch.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @return the width of the page described by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * <code>Paper</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    public double getWidth() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        return mWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * Sets the imageable area of this <code>Paper</code>.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * imageable area is the area on the page in which printing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @param x the X coordinate to which to set the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * upper-left corner of the imageable area of this <code>Paper</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @param y the Y coordinate to which to set the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * upper-left corner of the imageable area of this <code>Paper</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * @param width the value to which to set the width of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * imageable area of this <code>Paper</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * @param height the value to which to set the height of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * imageable area of this <code>Paper</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    public void setImageableArea(double x, double y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                                 double width, double height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        mImageableArea = new Rectangle2D.Double(x, y, width,height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * Returns the x coordinate of the upper-left corner of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * <code>Paper</code> object's imageable area.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @return the x coordinate of the imageable area.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    public double getImageableX() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        return mImageableArea.getX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * Returns the y coordinate of the upper-left corner of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * <code>Paper</code> object's imageable area.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * @return the y coordinate of the imageable area.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    public double getImageableY() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        return mImageableArea.getY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * Returns the width of this <code>Paper</code> object's imageable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * area.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * @return the width of the imageable area.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    public double getImageableWidth() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        return mImageableArea.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * Returns the height of this <code>Paper</code> object's imageable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * area.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * @return the height of the imageable area.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    public double getImageableHeight() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        return mImageableArea.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
}