Java Image Create createImage(String str)

Here you can find the source of createImage(String str)

Description

create Image

License

Open Source License

Declaration

public static Icon createImage(String str) 

Method Source Code


//package com.java2s;
/*//from w  w  w  . ja v  a2 s.com
Copyright (C) 2005  Eduardo Jodas Samper
    
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 2 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, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    
   author e-mail: eduardj@dev.java.net
*/

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;

import javax.swing.Icon;
import javax.swing.ImageIcon;

public class Main {
    public static Icon createImage(String str) {
        final int ICON_DIMENSION = 16;
        final Color BG_COLOR = new Color(96, 224, 96);
        if (str.length() > 1)
            return null;

        BufferedImage image = new BufferedImage(ICON_DIMENSION, ICON_DIMENSION, BufferedImage.TYPE_INT_ARGB);
        Graphics2D graphics = (Graphics2D) image.getGraphics();
        graphics.setComposite(AlphaComposite.Src);
        graphics.setColor(new Color(0, 0, 0, 0));
        graphics.fillRect(0, 0, ICON_DIMENSION, ICON_DIMENSION);

        graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        graphics.setColor(BG_COLOR);
        graphics.fillOval(0, 0, ICON_DIMENSION - 1, ICON_DIMENSION - 1);

        // create a whitish spot in the left-top corner of the icon
        double id4 = ICON_DIMENSION / 4.0;
        double spotX = id4;
        double spotY = id4;
        for (int col = 0; col < ICON_DIMENSION; col++) {
            for (int row = 0; row < ICON_DIMENSION; row++) {
                // distance to spot
                double dx = col - spotX;
                double dy = row - spotY;
                double dist = Math.sqrt(dx * dx + dy * dy);

                // distance of 0.0 - comes 90% to Color.white
                // distance of ICON_DIMENSION - stays the same

                if (dist > ICON_DIMENSION) {
                    dist = ICON_DIMENSION;
                }

                int currColor = image.getRGB(col, row);
                int transp = (currColor >>> 24) & 0xFF;
                int oldR = (currColor >>> 16) & 0xFF;
                int oldG = (currColor >>> 8) & 0xFF;
                int oldB = (currColor >>> 0) & 0xFF;

                double coef = 0.9 - 0.9 * dist / ICON_DIMENSION;
                int dr = 255 - oldR;
                int dg = 255 - oldG;
                int db = 255 - oldB;

                int newR = (int) (oldR + coef * dr);
                int newG = (int) (oldG + coef * dg);
                int newB = (int) (oldB + coef * db);

                int newColor = (transp << 24) | (newR << 16) | (newG << 8) | newB;
                image.setRGB(col, row, newColor);
            }
        }

        // draw outline of the icon
        graphics.setColor(Color.black);
        graphics.drawOval(0, 0, ICON_DIMENSION - 1, ICON_DIMENSION - 1);

        int w = graphics.getFontMetrics().stringWidth(str);
        int a = graphics.getFontMetrics().getAscent();
        int d = graphics.getFontMetrics().getDescent();
        graphics.drawString("" + str, (ICON_DIMENSION - w) / 2, ICON_DIMENSION / 2 + (a - d) / 2);

        image.flush();
        graphics.dispose();
        return new ImageIcon(image);
    }
}

Related

  1. createImage(byte[] data)
  2. createImage(Component comp, Icon icon)
  3. createImage(Icon icon)
  4. createImage(JPanel panel)
  5. createImage(URL url, String description, boolean checkPresence)
  6. createImageCopy(Image image)
  7. createImageFromBase64String(String s)
  8. createImageFromInvisibleComponent(JComponent component)