jdk/src/share/classes/java/awt/TexturePaint.java
author tbell
Wed, 07 Oct 2009 14:15:01 -0700
changeset 3965 63aae8ce7f47
parent 715 f16baef3a20e
child 5506 202f599c92aa
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 436
diff changeset
     2
 * Copyright 1997-2008 Sun Microsystems, Inc.  All Rights Reserved.
2
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;
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
import java.awt.geom.AffineTransform;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.image.BufferedImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.image.ColorModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * The <code>TexturePaint</code> class provides a way to fill a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * {@link Shape} with a texture that is specified as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * a {@link BufferedImage}. The size of the <code>BufferedImage</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * object should be small because the <code>BufferedImage</code> data
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * is copied by the <code>TexturePaint</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * At construction time, the texture is anchored to the upper
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * left corner of a {@link Rectangle2D} that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * specified in user space.  Texture is computed for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * locations in the device space by conceptually replicating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * specified <code>Rectangle2D</code> infinitely in all directions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * in user space and mapping the <code>BufferedImage</code> to each
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * replicated <code>Rectangle2D</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @see Paint
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * @see Graphics2D#setPaint
436
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
    48
 * @version 1.48, 06/05/07
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
public class TexturePaint implements Paint {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    BufferedImage bufImg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    double tx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    double ty;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    double sx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    double sy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * Constructs a <code>TexturePaint</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * @param txtr the <code>BufferedImage</code> object with the texture
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * used for painting
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * @param anchor the <code>Rectangle2D</code> in user space used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * anchor and replicate the texture
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    public TexturePaint(BufferedImage txtr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                        Rectangle2D anchor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        this.bufImg = txtr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        this.tx = anchor.getX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        this.ty = anchor.getY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        this.sx = anchor.getWidth() / bufImg.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        this.sy = anchor.getHeight() / bufImg.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * Returns the <code>BufferedImage</code> texture used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * fill the shapes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * @return a <code>BufferedImage</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    public BufferedImage getImage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        return bufImg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * Returns a copy of the anchor rectangle which positions and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * sizes the textured image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * @return the <code>Rectangle2D</code> used to anchor and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * size this <code>TexturePaint</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    public Rectangle2D getAnchorRect() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        return new Rectangle2D.Double(tx, ty,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                                      sx * bufImg.getWidth(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                                      sy * bufImg.getHeight());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    /**
436
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
    97
     * Creates and returns a {@link PaintContext} used to
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
    98
     * generate a tiled image pattern.
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
    99
     * See the {@link Paint#createContext specification} of the
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
   100
     * method in the {@link Paint} interface for information
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
   101
     * on null parameter handling.
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
   102
     *
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
   103
     * @param cm the preferred {@link ColorModel} which represents the most convenient
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
   104
     *           format for the caller to receive the pixel data, or {@code null}
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
   105
     *           if there is no preference.
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
   106
     * @param deviceBounds the device space bounding box
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
   107
     *                     of the graphics primitive being rendered.
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
   108
     * @param userBounds the user space bounding box
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
   109
     *                   of the graphics primitive being rendered.
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
   110
     * @param xform the {@link AffineTransform} from user
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
   111
     *              space into device space.
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
   112
     * @param hints the set of hints that the context object can use to
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
   113
     *              choose between rendering alternatives.
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
   114
     * @return the {@code PaintContext} for
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
   115
     *         generating color patterns.
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
   116
     * @see Paint
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @see PaintContext
436
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
   118
     * @see ColorModel
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
   119
     * @see Rectangle
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
   120
     * @see Rectangle2D
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
   121
     * @see AffineTransform
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
   122
     * @see RenderingHints
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    public PaintContext createContext(ColorModel cm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                                      Rectangle deviceBounds,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                                      Rectangle2D userBounds,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                                      AffineTransform xform,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                                      RenderingHints hints) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        if (xform == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            xform = new AffineTransform();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            xform = (AffineTransform) xform.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        xform.translate(tx, ty);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        xform.scale(sx, sy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        return TexturePaintContext.getContext(bufImg, xform, hints,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                                              deviceBounds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * Returns the transparency mode for this <code>TexturePaint</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @return the transparency mode for this <code>TexturePaint</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * as an integer value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @see Transparency
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    public int getTransparency() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        return (bufImg.getColorModel()).getTransparency();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
}