1896 lines · 75.9 KB · Java
RawDownload
     * The image is drawn inside the specified rectangle of this     * graphics context's coordinate space, and is scaled if     * necessary. Transparent pixels are drawn in the specified     * background color.     * This operation is equivalent to filling a rectangle of the     * width and height of the specified image with the given color and then     * drawing the image on top of it, but possibly more efficient.     * <p>     * This method returns immediately in all cases, even if the     * entire image has not yet been scaled, dithered, and converted     * for the current output device.     * If the current output representation is not yet complete then     * <code>drawImage</code> returns <code>false</code>. As more of     * the image becomes available, the process that draws the image notifies     * the specified image observer.     * <p>     * A scaled version of an image will not necessarily be     * available immediately just because an unscaled version of the     * image has been constructed for this output device.  Each size of     * the image may be cached separately and generated from the original     * data in a separate image production sequence.     * @param    img       the specified image to be drawn.     *                     This method does nothing if <code>img</code> is null.     * @param    x         the <i>x</i> coordinate.     * @param    y         the <i>y</i> coordinate.     * @param    width     the width of the rectangle.     * @param    height    the height of the rectangle.     * @param    bgcolor   the background color to paint under the     *                         non-opaque portions of the image.     * @param    observer    object to be notified as more of     *                          the image is converted.     * @see      java.awt.Image     * @see      java.awt.image.ImageObserver     * @see      java.awt.image.ImageObserver#imageUpdate(java.awt.Image, int, int, int, int, int)     * @since    JDK1.0     */    public boolean drawImage(Image img, int x, int y,                             int width, int height,                             Color bgcolor,                             ImageObserver observer) {         if (img == null) {            return true;        }         boolean result;        int srcWidth = img.getWidth(null);        int srcHeight = img.getHeight(null);         if (srcWidth < 0 || srcHeight < 0) {            result = false;        } else {            result = drawImage(img,                         x, y, x + width, y + height,                         0, 0, srcWidth, srcHeight,                         observer);        }         return result;    }     /**     * Draws as much of the specified area of the specified image as is     * currently available, scaling it on the fly to fit inside the     * specified area of the destination drawable surface. Transparent pixels     * do not affect whatever pixels are already there.     * <p>     * This method returns immediately in all cases, even if the     * image area to be drawn has not yet been scaled, dithered, and converted     * for the current output device.     * If the current output representation is not yet complete then     * <code>drawImage</code> returns <code>false</code>. As more of     * the image becomes available, the process that draws the image notifies     * the specified image observer.     * <p>     * This method always uses the unscaled version of the image     * to render the scaled rectangle and performs the required     * scaling on the fly. It does not use a cached, scaled version     * of the image for this operation. Scaling of the image from source     * to destination is performed such that the first coordinate     * of the source rectangle is mapped to the first coordinate of     * the destination rectangle, and the second source coordinate is     * mapped to the second destination coordinate. The subimage is     * scaled and flipped as needed to preserve those mappings.     * @param       img the specified image to be drawn     * @param       dx1 the <i>x</i> coordinate of the first corner of the     *                    destination rectangle.     * @param       dy1 the <i>y</i> coordinate of the first corner of the     *                    destination rectangle.     * @param       dx2 the <i>x</i> coordinate of the second corner of the     *                    destination rectangle.     * @param       dy2 the <i>y</i> coordinate of the second corner of the     *                    destination rectangle.     * @param       sx1 the <i>x</i> coordinate of the first corner of the     *                    source rectangle.     * @param       sy1 the <i>y</i> coordinate of the first corner of the     *                    source rectangle.     * @param       sx2 the <i>x</i> coordinate of the second corner of the     *                    source rectangle.     * @param       sy2 the <i>y</i> coordinate of the second corner of the     *                    source rectangle.     * @param       observer object to be notified as more of the image is     *                    scaled and converted.     * @see         java.awt.Image     * @see         java.awt.image.ImageObserver     * @see         java.awt.image.ImageObserver#imageUpdate(java.awt.Image, int, int, int, int, int)     * @since       JDK1.1     */    public boolean drawImage(Image img,                             int dx1, int dy1, int dx2, int dy2,                             int sx1, int sy1, int sx2, int sy2,                             ImageObserver observer) {         return drawImage(img,                         dx1, dy1, dx2, dy2,                         sx1, sy1, sx2, sy2,                         null, observer);    }     /**     * Draws as much of the specified area of the specified image as is     * currently available, scaling it on the fly to fit inside the     * specified area of the destination drawable surface.     * <p>     * Transparent pixels are drawn in the specified background color.     * This operation is equivalent to filling a rectangle of the     * width and height of the specified image with the given color and then     * drawing the image on top of it, but possibly more efficient.     * <p>     * This method returns immediately in all cases, even if the     * image area to be drawn has not yet been scaled, dithered, and converted     * for the current output device.     * If the current output representation is not yet complete then     * <code>drawImage</code> returns <code>false</code>. As more of     * the image becomes available, the process that draws the image notifies     * the specified image observer.     * <p>     * This method always uses the unscaled version of the image     * to render the scaled rectangle and performs the required     * scaling on the fly. It does not use a cached, scaled version     * of the image for this operation. Scaling of the image from source     * to destination is performed such that the first coordinate     * of the source rectangle is mapped to the first coordinate of     * the destination rectangle, and the second source coordinate is     * mapped to the second destination coordinate. The subimage is     * scaled and flipped as needed to preserve those mappings.     * @param       img the specified image to be drawn     *                  This method does nothing if <code>img</code> is null.     * @param       dx1 the <i>x</i> coordinate of the first corner of the     *                    destination rectangle.     * @param       dy1 the <i>y</i> coordinate of the first corner of the     *                    destination rectangle.     * @param       dx2 the <i>x</i> coordinate of the second corner of the     *                    destination rectangle.     * @param       dy2 the <i>y</i> coordinate of the second corner of the     *                    destination rectangle.     * @param       sx1 the <i>x</i> coordinate of the first corner of the     *                    source rectangle.     * @param       sy1 the <i>y</i> coordinate of the first corner of the     *                    source rectangle.     * @param       sx2 the <i>x</i> coordinate of the second corner of the     *                    source rectangle.     * @param       sy2 the <i>y</i> coordinate of the second corner of the     *                    source rectangle.     * @param       bgcolor the background color to paint under the     *                    non-opaque portions of the image.     * @param       observer object to be notified as more of the image is     *                    scaled and converted.     * @see         java.awt.Image     * @see         java.awt.image.ImageObserver     * @see         java.awt.image.ImageObserver#imageUpdate(java.awt.Image, int, int, int, int, int)     * @since       JDK1.1     */    public boolean drawImage(Image img,                             int dx1, int dy1, int dx2, int dy2,                             int sx1, int sy1, int sx2, int sy2,                             Color bgcolor,                             ImageObserver observer) {         if (img == null) {            return true;        }        int imgWidth = img.getWidth(null);        int imgHeight = img.getHeight(null);         if (imgWidth < 0 || imgHeight < 0) {            return true;        }         int srcWidth = sx2 - sx1;        int srcHeight = sy2 - sy1;         /* Create a transform which describes the changes         * from the source coordinates to the destination         * coordinates. The scaling is determined by the         * ratio of the two rectangles, while the translation         * comes from the difference of their origins.         */        float scalex = (float) (dx2 - dx1) / srcWidth;        float scaley = (float) (dy2 - dy1) / srcHeight;        AffineTransform xForm            = new AffineTransform(scalex,                                  0,                                  0,                                  scaley,                                  dx1 - (sx1 * scalex),                                  dy1 - (sy1 * scaley));         /* drawImageToPlatform needs the top-left of the source area and         * a positive width and height. The xform describes how to map         * src->dest, so that information is not lost.         */        int tmp=0;        if (sx2 < sx1) {            tmp = sx1;            sx1 = sx2;            sx2 = tmp;        }        if (sy2 < sy1) {            tmp = sy1;            sy1 = sy2;            sy2 = tmp;        }         /* if src area is beyond the bounds of the image, we must clip it.         * The transform is based on the specified area, not the clipped one.         */        if (sx1 < 0) {            sx1 = 0;        } else if (sx1 > imgWidth) { // empty srcArea, nothing to draw            sx1 = imgWidth;        }        if (sx2 < 0) { // empty srcArea, nothing to draw            sx2 = 0;        } else if (sx2 > imgWidth) {            sx2 = imgWidth;        }        if (sy1 < 0) {            sy1 = 0;        } else if (sy1 > imgHeight) { // empty srcArea            sy1 = imgHeight;        }        if (sy2 < 0) {  // empty srcArea            sy2 = 0;        } else if (sy2 > imgHeight) {            sy2 = imgHeight;        }         srcWidth =  sx2 - sx1;        srcHeight = sy2 - sy1;         if (srcWidth <= 0 || srcHeight <= 0) {            return true;        }         return drawImageToPlatform(img, xForm, bgcolor,                                   sx1, sy1, srcWidth, srcHeight, false);      }     /**     * Draws an image, applying a transform from image space into user space     * before drawing.     * The transformation from user space into device space is done with     * the current transform in the Graphics2D.     * The given transformation is applied to the image before the     * transform attribute in the Graphics2D state is applied.     * The rendering attributes applied include the clip, transform,     * and composite attributes. Note that the result is     * undefined, if the given transform is noninvertible.     * @param img The image to be drawn.     *            This method does nothing if <code>img</code> is null.     * @param xform The transformation from image space into user space.     * @param obs The image observer to be notified as more of the image     * is converted.     * @see #transform     * @see #setTransform     * @see #setComposite     * @see #clip     * @see #setClip     */    public boolean drawImage(Image img,                             AffineTransform xform,                             ImageObserver obs) {         if (img == null) {            return true;        }         boolean result;        int srcWidth = img.getWidth(null);        int srcHeight = img.getHeight(null);         if (srcWidth < 0 || srcHeight < 0) {            result = false;        } else {            result = drawImageToPlatform(img, xform, null,                                         0, 0, srcWidth, srcHeight, false);        }         return result;    }     /**     * Draws a BufferedImage that is filtered with a BufferedImageOp.     * The rendering attributes applied include the clip, transform     * and composite attributes.  This is equivalent to:     * <pre>     * img1 = op.filter(img, null);     * drawImage(img1, new AffineTransform(1f,0f,0f,1f,x,y), null);     * </pre>     * @param op The filter to be applied to the image before drawing.     * @param img The BufferedImage to be drawn.     *            This method does nothing if <code>img</code> is null.     * @param x,y The location in user space where the image should be drawn.     * @see #transform     * @see #setTransform     * @see #setComposite     * @see #clip     * @see #setClip     */    public void drawImage(BufferedImage img,                          BufferedImageOp op,                          int x,                          int y) {         if (img == null) {            return;        }         int srcWidth = img.getWidth(null);        int srcHeight = img.getHeight(null);         if (op != null) {            img = op.filter(img, null);        }        if (srcWidth <= 0 || srcHeight <= 0) {            return;        } else {            AffineTransform xform = new AffineTransform(1f,0f,0f,1f,x,y);            drawImageToPlatform(img, xform, null,                                0, 0, srcWidth, srcHeight, false);        }     }     /**     * Draws an image, applying a transform from image space into user space     * before drawing.     * The transformation from user space into device space is done with     * the current transform in the Graphics2D.     * The given transformation is applied to the image before the     * transform attribute in the Graphics2D state is applied.     * The rendering attributes applied include the clip, transform,     * and composite attributes. Note that the result is     * undefined, if the given transform is noninvertible.     * @param img The image to be drawn.     *            This method does nothing if <code>img</code> is null.     * @param xform The transformation from image space into user space.     * @see #transform     * @see #setTransform     * @see #setComposite     * @see #clip     * @see #setClip     */    public void drawRenderedImage(RenderedImage img,                                  AffineTransform xform) {         if (img == null) {            return;        }         BufferedImage bufferedImage = null;        int srcWidth = img.getWidth();        int srcHeight = img.getHeight();         if (srcWidth <= 0 || srcHeight <= 0) {            return;        }         if (img instanceof BufferedImage) {            bufferedImage = (BufferedImage) img;        } else {            bufferedImage = new BufferedImage(srcWidth, srcHeight,                                              BufferedImage.TYPE_INT_ARGB);            Graphics2D imageGraphics = bufferedImage.createGraphics();            imageGraphics.drawRenderedImage(img, xform);        }         drawImageToPlatform(bufferedImage, xform, null,                            0, 0, srcWidth, srcHeight, false);     } }

Lines 1501–1896 of 1896. Previous lines