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:GraphicsUtil.java

public static void drawString(Graphics g, String text, RectangularShape bounds, Align align, double angle) {
    Graphics2D g2 = (Graphics2D) g;
    Font font = g2.getFont();/*from  www  .j a v  a 2s .com*/
    if (angle != 0)
        g2.setFont(font.deriveFont(AffineTransform.getRotateInstance(Math.toRadians(angle))));

    Rectangle2D sSize = g2.getFontMetrics().getStringBounds(text, g2);
    Point2D pos = getPoint(bounds, align);
    double x = pos.getX();
    double y = pos.getY() + sSize.getHeight();

    switch (align) {
    case North:
    case South:
    case Center:
        x -= (sSize.getWidth() / 2);
        break;
    case NorthEast:
    case East:
    case SouthEast:
        x -= (sSize.getWidth());
        break;
    case SouthWest:
    case West:
    case NorthWest:
        break;
    }

    g2.drawString(text, (float) x, (float) y);
    g2.setFont(font);
}

From source file:org.gumtree.vis.mask.ChartMaskingUtilities.java

private static void drawMaskName(Graphics2D g2, AbstractMask mask, Rectangle2D imageArea, double fontSizeRate) {
    if (mask.getName() == null) {
        return;/*from   www.  j a  v a 2 s. c  o  m*/
    }
    Point2D fontLocation = mask.getTitleLocation(imageArea);
    g2.setPaint(Color.black);
    Font currentFont = g2.getFont();
    g2.setFont(currentFont.deriveFont((float) (maskNameFont.getSize() * fontSizeRate)).deriveFont(Font.ITALIC));
    g2.drawString(mask.getName(), (int) fontLocation.getX(), (int) fontLocation.getY());
    g2.setFont(currentFont);
}

From source file:Main.java

public static int paintMultilineText(Graphics2D g2d, String text, int textX, int textWidth, int textY,
        int maxTextLineCount) {
    FontRenderContext frc = new FontRenderContext(new AffineTransform(), true, false);
    int fa = g2d.getFontMetrics().getAscent();

    if (text.length() == 0)
        return textY;

    int currOffset = 0;
    AttributedString attributedDescription = new AttributedString(text);
    attributedDescription.addAttribute(TextAttribute.FONT, g2d.getFont());
    LineBreakMeasurer lineBreakMeasurer = new LineBreakMeasurer(attributedDescription.getIterator(), frc);
    int lineCount = 0;
    while (true) {
        TextLayout tl = lineBreakMeasurer.nextLayout(textWidth);
        if (tl == null)
            break;

        int charCount = tl.getCharacterCount();
        String line = text.substring(currOffset, currOffset + charCount);

        g2d.drawString(line, textX, textY);

        textY += fa;//w w  w  .  j a v a2  s.  co m
        currOffset += charCount;

        lineCount++;
        if ((maxTextLineCount > 0) && (lineCount == maxTextLineCount))
            break;
    }

    // textY += fh;

    return textY;
}

From source file:RussiaUnicode.java

public void paint(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g;
    g2d.drawString("\u0424\u0451\u0434\u043e\u0440.", 20, 30);
}

From source file:SVGGraphics2DDemo.java

public void paint(Graphics2D g) {
    g.drawString("Java,XML and Web Services Bible", 12, 12);
}

From source file:net.semanticmetadata.lire.utils.FileUtils.java

public static void saveImageResultsToPng(String prefix, TopDocs hits, String queryImage, IndexReader ir)
        throws IOException {
    LinkedList<BufferedImage> results = new LinkedList<BufferedImage>();
    int width = 0;
    for (int i = 0; i < Math.min(hits.scoreDocs.length, 10); i++) {
        // hits.score(i)
        // hits.doc(i).get("descriptorImageIdentifier")
        BufferedImage tmp = ImageIO
                .read(new FileInputStream(ir.document(hits.scoreDocs[i].doc).get("descriptorImageIdentifier")));
        if (tmp.getHeight() > 200) {
            double factor = 200d / ((double) tmp.getHeight());
            tmp = ImageUtils.scaleImage(tmp, (int) (tmp.getWidth() * factor), 200);
        }//  w  w w  .j  ava2s .c  o m
        width += tmp.getWidth() + 5;
        results.add(tmp);
    }
    BufferedImage result = new BufferedImage(width, 220, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2 = (Graphics2D) result.getGraphics();
    g2.setColor(Color.black);
    g2.clearRect(0, 0, result.getWidth(), result.getHeight());
    g2.setColor(Color.green);
    g2.setFont(Font.decode("\"Arial\", Font.BOLD, 12"));
    int offset = 0;
    int count = 0;
    for (Iterator<BufferedImage> iterator = results.iterator(); iterator.hasNext();) {
        BufferedImage next = iterator.next();
        g2.drawImage(next, offset, 20, null);
        g2.drawString(hits.scoreDocs[count].score + "", offset + 5, 12);
        offset += next.getWidth() + 5;
        count++;
    }
    ImageIO.write(result, "PNG", new File(prefix + "_" + (System.currentTimeMillis() / 1000) + ".png"));
}

From source file:BasicDraw.java

public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;

    g2d.drawString("aString", 100, 100);
    AffineTransform at = new AffineTransform();
    at.setToRotation(Math.PI / 4.0);
    g2d.setTransform(at);/* ww  w  .  j a v  a 2  s  .  com*/
    g2d.drawString("aString", 100, 100);

}

From source file:com.piaoyou.util.ImageUtil.java

public static BufferedImage getProductionImage(String productVersion, String productRealeaseDate) {

    String str = productVersion + " on " + productRealeaseDate;
    int IMAGE_WIDTH = 250;
    int IMAGE_HEIGHT = 30;

    BufferedImage bufferedImage = new BufferedImage(IMAGE_WIDTH, IMAGE_HEIGHT, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = bufferedImage.createGraphics();
    g.setBackground(Color.blue);/*from  w w w  .j a  v a  2s . c  o m*/
    g.setColor(Color.white);
    g.draw3DRect(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT, false);
    FontMetrics fontMetrics = g.getFontMetrics();
    int strWidth = fontMetrics.stringWidth(str);
    int strHeight = fontMetrics.getAscent() + fontMetrics.getDescent();
    g.drawString(str, (IMAGE_WIDTH - strWidth) / 2,
            IMAGE_HEIGHT - ((IMAGE_HEIGHT - strHeight) / 2) - fontMetrics.getDescent());
    g.dispose(); // free resource
    return bufferedImage;
}

From source file:common.utils.ImageUtils.java

/**
 *
 * @param src images to draw, they must be resized to an appropriate size
 * @param dst image where given images will be drawen
 *///  w w  w.  ja va  2s .com
public static void draw4on1(BufferedImage[] src, BufferedImage dst) {
    Graphics2D g2 = dst.createGraphics();
    g2.setColor(java.awt.Color.WHITE);
    g2.fillRect(0, 0, dst.getWidth(), dst.getHeight());
    int dxi;
    int dyi = 0;

    int x0 = dst.getWidth() - 5;
    int y0 = 5;
    int x = x0;
    int y = y0;
    for (int i = 0; i < src.length; i++) {
        if (i % 2 == 0) {
            dxi = -10;
        } else {
            dxi = 0;
        }
        //g2.draw3DRect(dx - 1 , dy-tmp_bi.getHeight() - 1, tmp_bi.getWidth() + 1 , tmp_bi.getHeight() + 1, true);
        g2.drawImage(src[i], x - src[i].getWidth() + dxi, y + dyi, null);
        g2.drawString("#" + i, x - src[i].getWidth() + dxi, y + dyi + 20);
        //g2.rotate(Math.toRadians(4));
        y = y + src[i].getHeight() / 2;
        if (y > dst.getHeight() - src[i].getHeight()) {
            y = y0;
            if (dyi == 0)
                dyi = 10;
            else
                dyi = 0;
            if (x < src[i].getWidth()) {
                x = dst.getWidth();
            }
            x = x - src[i].getWidth() / 2;
        }
    }
    g2.setColor(Color.gray);
    g2.drawRect(0, 0, dst.getWidth() - 1, dst.getHeight() - 1);
    g2.dispose();
}

From source file:net.semanticmetadata.lire.utils.FileUtils.java

public static void saveImageResultsToPng(String prefix, ImageSearchHits hits, String queryImage,
        IndexReader reader) throws IOException {
    LinkedList<BufferedImage> results = new LinkedList<BufferedImage>();
    int width = 0;
    for (int i = 0; i < hits.length(); i++) {
        // hits.score(i)
        // hits.doc(i).get("descriptorImageIdentifier")
        BufferedImage tmp = ImageIO.read(new FileInputStream(
                reader.document(hits.documentID(i)).getValues(DocumentBuilder.FIELD_NAME_IDENTIFIER)[0]));
        //            if (tmp.getHeight() > 200) {
        double factor = 200d / ((double) tmp.getHeight());
        tmp = ImageUtils.scaleImage(tmp, (int) (tmp.getWidth() * factor), 200);
        //            }
        width += tmp.getWidth() + 5;//from  w  ww .  j a v  a 2s . c  o  m
        results.add(tmp);
    }
    BufferedImage result = new BufferedImage(width, 220, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2 = (Graphics2D) result.getGraphics();
    g2.setColor(Color.white);
    g2.setBackground(Color.white);
    g2.clearRect(0, 0, result.getWidth(), result.getHeight());
    g2.setColor(Color.black);
    g2.setFont(Font.decode("\"Arial\", Font.BOLD, 12"));
    int offset = 0;
    int count = 0;
    for (Iterator<BufferedImage> iterator = results.iterator(); iterator.hasNext();) {
        BufferedImage next = iterator.next();
        g2.drawImage(next, offset, 20, null);
        g2.drawString(hits.score(count) + "", offset + 5, 12);
        offset += next.getWidth() + 5;
        count++;
    }
    ImageIO.write(result, "PNG", new File(prefix + "_" + (System.currentTimeMillis() / 1000) + ".png"));
}