Java ImageIcon getBorderedIcon(ImageIcon icon)

Here you can find the source of getBorderedIcon(ImageIcon icon)

Description

get Bordered Icon

License

Open Source License

Declaration

public static ImageIcon getBorderedIcon(ImageIcon icon) 

Method Source Code


//package com.java2s;
/*/*  w  w w .j a v a2 s. co  m*/
 * Copyright (C) 2014. EMBL, European Bioinformatics Institute
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.awt.BasicStroke;
import java.awt.Color;

import java.awt.Graphics2D;

import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;

public class Main {
    public static ImageIcon getBorderedIcon(ImageIcon icon) {
        int borderWidth = 1 / 4;
        int spaceAroundIcon = 0;
        Color borderColor = Color.BLACK;

        BufferedImage bi = new BufferedImage(icon.getIconWidth() + (2 * borderWidth + 2 * spaceAroundIcon),
                icon.getIconHeight() + (2 * borderWidth + 2 * spaceAroundIcon), BufferedImage.TYPE_INT_ARGB);

        Graphics2D g = bi.createGraphics();
        g.setColor(borderColor);
        g.drawImage(icon.getImage(), borderWidth + spaceAroundIcon, borderWidth + spaceAroundIcon, null);

        BasicStroke stroke = new BasicStroke(1); //1 pixel wide (thickness of the border)
        g.setStroke(stroke);

        g.drawRect(0, 0, bi.getWidth() - 1, bi.getHeight() - 1);
        g.dispose();
        return new ImageIcon(bi);
    }
}

Related

  1. drawImageBorder(Graphics g, ImageIcon img, Rectangle rect, Insets ins, boolean drawCenter)
  2. drawImageBorder(Graphics g, ImageIcon img, Rectangle rect, Insets ins, boolean drawCenter)
  3. fitIcon(ImageIcon icon, int containerWidth, int containerHeight)
  4. fitToSquare(ImageIcon icon, int newSize)
  5. generateImageIcon(Color color, int size, Insets insets)
  6. getByteImage(ImageIcon icon)
  7. getDisabledIcon(ImageIcon icon)
  8. getHeight(ImageIcon imagen)
  9. getOpenSwingImage(String name, ImageIcon defaultIcon)