Java Draw Image drawLabel(BufferedImage image, String text)

Here you can find the source of drawLabel(BufferedImage image, String text)

Description

draw Label

License

Open Source License

Parameter

Parameter Description
image the image to label.
text the text to use.

Return

the image with a label on it.

Declaration

public static BufferedImage drawLabel(BufferedImage image, String text) 

Method Source Code


//package com.java2s;
/*//from w  w  w. ja  v  a2  s  .  co  m
  This file is part of Cuber.
    
Cuber 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.
    
Cuber 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 Cuber.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.awt.Color;

import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;

public class Main {
    /**
     * @param image
     *            the image to label.
     * @param text
     *            the text to use.
     * @return the image with a label on it.
     * @since 08/07/2013
     * @author wonka
     */
    public static BufferedImage drawLabel(BufferedImage image, String text) {
        Graphics2D g = (Graphics2D) image.getGraphics();
        g.setColor(new Color(255, 255, 255, 255));
        int w1 = image.getWidth() * 2 / 100;
        int y1 = 10;
        int labelSize = 30;
        g.fillRect(w1, y1, image.getWidth() - (w1 + w1), labelSize);

        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        g.setColor(Color.BLACK);
        int fontSize = labelSize * 70 / 100;
        g.setFont(new Font(Font.MONOSPACED, Font.PLAIN, fontSize));
        g.drawString(text, w1, y1 + fontSize);
        return image;
    }
}

Related

  1. drawImageInPolygon(Graphics g2d, BufferedImage img, Polygon poly, double xfactor, double yfactor)
  2. drawImageInRect(Graphics g2d, BufferedImage img, Rectangle rect, double xfactor, double yfactor)
  3. drawImageInRect(Graphics2D gr, Image img, Rectangle r, boolean noShrink)
  4. drawImageWithClipping(Graphics g, BufferedImage img)
  5. drawInMiddle(Graphics g, Image image, String text)
  6. drawNormalImage(Rectangle2D bounds, Graphics2D g2, Image image, ImageObserver observer, int imageWidth, int imageHeight)
  7. drawOnto(final BufferedImage pDestination, final Image pSource)
  8. drawPixel(BufferedImage img, int x, int y, int color)
  9. drawPixel(int x, int y, Color color, BufferedImage displayMap)