pixmapengine.java

来自「JAVA的一些源码 JAVA2 STANDARD EDITION DEVELO」· Java 代码 · 共 548 行 · 第 1/2 页

JAVA
548
字号
     *        GTKConstants.RIGHT, GTKConstants.TOP or GTKConstants.BOTTOM     * @param size Size of the gap, either width or height, dependant upon     *        gapSide     * @param gapStart Starting location of the gap. The axis the gap is     *        on is dictated by the gapSide     * @param gapSize size of the gap     */    private void paintGapImage(Graphics g, int x, int y, int w, int h,                               Image image, Insets insets, int gapSide,                               int size, int gapStart, int gapSize) {        if (image != null && gapSize > 0) {            switch(gapSide) {            case LEFT:                paintImage(g, x, y + gapStart, Math.min(w, size),                      Math.min(h - y - gapStart, gapSize), image,insets, true,                           false, true);                break;            case RIGHT:                paintImage(g, x + w - Math.min(w, size),                           y + gapStart, Math.min(w, size),                           Math.min(h - y - gapStart, gapSize), image,                           insets, true, false, true);                break;            case TOP:                paintImage(g, x + gapStart, y, Math.min(w - x - gapStart,                           gapSize), Math.min(h, size), image, insets, true,                           false, true);                break;            case BOTTOM:                paintImage(g, x + gapStart, y + h - Math.min(h, size),                           Math.min(w - x - gapStart, gapSize),                           Math.min(h, size), image, insets, true, false,true);                break;            }         }    }    /**     * Paints the image and overlay image from the passed in style.     *     * @param g Graphics object to paint to     * @param x X origin     * @param y Y origin     * @param w Width to draw to     * @param h Height to draw to     * @param info Used to fetch image, insets and overlay image from     */    private boolean paintPixmap(Graphics g, int x, int y, int w, int h,                                PixmapStyle.Info info, boolean drawCenter) {        if (info != null) {            Rectangle clip = g.getClipBounds();            _clipX1 = clip.x;            _clipY1 = clip.y;            _clipX2 = _clipX1 + clip.width;            _clipY2 = _clipY1 + clip.height;            paintImage(g, x, y, w, h, info.getImage(), info.getImageInsets(),                       info.getStretch(), false, drawCenter);            paintImage(g, x, y, w, h, info.getOverlayImage(),                       info.getOverlayInsets(), info.getOverlayStretch(),                       true, drawCenter);            return true;         }        return false;    }    /**     * Paints the image in the specified region.     *     * @param g Graphics object to paint to     * @param x X origin     * @param y Y origin     * @param w Width to draw to     * @param h Height to draw to     * @param image Image to render     * @param insets Insets used to determine portion of image that is fixed.     */    private void paintImage(Graphics g, int x, int y, int w, int h,                            Image image, Insets insets, boolean stretch,                            boolean overlay, boolean drawCenter) {        if (image == null) {            return;        }        if (insets == null) {            insets = GTKPainter.EMPTY_INSETS;        }        int iw = image.getWidth(null);        int ih = image.getHeight(null);        if (iw <= 0 || ih <= 0) {            return;        }        Object lastHint;        Object renderingHint = RENDERING_HINT;        if (renderingHint != null && stretch) {            Graphics2D g2 = (Graphics2D)g;            lastHint = g2.getRenderingHint(RenderingHints.KEY_INTERPOLATION);            if (lastHint == null) {                lastHint = RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR;            }            g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,                                renderingHint);        }        else {            lastHint = null;        }        if (!stretch) {            if (overlay) {                g.drawImage(image, x + w / 2 - iw / 2, y + h / 2 - ih / 2,                            null);            }            else {                int lastIY = 0;                for (int yCounter = y, maxY = y + h; yCounter < maxY;                         yCounter += (ih - lastIY), lastIY = 0) {                    int lastIX = 0;                    for (int xCounter = x, maxX = x + w; xCounter < maxX;                             xCounter += (iw - lastIX), lastIX = 0) {                        int dx2 = Math.min(maxX, xCounter + iw - lastIX);                        int dy2 = Math.min(maxY, yCounter + ih - lastIY);                        if (intersectsClip(xCounter, yCounter, dx2, dy2)) {                            g.drawImage(image, xCounter, yCounter, dx2, dy2,                                        lastIX, lastIY, lastIX + dx2 -xCounter,                                        lastIY + dy2 - yCounter, null);                        }                    }                }            }        }        else {            int it = insets.top;            int il = insets.left;            int ib = insets.bottom;            int ir = insets.right;            // Constrain the insets to the size of the image            if (it + ib >= ih) {                ib = it = Math.max(0, ih / 2 - 1);            }            if (il + ir >= iw) {                il = ir = Math.max(0, iw / 2 - 1);            }            // Constrain the insets to the size of the region we're painting            // in.            if (it + ib > h) {                it = ib = Math.max(2, h / 2 - 1);            }            if (il + ir > w) {                il = ir = Math.max(2, w / 2 - 1);            }            // left            if (il > 0 && it + ib < ih) {                drawChunk(image, g, stretch, x, y + it, x + il, y + h - ib, 0,                          it, il, ih - ib, false);            }            // top left            if (il > 0 && it > 0) {                g.drawImage(image, x, y, x + il, y + it, 0, 0, il, it, null);            }            // top            if (it > 0 && il + ir < iw) {                drawChunk(image, g, stretch, x + il, y, x + w - ir, y + it,                          il, 0, iw - ir, it, true);            }            // top right            if (ir < iw && it > 0) {                g.drawImage(image, x + w - ir, y, x + w, y + it, iw - ir, 0,                            iw, it, null);            }            // right            if (ir < iw && it + ib < ih) {                drawChunk(image, g, stretch, x + w - ir, y + it, x + w,                          y + h - ib, iw - ir, it, iw, ih - ib, false);            }            // bottom right            if (ir < iw && ib < ih) {                g.drawImage(image, x + w - ir, y + h - ib, x + w, y + h,                            iw - ir, ih - ib, iw, ih, null);            }            // bottom            if (il + ir < iw && ib > 0) {                drawChunk(image, g, stretch, x + il, y + h - ib, x + w - ir,                          y + h, il, ih - ib, iw - ir, ih, true);            }            // bottom left            if (il > 0 && ib > 0) {                g.drawImage(image, x, y + h - ib, x + il,                            y + h, 0, ih - ib, il, ih, null);            }            // center            if (drawCenter && il + ir < iw && it + ib < ih) {                g.drawImage(image, x + il, y + it, x + w - ir, y + h - ib,                            il, it, iw - ir, ih - ib, null);            }        }        if (lastHint != null) {            ((Graphics2D)g).setRenderingHint(RenderingHints.KEY_INTERPOLATION,                                             lastHint);        }    }    /**     * Draws a portion of an image, stretched or tiled.     *     * @param image Image to render.     * @param g Graphics to render to     * @param stretch Whether the image should be stretched or timed in the     *                provided space.     * @param dx1 X origin to draw to     * @param dy1 Y origin to draw to     * @param dx2 End x location to draw to     * @param dy2 End y location to draw to     * @param sx1 X origin to draw from     * @param sy1 Y origin to draw from     * @param sx2 Max x location to draw from     * @param sy2 Max y location to draw from     * @param xDirection Used if the image is not stretched. If true it     *        indicates the image should be tiled along the x axis.     */    private void drawChunk(Image image, Graphics g, boolean stretch,                           int dx1, int dy1, int dx2, int dy2, int sx1,                           int sy1, int sx2, int sy2,                           boolean xDirection) {        if (dx2 - dx1 <= 0 || dy2 - dy1 <= 0 ||                              !intersectsClip(dx1, dy1, dx2, dy2)) {            // Bogus location, nothing to paint            return;        }        if (stretch) {            g.drawImage(image, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, null);        }        else {            int xSize = sx2 - sx1;            int ySize = sy2 - sy1;            int deltaX;            int deltaY;            if (xDirection) {                deltaX = xSize;                deltaY = 0;            }            else {                deltaX = 0;                deltaY = ySize;            }            while (dx1 < dx2 && dy1 < dy2) {                int newDX2 = Math.min(dx2, dx1 + xSize);                int newDY2 = Math.min(dy2, dy1 + ySize);                if (intersectsClip(dx1, dy1, newDX2, newDY2)) {                    g.drawImage(image, dx1, dy1, newDX2, newDY2,                                sx1, sy1, sx1 + newDX2 - dx1,                                sy1 + newDY2 - dy1, null);                }                dx1 += deltaX;                dy1 += deltaY;            }        }    }    /**     * Returns true if the passed in region intersects the clip.     */    private boolean intersectsClip(int x1, int y1, int x2, int y2) {        return ((x2 < x1 || x2 > _clipX1) &&                (y2 < y1 || y2 > _clipY1) &&                (_clipX2 < _clipX1 || _clipX2 > x1) &&                (_clipY2 < _clipY1 || _clipY2 > y1));    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?