src/share/classes/java/awt/Graphics2D.java

Print this page


   1 /*
   2  * Copyright (c) 1996, 2005, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  27 
  28 import java.awt.RenderingHints.Key;
  29 import java.awt.geom.AffineTransform;
  30 import java.awt.image.ImageObserver;
  31 import java.awt.image.BufferedImageOp;
  32 import java.awt.image.BufferedImage;
  33 import java.awt.image.RenderedImage;
  34 import java.awt.image.renderable.RenderableImage;
  35 import java.awt.font.GlyphVector;
  36 import java.awt.font.FontRenderContext;
  37 import java.awt.font.TextAttribute;
  38 import java.text.AttributedCharacterIterator;
  39 import java.util.Map;
  40 
  41 /**
  42  * This <code>Graphics2D</code> class extends the
  43  * {@link Graphics} class to provide more sophisticated
  44  * control over geometry, coordinate transformations, color management,
  45  * and text layout.  This is the fundamental class for rendering
  46  * 2-dimensional shapes, text and images on the  Java(tm) platform.
  47  * <p>
  48  * <h2>Coordinate Spaces</h2>
  49  * All coordinates passed to a <code>Graphics2D</code> object are specified
  50  * in a device-independent coordinate system called User Space, which is
  51  * used by applications.  The <code>Graphics2D</code> object contains
  52  * an {@link AffineTransform} object as part of its rendering state
  53  * that defines how to convert coordinates from user space to
  54  * device-dependent coordinates in Device Space.
  55  * <p>
  56  * Coordinates in device space usually refer to individual device pixels
  57  * and are aligned on the infinitely thin gaps between these pixels.
  58  * Some <code>Graphics2D</code> objects can be used to capture rendering
  59  * operations for storage into a graphics metafile for playback on a
  60  * concrete device of unknown physical resolution at a later time.  Since
  61  * the resolution might not be known when the rendering operations are
  62  * captured, the <code>Graphics2D</code> <code>Transform</code> is set up
  63  * to transform user coordinates to a virtual device space that
  64  * approximates the expected resolution of the target device. Further
  65  * transformations might need to be applied at playback time if the
  66  * estimate is incorrect.
  67  * <p>


 212  * If an <code>AffineTransform</code> is passed to
 213  * {@link #drawImage(java.awt.Image, java.awt.geom.AffineTransform, java.awt.image.ImageObserver) drawImage(Image, AffineTransform, ImageObserver)},
 214  * the <code>AffineTransform</code> is used to transform the bounding
 215  * box from image space to user space. If no <code>AffineTransform</code>
 216  * is supplied, the bounding box is treated as if it is already in user space.
 217  * <li>
 218  * The bounding box of the source <code>Image</code> is transformed from user
 219  * space into device space using the current <code>Transform</code>.
 220  * Note that the result of transforming the bounding box does not
 221  * necessarily result in a rectangular region in device space.
 222  * <li>
 223  * The <code>Image</code> object determines what colors to render,
 224  * sampled according to the source to destination
 225  * coordinate mapping specified by the current <code>Transform</code> and the
 226  * optional image transform.
 227  * </ol>
 228  * </ol>
 229  *
 230  * <h2>Default Rendering Attributes</h2>
 231  * The default values for the <code>Graphics2D</code> rendering attributes are:
 232  * <dl compact>
 233  * <dt><i><code>Paint</code></i>
 234  * <dd>The color of the <code>Component</code>.
 235  * <dt><i><code>Font</code></i>
 236  * <dd>The <code>Font</code> of the <code>Component</code>.
 237  * <dt><i><code>Stroke</code></i>
 238  * <dd>A square pen with a linewidth of 1, no dashing, miter segment joins
 239  * and square end caps.
 240  * <dt><i><code>Transform</code></i>
 241  * <dd>The
 242  * {@link GraphicsConfiguration#getDefaultTransform() getDefaultTransform}
 243  * for the <code>GraphicsConfiguration</code> of the <code>Component</code>.
 244  * <dt><i><code>Composite</code></i>
 245  * <dd>The {@link AlphaComposite#SRC_OVER} rule.
 246  * <dt><i><code>Clip</code></i>
 247  * <dd>No rendering <code>Clip</code>, the output is clipped to the
 248  * <code>Component</code>.
 249  * </dl>
 250  *
 251  * <h2>Rendering Compatibility Issues</h2>
 252  * The JDK(tm) 1.1 rendering model is based on a pixelization model


 334  * process can move endpoints by up to half a pixel (usually towards
 335  * positive infinity along both axes) to promote these consistent
 336  * results.
 337  * </ul>
 338  * <p>
 339  * The following definitions of general legacy methods
 340  * perform identically to previously specified behavior under default
 341  * attribute settings:
 342  * <ul>
 343  * <li>
 344  * For <code>fill</code> operations, including <code>fillRect</code>,
 345  * <code>fillRoundRect</code>, <code>fillOval</code>,
 346  * <code>fillArc</code>, <code>fillPolygon</code>, and
 347  * <code>clearRect</code>, {@link #fill(Shape) fill} can now be called
 348  * with the desired <code>Shape</code>.  For example, when filling a
 349  * rectangle:
 350  * <pre>
 351  * fill(new Rectangle(x, y, w, h));
 352  * </pre>
 353  * is called.
 354  * <p>
 355  * <li>
 356  * Similarly, for draw operations, including <code>drawLine</code>,
 357  * <code>drawRect</code>, <code>drawRoundRect</code>,
 358  * <code>drawOval</code>, <code>drawArc</code>, <code>drawPolyline</code>,
 359  * and <code>drawPolygon</code>, {@link #draw(Shape) draw} can now be
 360  * called with the desired <code>Shape</code>.  For example, when drawing a
 361  * rectangle:
 362  * <pre>
 363  * draw(new Rectangle(x, y, w, h));
 364  * </pre>
 365  * is called.
 366  * <p>
 367  * <li>
 368  * The <code>draw3DRect</code> and <code>fill3DRect</code> methods were
 369  * implemented in terms of the <code>drawLine</code> and
 370  * <code>fillRect</code> methods in the <code>Graphics</code> class which
 371  * would predicate their behavior upon the current <code>Stroke</code>
 372  * and <code>Paint</code> objects in a <code>Graphics2D</code> context.
 373  * This class overrides those implementations with versions that use
 374  * the current <code>Color</code> exclusively, overriding the current
 375  * <code>Paint</code> and which uses <code>fillRect</code> to describe
 376  * the exact same behavior as the preexisting methods regardless of the
 377  * setting of the current <code>Stroke</code>.
 378  * </ul>
 379  * The <code>Graphics</code> class defines only the <code>setColor</code>
 380  * method to control the color to be painted.  Since the Java 2D API extends
 381  * the <code>Color</code> object to implement the new <code>Paint</code>
 382  * interface, the existing
 383  * <code>setColor</code> method is now a convenience method for setting the
 384  * current <code>Paint</code> attribute to a <code>Color</code> object.
 385  * <code>setColor(c)</code> is equivalent to <code>setPaint(c)</code>.
 386  * <p>


   1 /*
   2  * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  27 
  28 import java.awt.RenderingHints.Key;
  29 import java.awt.geom.AffineTransform;
  30 import java.awt.image.ImageObserver;
  31 import java.awt.image.BufferedImageOp;
  32 import java.awt.image.BufferedImage;
  33 import java.awt.image.RenderedImage;
  34 import java.awt.image.renderable.RenderableImage;
  35 import java.awt.font.GlyphVector;
  36 import java.awt.font.FontRenderContext;
  37 import java.awt.font.TextAttribute;
  38 import java.text.AttributedCharacterIterator;
  39 import java.util.Map;
  40 
  41 /**
  42  * This <code>Graphics2D</code> class extends the
  43  * {@link Graphics} class to provide more sophisticated
  44  * control over geometry, coordinate transformations, color management,
  45  * and text layout.  This is the fundamental class for rendering
  46  * 2-dimensional shapes, text and images on the  Java(tm) platform.
  47  *
  48  * <h2>Coordinate Spaces</h2>
  49  * All coordinates passed to a <code>Graphics2D</code> object are specified
  50  * in a device-independent coordinate system called User Space, which is
  51  * used by applications.  The <code>Graphics2D</code> object contains
  52  * an {@link AffineTransform} object as part of its rendering state
  53  * that defines how to convert coordinates from user space to
  54  * device-dependent coordinates in Device Space.
  55  * <p>
  56  * Coordinates in device space usually refer to individual device pixels
  57  * and are aligned on the infinitely thin gaps between these pixels.
  58  * Some <code>Graphics2D</code> objects can be used to capture rendering
  59  * operations for storage into a graphics metafile for playback on a
  60  * concrete device of unknown physical resolution at a later time.  Since
  61  * the resolution might not be known when the rendering operations are
  62  * captured, the <code>Graphics2D</code> <code>Transform</code> is set up
  63  * to transform user coordinates to a virtual device space that
  64  * approximates the expected resolution of the target device. Further
  65  * transformations might need to be applied at playback time if the
  66  * estimate is incorrect.
  67  * <p>


 212  * If an <code>AffineTransform</code> is passed to
 213  * {@link #drawImage(java.awt.Image, java.awt.geom.AffineTransform, java.awt.image.ImageObserver) drawImage(Image, AffineTransform, ImageObserver)},
 214  * the <code>AffineTransform</code> is used to transform the bounding
 215  * box from image space to user space. If no <code>AffineTransform</code>
 216  * is supplied, the bounding box is treated as if it is already in user space.
 217  * <li>
 218  * The bounding box of the source <code>Image</code> is transformed from user
 219  * space into device space using the current <code>Transform</code>.
 220  * Note that the result of transforming the bounding box does not
 221  * necessarily result in a rectangular region in device space.
 222  * <li>
 223  * The <code>Image</code> object determines what colors to render,
 224  * sampled according to the source to destination
 225  * coordinate mapping specified by the current <code>Transform</code> and the
 226  * optional image transform.
 227  * </ol>
 228  * </ol>
 229  *
 230  * <h2>Default Rendering Attributes</h2>
 231  * The default values for the <code>Graphics2D</code> rendering attributes are:
 232  * <dl>
 233  * <dt><i><code>Paint</code></i>
 234  * <dd>The color of the <code>Component</code>.
 235  * <dt><i><code>Font</code></i>
 236  * <dd>The <code>Font</code> of the <code>Component</code>.
 237  * <dt><i><code>Stroke</code></i>
 238  * <dd>A square pen with a linewidth of 1, no dashing, miter segment joins
 239  * and square end caps.
 240  * <dt><i><code>Transform</code></i>
 241  * <dd>The
 242  * {@link GraphicsConfiguration#getDefaultTransform() getDefaultTransform}
 243  * for the <code>GraphicsConfiguration</code> of the <code>Component</code>.
 244  * <dt><i><code>Composite</code></i>
 245  * <dd>The {@link AlphaComposite#SRC_OVER} rule.
 246  * <dt><i><code>Clip</code></i>
 247  * <dd>No rendering <code>Clip</code>, the output is clipped to the
 248  * <code>Component</code>.
 249  * </dl>
 250  *
 251  * <h2>Rendering Compatibility Issues</h2>
 252  * The JDK(tm) 1.1 rendering model is based on a pixelization model


 334  * process can move endpoints by up to half a pixel (usually towards
 335  * positive infinity along both axes) to promote these consistent
 336  * results.
 337  * </ul>
 338  * <p>
 339  * The following definitions of general legacy methods
 340  * perform identically to previously specified behavior under default
 341  * attribute settings:
 342  * <ul>
 343  * <li>
 344  * For <code>fill</code> operations, including <code>fillRect</code>,
 345  * <code>fillRoundRect</code>, <code>fillOval</code>,
 346  * <code>fillArc</code>, <code>fillPolygon</code>, and
 347  * <code>clearRect</code>, {@link #fill(Shape) fill} can now be called
 348  * with the desired <code>Shape</code>.  For example, when filling a
 349  * rectangle:
 350  * <pre>
 351  * fill(new Rectangle(x, y, w, h));
 352  * </pre>
 353  * is called.
 354  *
 355  * <li>
 356  * Similarly, for draw operations, including <code>drawLine</code>,
 357  * <code>drawRect</code>, <code>drawRoundRect</code>,
 358  * <code>drawOval</code>, <code>drawArc</code>, <code>drawPolyline</code>,
 359  * and <code>drawPolygon</code>, {@link #draw(Shape) draw} can now be
 360  * called with the desired <code>Shape</code>.  For example, when drawing a
 361  * rectangle:
 362  * <pre>
 363  * draw(new Rectangle(x, y, w, h));
 364  * </pre>
 365  * is called.
 366  *
 367  * <li>
 368  * The <code>draw3DRect</code> and <code>fill3DRect</code> methods were
 369  * implemented in terms of the <code>drawLine</code> and
 370  * <code>fillRect</code> methods in the <code>Graphics</code> class which
 371  * would predicate their behavior upon the current <code>Stroke</code>
 372  * and <code>Paint</code> objects in a <code>Graphics2D</code> context.
 373  * This class overrides those implementations with versions that use
 374  * the current <code>Color</code> exclusively, overriding the current
 375  * <code>Paint</code> and which uses <code>fillRect</code> to describe
 376  * the exact same behavior as the preexisting methods regardless of the
 377  * setting of the current <code>Stroke</code>.
 378  * </ul>
 379  * The <code>Graphics</code> class defines only the <code>setColor</code>
 380  * method to control the color to be painted.  Since the Java 2D API extends
 381  * the <code>Color</code> object to implement the new <code>Paint</code>
 382  * interface, the existing
 383  * <code>setColor</code> method is now a convenience method for setting the
 384  * current <code>Paint</code> attribute to a <code>Color</code> object.
 385  * <code>setColor(c)</code> is equivalent to <code>setPaint(c)</code>.
 386  * <p>