jdk/src/share/classes/sun/print/PrinterGraphicsConfig.java
author dav
Mon, 07 Apr 2008 14:53:51 +0400
changeset 438 2ae294e4518c
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6613529: Avoid duplicate object creation within JDK packages Summary: avoid using constructors when unique values are not necessary Reviewed-by: volk, igor, peterz
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 2004-2007 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 sun.print;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.GraphicsConfiguration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.GraphicsDevice;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.Rectangle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.Transparency;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.awt.geom.AffineTransform;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.awt.image.BufferedImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.awt.image.ColorModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.awt.image.DirectColorModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
public class PrinterGraphicsConfig extends GraphicsConfiguration {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    static ColorModel theModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    GraphicsDevice gd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    int pageWidth, pageHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    AffineTransform deviceTransform;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    public PrinterGraphicsConfig(String printerID, AffineTransform deviceTx,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
                                 int pageWid, int pageHgt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        this.pageWidth = pageWid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        this.pageHeight = pageHgt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        this.deviceTransform = deviceTx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        this.gd = new PrinterGraphicsDevice(this, printerID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * Return the graphics device associated with this configuration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    public GraphicsDevice getDevice() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        return gd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * Returns the color model associated with this configuration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    public ColorModel getColorModel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        if (theModel == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            BufferedImage bufImg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                new BufferedImage(1,1, BufferedImage.TYPE_3BYTE_BGR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            theModel = bufImg.getColorModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        return theModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * Returns the color model associated with this configuration that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * supports the specified transparency.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    public ColorModel getColorModel(int transparency) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        switch (transparency) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        case Transparency.OPAQUE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            return getColorModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        case Transparency.BITMASK:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            return new DirectColorModel(25, 0xff0000, 0xff00, 0xff, 0x1000000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        case Transparency.TRANSLUCENT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            return ColorModel.getRGBdefault();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * Returns the default Transform for this configuration.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * Transform is typically the Identity transform for most normal
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * screens.  Device coordinates for screen and printer devices will
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * have the origin in the upper left-hand corner of the target region of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * the device, with X coordinates
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * increasing to the right and Y coordinates increasing downwards.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * For image buffers, this Transform will be the Identity transform.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    public AffineTransform getDefaultTransform() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        return new AffineTransform(deviceTransform);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * Returns a Transform that can be composed with the default Transform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * of a Graphics2D so that 72 units in user space will equal 1 inch
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * in device space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * Given a Graphics2D, g, one can reset the transformation to create
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * such a mapping by using the following pseudocode:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     *      GraphicsConfiguration gc = g.getGraphicsConfiguration();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     *      g.setTransform(gc.getDefaultTransform());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *      g.transform(gc.getNormalizingTransform());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * Note that sometimes this Transform will be identity (e.g. for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * printers or metafile output) and that this Transform is only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * as accurate as the information supplied by the underlying system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * For image buffers, this Transform will be the Identity transform,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * since there is no valid distance measurement.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public AffineTransform getNormalizingTransform() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        return new AffineTransform();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    public Rectangle getBounds() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        return new Rectangle(0, 0, pageWidth, pageHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
}