Example usage for java.awt Graphics2D drawString

List of usage examples for java.awt Graphics2D drawString

Introduction

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

Prototype

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

Source Link

Document

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

Usage

From source file:com.pureinfo.srm.common.ImageHelper.java

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);
}

From source file:org.jhotdraw.samples.svg.figures.SVGImage.java

public static SVGImage getLoadingImage() {
    BufferedImage image;/*from   w  w  w. j av  a  2  s  .  com*/
    try {
        image = ImageIO.read(SVGImage.class.getResource("loading.jpg"));
    } catch (Exception e) {
        String text = "";
        Font font = new Font("", Font.PLAIN, 18);
        image = new BufferedImage(150, 100, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = (Graphics2D) image.createGraphics();
        g.setColor(Color.black);
        g.setFont(font);
        int w = g.getFontMetrics().stringWidth(text);
        int h = g.getFontMetrics().getHeight();
        g.drawString(text, 10, h);
        g.dispose();
    }
    return new SVGImage(image);
}

From source file:Main.java

public static BufferedImage takeScreenShot(Component component, String... watermarks) {

    Dimension size = component.getSize();

    BufferedImage screenshot = new BufferedImage(size.width, size.height, Transparency.OPAQUE);
    Graphics2D g = screenshot.createGraphics();
    g.setClip(0, 0, size.width - 1, size.height - 1);

    component.update(g);/*from w ww  .ja  va2  s  .  com*/

    FontMetrics fm = g.getFontMetrics();
    int y = fm.getDescent();
    for (String watermark : watermarks)
        if (watermark != null) {
            int x = size.width - SwingUtilities.computeStringWidth(fm, watermark);

            g.setColor(Color.black);
            g.drawString(watermark, x, y);

            g.setColor(Color.white);
            g.drawString(watermark, x - 1, y - 1);

            y -= fm.getHeight();
        }

    g.dispose();

    return screenshot;
}

From source file:juicebox.tools.utils.juicer.apa.APAPlotter.java

/**
 * Plot text centered at a point (rather than from the upper left corner as is the default)
 *
 * @param g2       graphics2D object/* w  w  w  .j a v a2s  .  c  o  m*/
 * @param text     to be plotted
 * @param position of where text will be centered at
 */
private static void drawCenteredString(Graphics2D g2, String text, Point position) {
    FontMetrics fm = g2.getFontMetrics();
    int x2 = position.x - fm.stringWidth(text) / 2;
    int y2 = fm.getAscent() + (position.y - (fm.getAscent() + fm.getDescent()) / 2);
    g2.drawString(text, x2, y2);
}

From source file:net.noday.core.utils.Captcha.java

public static BufferedImage gen(String text, int width, int height) throws IOException {
    BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
    Graphics2D g = (Graphics2D) bi.getGraphics();
    g.setColor(Color.GRAY);//from   w w  w . j  a v a  2  s.co  m
    g.fillRect(0, 0, width, height);
    for (int i = 0; i < 10; i++) {
        g.setColor(randColor(150, 250));
        g.drawOval(random.nextInt(110), random.nextInt(24), 5 + random.nextInt(10), 5 + random.nextInt(10));
        Font f = new Font("Arial", Font.ITALIC, 20);
        g.setFont(f);
        g.setColor(randColor(10, 240));
        g.drawString(text, 4, 24);
    }
    return bi;
}

From source file:org.mili.core.graphics.GraphicsUtil.java

/**
 * Prints a character image in defined font in a file.
 *
 * @param dir directory./*from  ww w  .  j  a v a 2  s.  c  o  m*/
 * @param fn filename.
 * @param font font.
 * @param c character to print in file.
 * @param mx character size x.
 * @param my character size y.
 * @throws IOException if io exception occurs.
 */
public static void writeChar(File dir, String fn, Font font, char c, int mx, int my) throws IOException {
    BufferedImage bi = new BufferedImage(mx, my, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = (Graphics2D) bi.createGraphics();
    g.setFont(font);
    FontMetrics fm = g.getFontMetrics();
    int fh = fm.getHeight();
    int cw = fm.charWidth(c);
    int asc = g.getFontMetrics().getAscent();
    int x0 = mx / 2 - cw / 2;
    int y0 = my / 2 - fh / 2 + asc;
    g.drawString(String.valueOf(c), x0, y0);
    g.dispose();
    File f = new File(dir, fn);
    GraphicsUtil.writeImage(f, bi);
}

From source file:org.ut.biolab.medsavant.client.util.ClientMiscUtils.java

/**
 * Draws a string centred in the given box.
 *///from   w  w  w  .java2  s  .  c  o  m
public static void drawCentred(Graphics2D g2, String message, Rectangle2D box) {
    FontMetrics metrics = g2.getFontMetrics();
    Rectangle2D stringBounds = g2.getFont().getStringBounds(message, g2.getFontRenderContext());
    float x = (float) (box.getX() + (box.getWidth() - stringBounds.getWidth()) / 2.0);
    float y = (float) (box.getY() + (box.getHeight() + metrics.getAscent() - metrics.getDescent()) / 2.0);

    g2.drawString(message, x, y);
}

From source file:org.springframework.cloud.stream.app.pose.estimation.processor.DebugVisualizationUtility.java

private static byte[] drawLimbCandidate(byte[] imageBytes, Limb limb) {

    return new ImageGraphicsTemplate() {
        @Override// w w w .j a v  a  2s. c  o m
        public void drawWithingImage(Graphics2D g) {
            Part from = limb.getFromPart();
            Part to = limb.getToPart();

            int x1 = from.getNormalizedX();
            int x2 = to.getNormalizedX();

            int y1 = from.getNormalizedY();
            int y2 = to.getNormalizedY();

            int xl = (x2 - x1) / 2;
            int yl = (y2 - y1) / 2;

            g.setColor(GraphicsUtils.yellow);
            g.drawString(String.format("%.2f", limb.getPafScore()), x1 + xl + 5, y1 + yl + 5);

            g.setColor(new Color(167, 252, 0));
            g.draw(new Line2D.Double(from.getNormalizedX(), from.getNormalizedY(), to.getNormalizedX(),
                    to.getNormalizedY()));

            g.setColor(GraphicsUtils.CLASS_COLOR2[from.getPartType().getId()]);
            g.fillOval(from.getNormalizedX() - OVAL_WIDTH / 2, from.getNormalizedY() - OVAL_WIDTH / 2,
                    OVAL_WIDTH, OVAL_HEIGHT);
            g.setColor(GraphicsUtils.CLASS_COLOR2[to.getPartType().getId()]);
            g.fillOval(to.getNormalizedX() - OVAL_WIDTH / 2, to.getNormalizedY() - OVAL_WIDTH / 2, OVAL_WIDTH,
                    OVAL_HEIGHT);
        }
    }.draw(imageBytes);
}

From source file:Main.java

/**
 * Creates and returns image from the given text.
 * @param text input text// w w w .j a  va2 s  . c o  m
 * @param font text font
 * @return image with input text
 */
public static BufferedImage createImageFromText(String text, Font font) {
    //You may want to change these setting, or make them parameters
    boolean isAntiAliased = true;
    boolean usesFractionalMetrics = false;
    FontRenderContext frc = new FontRenderContext(null, isAntiAliased, usesFractionalMetrics);
    TextLayout layout = new TextLayout(text, font, frc);
    Rectangle2D bounds = layout.getBounds();
    int w = (int) Math.ceil(bounds.getWidth());
    int h = (int) Math.ceil(bounds.getHeight()) + 2;
    BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); //for example;
    Graphics2D g = image.createGraphics();
    g.setColor(Color.WHITE);
    g.fillRect(0, 0, w, h);
    g.setColor(Color.BLACK);
    g.setFont(font);
    Object antiAliased = isAntiAliased ? RenderingHints.VALUE_TEXT_ANTIALIAS_ON
            : RenderingHints.VALUE_TEXT_ANTIALIAS_OFF;
    g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, antiAliased);
    Object fractionalMetrics = usesFractionalMetrics ? RenderingHints.VALUE_FRACTIONALMETRICS_ON
            : RenderingHints.VALUE_FRACTIONALMETRICS_OFF;
    g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, fractionalMetrics);
    g.drawString(text, (float) -bounds.getX(), (float) -bounds.getY());
    g.dispose();

    return image;
}

From source file:savant.util.MiscUtils.java

/**
 * Patterned off GraphPane.drawMessageHelper, draws a string centred in the given box.
 *//*ww w .  j a va 2  s . c om*/
public static void drawMessage(Graphics2D g2, String message, Rectangle2D box) {
    FontMetrics metrics = g2.getFontMetrics();
    Rectangle2D stringBounds = g2.getFont().getStringBounds(message, g2.getFontRenderContext());
    float x = (float) (box.getX() + (box.getWidth() - stringBounds.getWidth()) / 2.0);
    float y = (float) (box.getY() + (box.getHeight() + metrics.getAscent() - metrics.getDescent()) / 2.0);

    g2.drawString(message, x, y);
}