Java ImageIcon drawImageBorder(Graphics g, ImageIcon img, Rectangle rect, Insets ins, boolean drawCenter)

Here you can find the source of drawImageBorder(Graphics g, ImageIcon img, Rectangle rect, Insets ins, boolean drawCenter)

Description

Draws a border based on an image.

License

Open Source License

Declaration

public static void drawImageBorder(Graphics g, ImageIcon img,
        Rectangle rect, Insets ins, boolean drawCenter) 

Method Source Code

//package com.java2s;
import javax.swing.*;

import java.awt.*;

public class Main {
    /**//  w w  w.j  a va 2 s.c  o  m
     * Draws a border based on an image. The image can be divided into nine different areas. Each area size is
     * determined by the insets.
     */
    public static void drawImageBorder(Graphics g, ImageIcon img,
            Rectangle rect, Insets ins, boolean drawCenter) {
        int left = ins.left;
        int right = ins.right;
        int top = ins.top;
        int bottom = ins.bottom;
        int x = rect.x;
        int y = rect.y;
        int w = rect.width;
        int h = rect.height;

        // top
        g.drawImage(img.getImage(), x, y, x + left, y + top, 0, 0, left,
                top, null);
        g.drawImage(img.getImage(), x + left, y, x + w - right, y + top,
                left, 0, img.getIconWidth() - right, top, null);
        g.drawImage(img.getImage(), x + w - right, y, x + w, y + top,
                img.getIconWidth() - right, 0, img.getIconWidth(), top,
                null);

        // middle
        g.drawImage(img.getImage(), x, y + top, x + left, y + h - bottom,
                0, top, left, img.getIconHeight() - bottom, null);
        g.drawImage(img.getImage(), x + left, y + top, x + w - right, y + h
                - bottom, left, top, img.getIconWidth() - right,
                img.getIconHeight() - bottom, null);
        g.drawImage(img.getImage(), x + w - right, y + top, x + w, y + h
                - bottom, img.getIconWidth() - right, top,
                img.getIconWidth(), img.getIconHeight() - bottom, null);

        // bottom
        g.drawImage(img.getImage(), x, y + h - bottom, x + left, y + h, 0,
                img.getIconHeight() - bottom, left, img.getIconHeight(),
                null);
        g.drawImage(img.getImage(), x + left, y + h - bottom,
                x + w - right, y + h, left, img.getIconHeight() - bottom,
                img.getIconWidth() - right, img.getIconHeight(), null);
        g.drawImage(img.getImage(), x + w - right, y + h - bottom, x + w, y
                + h, img.getIconWidth() - right, img.getIconHeight()
                - bottom, img.getIconWidth(), img.getIconHeight(), null);

        if (drawCenter) {
            g.drawImage(img.getImage(), x + left, y + top, x + w - right, y
                    + h - bottom, left, top, img.getIconWidth() - right,
                    img.getIconHeight() - bottom, null);
        }
    }
}

Related

  1. createScaledImageIcon(byte[] albumArtBytes)
  2. CreateSizedImageIconScaledSmooth(URL filePath, int width, int height)
  3. createTempImage(ImageIcon icon, File oldFile)
  4. displayImage(final ImageIcon ii)
  5. drawImageBorder(Graphics g, ImageIcon img, Rectangle rect, Insets ins, boolean drawCenter)
  6. fitIcon(ImageIcon icon, int containerWidth, int containerHeight)
  7. fitToSquare(ImageIcon icon, int newSize)
  8. generateImageIcon(Color color, int size, Insets insets)
  9. getBorderedIcon(ImageIcon icon)