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:edu.ku.brc.ui.GradiantButton.java

/**
 * Paints the text of the control/* ww  w. jav a2s .c  o m*/
 * @param g2 the graphics to be painted into
 * @param w the width of the control
 * @param h the height of the control
 * @param text the string
 */
protected void drawText(Graphics2D g2, int w, int h, String text) {
    // calculate the width and height
    int fw = g2.getFontMetrics().stringWidth(text);
    int fh = g2.getFontMetrics().getAscent() - g2.getFontMetrics().getDescent();

    int textx = this.getHorizontalAlignment() == SwingConstants.LEFT ? Math.max(getInsets().left, 2)
            : (w - fw) / 2;
    int texty = h / 2 + fh / 2;

    // draw the text
    g2.setColor(textColorShadow);
    g2.drawString(text, textx, texty);
    g2.setColor(textColor);
    g2.drawString(text, textx, texty);

}

From source file:FontTest.java

public void paintComponent(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;

    String message = "Hello, World!";

    Font f = new Font("Serif", Font.BOLD, 36);
    g2.setFont(f);/*from w w  w  .jav  a  2 s  .co m*/

    // measure the size of the message

    FontRenderContext context = g2.getFontRenderContext();
    Rectangle2D bounds = f.getStringBounds(message, context);

    // set (x,y) = top left corner of text

    double x = (getWidth() - bounds.getWidth()) / 2;
    double y = (getHeight() - bounds.getHeight()) / 2;

    // add ascent to y to reach the baseline

    double ascent = -bounds.getY();
    double baseY = y + ascent;

    // draw the message

    g2.drawString(message, (int) x, (int) baseY);

    g2.setPaint(Color.LIGHT_GRAY);

    // draw the baseline

    g2.draw(new Line2D.Double(x, baseY, x + bounds.getWidth(), baseY));

    // draw the enclosing rectangle

    Rectangle2D rect = new Rectangle2D.Double(x, y, bounds.getWidth(), bounds.getHeight());
    g2.draw(rect);
}

From source file:edu.csudh.goTorosBank.WithdrawServlet.java

public String writeIntoCheck(String filePath, String username, String theAmount, String amountInWords,
        String dateWrote, String person_payingto, String billType) throws IOException, NullPointerException {

    File blueCheck = new File("blank-blue-check.jpg");
    String pathToOriginal = getServletContext().getRealPath("/" + blueCheck);

    File file = new File(pathToOriginal);
    BufferedImage imageToCopy = null;
    try {//from  w  w w  . ja  v a2s  .  c  o m
        imageToCopy = ImageIO.read(file);
    } catch (IOException e) {
        e.printStackTrace();
    }

    String amount = theAmount;
    String person_gettingPayed = person_payingto;
    String amountinWords = amountInWords;
    String date = dateWrote;
    String bill_type = billType;

    int w = imageToCopy.getWidth();
    int h = imageToCopy.getHeight();
    BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = img.createGraphics();
    g2d.setColor(g2d.getBackground());
    g2d.fillRect(0, 0, w, h);
    g2d.drawImage(imageToCopy, 0, -100, null);
    g2d.setPaint(Color.black);
    g2d.setFont(new java.awt.Font("Serif", Font.BOLD, 36));
    //g2d.setFont(new Font("Serif", Font.BOLD, 36));

    FontMetrics fm = g2d.getFontMetrics();
    int x = img.getWidth() - fm.stringWidth(amount) - 100;
    int y = fm.getHeight();
    g2d.drawString(amount, x - 70, y + 335);
    g2d.drawString(person_gettingPayed, x - 800, y + 329);
    g2d.drawString(amountinWords, x - 940, y + 390);
    g2d.drawString(date, x - 340, y + 245);
    g2d.drawString(bill_type, x - 900, y + 530);

    String signature = "Use The Force";
    g2d.setFont(new java.awt.Font("Monotype Corsiva", Font.BOLD | Font.ITALIC, 36));
    g2d.drawString(signature, x - 340, y + 530);
    g2d.dispose();
    /*write check to file*/
    String filename = fileNameGenerator(username);
    String fullname = filePath + "_" + filename + ".jpg";
    ImageIO.write(img, "jpg", new File(fullname));
    return fullname;
}

From source file:org.n52.server.io.DiagramGenerator.java

public void createLegend(DesignOptions options, OutputStream out) throws OXFException, IOException {

    int topMargin = 10;
    int leftMargin = 30;
    int iconWidth = 15;
    int iconHeight = 15;
    int verticalSpaceBetweenEntries = 20;
    int horizontalSpaceBetweenIconAndText = 15;

    DesignDescriptionList ddList = buildUpDesignDescriptionList(options);
    int width = 800;
    int height = topMargin + (ddList.size() * (iconHeight + verticalSpaceBetweenEntries));

    BufferedImage legendImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics2D legendGraphics = legendImage.createGraphics();
    legendGraphics.setColor(Color.white);
    legendGraphics.fillRect(0, 0, width, height);

    int offset = 0;
    for (RenderingDesign dd : ddList.getAllDesigns()) {
        int yPos = topMargin + offset * iconHeight + offset * verticalSpaceBetweenEntries;

        // icon://from  www  .  jav a 2 s .co  m
        legendGraphics.setColor(dd.getColor());
        legendGraphics.fillRect(leftMargin, yPos, iconWidth, iconHeight);

        // text:
        legendGraphics.setColor(Color.black);

        legendGraphics.drawString(dd.getFeature().getLabel() + " - " + dd.getLabel(),
                leftMargin + iconWidth + horizontalSpaceBetweenIconAndText, yPos + iconHeight);

        offset++;
    }

    JPEGImageWriteParam p = new JPEGImageWriteParam(null);
    p.setCompressionMode(JPEGImageWriteParam.MODE_DEFAULT);
    write(legendImage, FORMAT, out);
}

From source file:org.squidy.designer.zoom.impl.SourceCodeShape.java

@Override
protected void paintShapeZoomedIn(PPaintContext paintContext) {
    super.paintShapeZoomedIn(paintContext);

    Graphics2D g = paintContext.getGraphics();

    PBounds bounds = getBoundsReference();

    Class<?> type = nodeShape.getProcessable().getClass();

    String typeName = type.getSimpleName();

    g.setFont(fontName);//from  w w  w.j  a v  a 2s  . com
    g.drawString(typeName, (int) (bounds.x + 50), 140);

    g.setFont(fontSource);

    // Calculate sourceName string if not done yet.
    //      if (sourceName == null) {
    //         sourceName = FontUtils.createCroppedLabelIfNecessary(g.getFontMetrics(), "Source: "
    //               + sourceCodeURL.toString(), (int) (bounds.width));
    //         sourceNameX = (int) (bounds.x + bounds.width - FontUtils.getWidthOfText(g.getFontMetrics(), sourceName) - 20);
    //      }
    //      g.drawString(sourceName, sourceNameX, 90);

    // Calculate className string if not done yet.
    if (className == null) {
        className = FontUtils.createCroppedLabelIfNecessary(g.getFontMetrics(), "Class: " + type.getName(),
                (int) (bounds.width * 0.7));
        classNameX = (int) (bounds.x + bounds.width - FontUtils.getWidthOfText(g.getFontMetrics(), className)
                - 20);
    }
    g.drawString(className, classNameX, 110);
}

From source file:FilledGeneralPath.java

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    int x = 5;//ww  w. ja v a2s . c o  m
    int y = 7;

    // fill and stroke GeneralPath
    int xPoints[] = { x, 200, x, 200 };
    int yPoints[] = { y, 200, 200, y };
    GeneralPath filledPolygon = new GeneralPath(GeneralPath.WIND_EVEN_ODD, xPoints.length);
    filledPolygon.moveTo(xPoints[0], yPoints[0]);
    for (int index = 1; index < xPoints.length; index++) {
        filledPolygon.lineTo(xPoints[index], yPoints[index]);
    }
    filledPolygon.closePath();
    g2.setPaint(Color.red);
    g2.fill(filledPolygon);
    g2.setPaint(Color.black);
    g2.draw(filledPolygon);
    g2.drawString("Filled and Stroked GeneralPath", x, 250);
}

From source file:org.evors.rs.ui.sandpit.TrialViewer.java

public void drawText(Graphics2D g2, String text) {
    FontMetrics fm = g2.getFontMetrics();
    List<String> strings = Splitter.on("\n").splitToList(text);
    float x1 = 40, y = 40;
    int dx = 0;//from  ww w . j a v a2s .  co  m

    for (String s : strings) {
        int width = fm.stringWidth(s);
        if (width > dx) {
            dx = width;
        }
    }
    g2.setColor(new Color(0.6f, 0.6f, 0.6f, 0.6f));
    g2.fillRect((int) x1, (int) y - fm.getHeight(), dx + 5, fm.getHeight() * strings.size() + 5);
    g2.setColor(Color.red);
    for (String s : strings) {
        g2.drawString(s, 40, y);
        y += fm.getHeight();
    }

}

From source file:org.colombbus.tangara.AboutWindow.java

private BufferedImage createBackgroundImage(Image baseBackgroundImg) {
    BufferedImage newImg = new BufferedImage(baseBackgroundImg.getWidth(null),
            baseBackgroundImg.getHeight(null), BufferedImage.TYPE_INT_RGB);
    newImg.getGraphics().drawImage(baseBackgroundImg, 0, 0, null);
    Graphics2D drawingGraphics = (Graphics2D) newImg.getGraphics();
    Color titleColor = Configuration.instance().getColor("tangara.title.color");
    String titleText = Configuration.instance().getString("tangara.title");
    Font titleFont = Configuration.instance().getFont("tangara.title.font");
    drawingGraphics.setFont(titleFont);//  ww w. j  a  v  a2  s.  com
    drawingGraphics.setColor(titleColor);
    drawingGraphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
            RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    drawingGraphics.drawString(titleText, marginLeft, windowHeight / 2 - marginText);
    return newImg;
}

From source file:org.cybercat.automation.addons.common.ScreenshotManager.java

public BufferedImage applySubs(BufferedImage image, String text) {
    Graphics2D g2 = image.createGraphics();
    g2.setFont(font);//  w w  w .  j  av  a 2s .  com

    int height = image.getHeight();
    int width = image.getWidth();

    g2.setColor(fontColor);

    String[] subs = text.split("\n");
    FontMetrics fMetrics = g2.getFontMetrics();
    int lineHeight = fMetrics.getHeight();
    int lineWidth;
    for (int i = subs.length; i > 0; i--) {
        lineWidth = fMetrics.stringWidth(subs[i - 1]);
        int y = height - bottomOffset - ((subs.length - i) * (lineHeight + lineOffset));
        int x = (width / 2) - (lineWidth / 2);
        g2.drawString(subs[i - 1], x, y);
    }
    g2.dispose();
    return image;
}

From source file:org.n52.server.sos.generator.DiagramGenerator.java

/**
 * Produce legend.//from  w  w w.java2s.c o  m
 * 
 * @param options
 *            the options
 * @param out
 *            the out
 * @throws OXFException
 *             the oXF exception
 */
public void createLegend(DesignOptions options, OutputStream out) throws OXFException, IOException {

    int topMargin = 10;
    int leftMargin = 30;
    int iconWidth = 15;
    int iconHeight = 15;
    int verticalSpaceBetweenEntries = 20;
    int horizontalSpaceBetweenIconAndText = 15;

    DesignDescriptionList ddList = buildUpDesignDescriptionList(options);
    int width = 800;
    int height = topMargin + (ddList.size() * (iconHeight + verticalSpaceBetweenEntries));

    BufferedImage legendImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics2D legendGraphics = legendImage.createGraphics();
    legendGraphics.setColor(Color.white);
    legendGraphics.fillRect(0, 0, width, height);

    int offset = 0;
    for (RenderingDesign dd : ddList.getAllDesigns()) {
        int yPos = topMargin + offset * iconHeight + offset * verticalSpaceBetweenEntries;

        // icon:
        legendGraphics.setColor(dd.getColor());
        legendGraphics.fillRect(leftMargin, yPos, iconWidth, iconHeight);

        // text:
        legendGraphics.setColor(Color.black);

        legendGraphics.drawString(dd.getFeature().getLabel() + " - " + dd.getLabel(),
                leftMargin + iconWidth + horizontalSpaceBetweenIconAndText, yPos + iconHeight);

        offset++;
    }

    // draw legend into image:
    JPEGEncodeParam p = new JPEGEncodeParam();
    p.setQuality(1f);
    ImageEncoder encoder = ImageCodec.createImageEncoder("jpeg", out, p);

    encoder.encode(legendImage);
}