Here you can find the source of drawLabel(BufferedImage image, String text)
Parameter | Description |
---|---|
image | the image to label. |
text | the text to use. |
public static BufferedImage drawLabel(BufferedImage image, String text)
//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; } }