Java Graphics Draw Multiline String draw(String _str, Graphics2D _g2, int _nX0, int _nY0, int _nX1, int _nY1)

Here you can find the source of draw(String _str, Graphics2D _g2, int _nX0, int _nY0, int _nX1, int _nY1)

Description

draw

License

Open Source License

Declaration

private static void draw(String _str, Graphics2D _g2, int _nX0, int _nY0, int _nX1, int _nY1) 

Method Source Code

//package com.java2s;
/**/*from   w ww . j a va 2  s.  c o  m*/
 * PureInfo JavaMail
 * @(#)Test.java   1.0 Nov 1, 2005
 * 
 * Copyright(c) 2004-2005, PureInfo Information Technology Corp. Ltd. 
 * All rights reserved, see the license file.
 * 
 * www.pureinfo.com.cn
 */

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

import java.awt.font.TextAttribute;

import java.text.AttributedString;
import java.util.Random;

public class Main {
    private static Random random = new Random(System.currentTimeMillis());

    private static void draw(String _str, Graphics2D _g2, int _nX0, int _nY0, int _nX1, int _nY1) {
        Font font = new Font(getFontName(), Font.BOLD, getFontSize());
        AttributedString str = new AttributedString(_str);
        str.addAttribute(TextAttribute.FONT, font);
        str.addAttribute(TextAttribute.FOREGROUND, getColor());
        _g2.drawString(str.getIterator(), getNumber(_nX0, _nX1), (_nY0 + _nY1) * 2 / 3);
    }

    private static String getFontName() {
        String[] names = new String[] { "Comic Sans MS", "Arial Narrow", "Times New Roman", "Courier New" };
        return names[Math.abs(random.nextInt()) % names.length];
    }

    private static int getFontSize() {
        int nSize = Math.abs(random.nextInt()) % 10 * 3 + 25;
        return nSize > 45 ? 45 : (nSize < 30 ? 30 : nSize);
    }

    private static Color getColor() {
        Color[] names = new Color[] { Color.WHITE, Color.RED, Color.GREEN, Color.BLUE, Color.CYAN,
                Color.DARK_GRAY };
        return names[Math.abs(random.nextInt()) % names.length];
    }

    private static int getNumber(int _nX0, int _nX1) {
        int number = _nX0 + (Math.abs(random.nextInt()) % (_nX1 - _nX0)) * 3 / 5;
        return number > 170 ? 170 : number;
    }
}

Related

  1. drawMultilineString(Graphics g, String s, int alignment, Rectangle r, boolean print)
  2. drawMultilineText(final Graphics2D gc, final int x, int y, final String text)
  3. drawMultiLineText(String text, Graphics G, int x, int y)
  4. drawRightMultiLineText(String text, Graphics G, int x, int y)