Draws an image on top of a component by doing a 3x3 grid stretch of the image using the specified insets. - Java 2D Graphics

Java examples for 2D Graphics:Image

Description

Draws an image on top of a component by doing a 3x3 grid stretch of the image using the specified insets.

Demo Code

/*//www . java  2  s .co m
 * $Id: ColorUtil.java 3742 2010-08-05 03:25:09Z kschaefe $
 *
 * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
 * Santa Clara, California 95054, U.S.A. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */
//package com.java2s;

import java.awt.Graphics;
import java.awt.Insets;

import java.awt.image.BufferedImage;
import javax.swing.JComponent;

public class Main {
    /**
     * Draws an image on top of a component by doing a 3x3 grid stretch of the image
     * using the specified insets.
     * <p>
     * TODO this is image related; move to GraphicsUtilities
     */
    public static void tileStretchPaint(Graphics g, JComponent comp,
            BufferedImage img, Insets ins) {

        int left = ins.left;
        int right = ins.right;
        int top = ins.top;
        int bottom = ins.bottom;

        // top
        g.drawImage(img, 0, 0, left, top, 0, 0, left, top, null);
        g.drawImage(img, left, 0, comp.getWidth() - right, top, left, 0,
                img.getWidth() - right, top, null);
        g.drawImage(img, comp.getWidth() - right, 0, comp.getWidth(), top,
                img.getWidth() - right, 0, img.getWidth(), top, null);

        // middle
        g.drawImage(img, 0, top, left, comp.getHeight() - bottom, 0, top,
                left, img.getHeight() - bottom, null);

        g.drawImage(img, left, top, comp.getWidth() - right,
                comp.getHeight() - bottom, left, top, img.getWidth()
                        - right, img.getHeight() - bottom, null);

        g.drawImage(img, comp.getWidth() - right, top, comp.getWidth(),
                comp.getHeight() - bottom, img.getWidth() - right, top,
                img.getWidth(), img.getHeight() - bottom, null);

        // bottom
        g.drawImage(img, 0, comp.getHeight() - bottom, left,
                comp.getHeight(), 0, img.getHeight() - bottom, left,
                img.getHeight(), null);
        g.drawImage(img, left, comp.getHeight() - bottom, comp.getWidth()
                - right, comp.getHeight(), left, img.getHeight() - bottom,
                img.getWidth() - right, img.getHeight(), null);
        g.drawImage(img, comp.getWidth() - right,
                comp.getHeight() - bottom, comp.getWidth(),
                comp.getHeight(), img.getWidth() - right, img.getHeight()
                        - bottom, img.getWidth(), img.getHeight(), null);
    }
}

Related Tutorials