draw Titles for BufferedImage - Java 2D Graphics

Java examples for 2D Graphics:BufferedImage Paint

Description

draw Titles for BufferedImage

Demo Code


//package com.java2s;

import java.awt.*;
import java.awt.image.BufferedImage;

import java.io.File;
import java.io.FileInputStream;

public class Main {

    public static BufferedImage drawTitles(BufferedImage source,
            String[] titles, int[] xPositions, int[] yPositions,
            Color fontColor, float fontsize) throws Exception {

        Font font = Font/*from  w  ww .  j a v a 2  s  .c  o m*/
                .createFont(Font.TRUETYPE_FONT, new FileInputStream(
                        new File("resources/fonts/CRISTAL.ttf")));
        font = font.deriveFont(fontsize);
        BufferedImage img = new BufferedImage(source.getWidth(),
                source.getHeight(), BufferedImage.TYPE_4BYTE_ABGR);
        Graphics2D g2 = (Graphics2D) img.getGraphics();
        g2.drawImage(source, 0, 0, null);
        g2.setColor(fontColor);
        g2.setFont(font);
        for (int i = 0; i < titles.length; i++) {
            g2.drawString(titles[i], xPositions[i], yPositions[i]);
        }
        g2.dispose();
        return img;
    }
}

Related Tutorials