Example usage for java.awt Graphics drawString

List of usage examples for java.awt Graphics drawString

Introduction

In this page you can find the example usage for java.awt Graphics drawString.

Prototype

public abstract void drawString(AttributedCharacterIterator iterator, int x, int y);

Source Link

Document

Renders the text of the specified iterator applying its attributes in accordance with the specification of the java.awt.font.TextAttribute TextAttribute class.

Usage

From source file:Main.java

/** Draw 'str' at the given location, but process newlines by moving
 * to a new line. *///from   w w w  .j a  v  a2s  . c o  m
public static void drawTextWithNewlines(Graphics g, String str, int x, int y) {
    String lines[] = str.split("\n");
    int lineHeight = g.getFontMetrics().getHeight();
    for (String s : lines) {
        g.drawString(s, x, y);
        y += lineHeight;
    }
}

From source file:WaterMark.java

public static String execute(String src, String dest, String text, Color color, Font font) throws Exception {
    BufferedImage srcImage = ImageIO.read(new File(src));

    int width = srcImage.getWidth(null);
    int height = srcImage.getHeight(null);
    BufferedImage destImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics g = destImage.getGraphics();

    g.drawImage(srcImage, 0, 0, width, height, null);
    g.setColor(color);/*from  www .  ja  va  2s .c  o  m*/
    g.setFont(font);
    g.fillRect(0, 0, 50, 50);
    g.drawString(text, width / 5, height - 10);
    g.dispose();

    ImageIO.write(destImage, DEFAULT_FORMAT, new File("dest.jpg"));
    return dest;
}

From source file:Main.java

public static void drawCenteredString(Graphics g, String str, int x, int y) {
    FontMetrics metrics = g.getFontMetrics(g.getFont());
    Rectangle2D rect = metrics.getStringBounds(str, g);
    int w = (int) (rect.getWidth());
    int h = (int) (rect.getHeight());
    g.drawString(str, x - w / 2, y + h / 2);
}

From source file:Main.java

/**
 * Draw a string centered within a rectangle.  The string is drawn using the graphics context's
 * current font and color./*w  ww  .j av  a2s .  com*/
 *
 * @param g the graphics context.
 * @param str the string.
 * @param x the bounding x position.
 * @param y the bounding y position.
 * @param width the bounding width.
 * @param height the bounding height.
 */
public static void drawStringCentered(Graphics g, String str, int x, int y, int width, int height) {
    FontMetrics fm = g.getFontMetrics(g.getFont());
    int xpos = x + ((width - fm.stringWidth(str)) / 2);
    int ypos = y + ((height + fm.getAscent()) / 2);
    g.drawString(str, xpos, ypos);
}

From source file:Main.java

public static void drawCenteredString(String string, int width, int height, Graphics graphics) {
    FontMetrics fontMetrics = graphics.getFontMetrics();
    int x = (width - fontMetrics.stringWidth(string)) / 2;
    int y = (fontMetrics.getAscent() + (height - (fontMetrics.getAscent() + fontMetrics.getDescent())) / 2);
    graphics.drawString(string, x, y);
}

From source file:GraphicsUtil.java

static public void drawText(Graphics g, String text, int x, int y, int halign, int valign) {
    if (text.length() == 0)
        return;/*from www  .  j a va  2 s.  com*/
    Rectangle bd = getTextBounds(g, text, x, y, halign, valign);
    g.drawString(text, bd.x, bd.y + g.getFontMetrics().getAscent());
}

From source file:org.b3log.symphony.model.Character.java

/**
 * Creates an image with the specified content (a character).
 *
 * @param content the specified content/* w w w . j  av  a2s  .co  m*/
 * @return image
 */
public static BufferedImage createImage(final String content) {
    final BufferedImage ret = new BufferedImage(500, 500, Transparency.TRANSLUCENT);
    final Graphics g = ret.getGraphics();
    g.setClip(0, 0, 50, 50);
    g.fillRect(0, 0, 50, 50);
    g.setFont(new Font(null, Font.PLAIN, 40));
    g.setColor(Color.BLACK);
    g.drawString(content, 5, 40);
    g.dispose();

    return ret;
}

From source file:FontAlgo.java

private static TextualChar getTextualChar(char a_char) throws Throwable {
    BufferedImage bImg = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
    Graphics g = bImg.getGraphics();
    g.setColor(Color.green);//from  www .  java2  s.c om
    g.fillRect(0, 0, WIDTH, HEIGHT);

    g.setFont(appliedFont);
    g.setColor(Color.black);
    g.drawString(new String(new char[] { a_char }), 10, g.getFontMetrics().getHeight());
    PixelGrabber p = new PixelGrabber(bImg, 0, 0, WIDTH, HEIGHT, true);

    if (p.grabPixels()) {
        char[][] pattern = new char[WIDTH][HEIGHT];
        int baseColourPixel = 0, contrastColourPixel = 0, x1 = 0, x2 = 0, y1 = 0, y2 = 0;
        int[] pixels = (int[]) p.getPixels();
        baseColourPixel = pixels[0];
        // System.out.println("base: " + base);
        int xCounter = 0, yCounter = 0;
        for (int iPixel : pixels) {
            // System.out.println(iX + " - " + iY);
            if (isReverse) {
                pattern[xCounter][yCounter] = iPixel == baseColourPixel ? CHAR_TO_PATTERN : ' ';
            } else {
                pattern[xCounter][yCounter] = iPixel != baseColourPixel ? CHAR_TO_PATTERN : ' ';
            }

            yCounter++;
            if (yCounter > 49) {
                xCounter++;
                yCounter = 0;
            }

            if (contrastColourPixel == 0 && iPixel != baseColourPixel) {
                contrastColourPixel = iPixel;
                x1 = xCounter - 2;
                y1 = yCounter - 3;
                y2 = yCounter + 3;
            }

            if (contrastColourPixel == iPixel) {
                x2 = xCounter + 3;

                if (y1 > (yCounter - 3)) {
                    y1 = yCounter - 3;
                }

                if (y2 < (yCounter + 3)) {
                    y2 = yCounter + 3;
                }
            }
        }
        return new TextualChar(x1, x2, y1, y2, pattern);
    }
    return null;
}

From source file:Main.java

/** Draw 'str' centered at 'p'. */
public static void drawCenteredText(Graphics g, Point p, String str) {
    FontMetrics fm = g.getFontMetrics();
    LineMetrics lm = fm.getLineMetrics(str, g);

    // Go to 'p', then add a/2 to get to the baseline.
    // I ignore the descent because it looks better to center without
    // regard to descenders.
    int baseY = p.y + (int) (lm.getAscent() / 2);
    int baseX = p.x - fm.stringWidth(str) / 2;

    g.drawString(str, baseX, baseY);
}

From source file:com.baidu.rigel.biplatform.ma.auth.resource.RandomValidateCode.java

private static String drowString(Graphics g, String randomString, int i) {
    g.setFont(getFont());//from  w  w w  .java2s . co  m
    g.setColor(new Color(random.nextInt(101), random.nextInt(111), random.nextInt(121)));
    String rand = String.valueOf(getRandomString(random.nextInt(randString.length())));
    randomString += rand;
    g.translate(random.nextInt(3), random.nextInt(3));
    g.drawString(rand, 13 * i, 16);
    return randomString;
}