Java Draw String drawString(Graphics2D g, String string, int x, int y, boolean includeOutline)

Here you can find the source of drawString(Graphics2D g, String string, int x, int y, boolean includeOutline)

Description

Draw a string in Verdana size 14.

License

Open Source License

Parameter

Parameter Description
g the Graphics2D to paint to.
string the string to paint
x the x-coordinate of the string location
y the y-coordinate of the string location
includeOutline whether to paint a white outline

Declaration

public static void drawString(Graphics2D g, String string, int x,
        int y, boolean includeOutline) 

Method Source Code

//package com.java2s;
/*/*www .  ja  v a2 s .c  om*/
 * @(#)BlogHelper.java
 *
 * $Date: 2014-11-27 07:50:51 +0100 (Do, 27 Nov 2014) $
 *
 * Copyright (c) 2014 by Jeremy Wood.
 * All rights reserved.
 *
 * The copyright of this software is owned by Jeremy Wood. 
 * You may not use, copy or modify this software, except in  
 * accordance with the license agreement you entered into with  
 * Jeremy Wood. For details see accompanying license terms.
 * 
 * This software is probably, but not necessarily, discussed here:
 * https://javagraphics.java.net/
 * 
 * That site should also contain the most recent official version
 * of this software.  (See the SVN repository for more details.)
 */

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

import java.awt.Font;
import java.awt.Graphics2D;

import java.awt.Shape;

public class Main {
    /** Draw a string in Verdana size 14.
     * @param g the Graphics2D to paint to.
     * @param string the string to paint
     * @param x the x-coordinate of the string location
     * @param y the y-coordinate of the string location
     * @param includeOutline whether to paint a white outline
     */
    public static void drawString(Graphics2D g, String string, int x,
            int y, boolean includeOutline) {
        Font font = (new Font("Verdana", 0, 14));
        if (includeOutline) {
            Shape textShape = font.createGlyphVector(
                    g.getFontRenderContext(), string).getOutline(x, y);
            g.setComposite(AlphaComposite.SrcOver);
            if (includeOutline) {
                g.setStroke(new BasicStroke(3, BasicStroke.CAP_ROUND,
                        BasicStroke.JOIN_ROUND));
                g.setColor(Color.white);
                g.draw(textShape);
            }
            g.setColor(Color.black);
            g.fill(textShape);
        } else {
            g.setFont(font);
            g.setColor(Color.black);
            g.drawString(string, x, y);
        }
    }
}

Related

  1. drawString(Graphics g, String text, int x, int y)
  2. drawString(Graphics g2, String string, int x, int y, Font font, Color color)
  3. drawString(Graphics2D g, String s, int x, int y, int width)
  4. drawString(Graphics2D g, String string, double x, double y, Color color)
  5. drawString(Graphics2D g, String string, int x, int y)
  6. drawString(Graphics2D g2, String str, int x, int y, int halign, int valign)
  7. drawString(Graphics2D gfx, String text, int x, int y)
  8. drawString(Graphics2D graphics, String text, double x, double y, boolean centerHorizontal, boolean centerVertical, Color backgroundColor)
  9. drawString(Image img, String text, Color color)