Example usage for java.awt FontMetrics getDescent

List of usage examples for java.awt FontMetrics getDescent

Introduction

In this page you can find the example usage for java.awt FontMetrics getDescent.

Prototype

public int getDescent() 

Source Link

Document

Determines the font descent of the Font described by this FontMetrics object.

Usage

From source file:Main.java

public static void main(String[] args) {
    String s = "The quick brown fox jumps over the lazy dog!";
    BufferedImage bi = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
    Graphics g = bi.getGraphics();
    FontMetrics fm = g.getFontMetrics();
    Rectangle2D b = fm.getStringBounds(s, g);
    System.out.println(b);//from  w w w .  j a v a  2s .  com
    bi = new BufferedImage((int) b.getWidth(), (int) (b.getHeight() + fm.getDescent()),
            BufferedImage.TYPE_INT_RGB);
    g = bi.getGraphics();
    g.drawString(s, 0, (int) b.getHeight());

    JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(bi)));

    // Technique 3 - JLabel
    JLabel l = new JLabel(s);
    l.setSize(l.getPreferredSize());
    bi = new BufferedImage(l.getWidth(), l.getHeight(), BufferedImage.TYPE_INT_RGB);
    g = bi.getGraphics();
    g.setColor(Color.WHITE);
    g.fillRect(0, 0, 400, 100);
    l.paint(g);

    JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(bi)));
}

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 w  w. j a v  a2 s .  c o m*/

    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: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);/* w  w  w . j a  v a  2s .  co  m*/
}

From source file:Main.java

/**
 * Paints string with underlined character at the specified index.
 *
 * @param g               graphics context
 * @param text            painted text//from   ww w.j  a v a 2 s. co  m
 * @param underlinedIndex underlined character index
 * @param x               text X coordinate
 * @param y               text Y coordinate
 */
public static void drawStringUnderlineCharAt(final Graphics g, final String text, final int underlinedIndex,
        final int x, final int y) {
    // Painting string
    drawString(g, text, x, y);

    // Painting character underline
    if (underlinedIndex >= 0 && underlinedIndex < text.length()) {
        final FontMetrics fm = g.getFontMetrics();
        g.fillRect(x + fm.stringWidth(text.substring(0, underlinedIndex)), y + fm.getDescent() - 1,
                fm.charWidth(text.charAt(underlinedIndex)), 1);
    }
}

From source file:GraphicsUtil.java

static public Rectangle getTextBounds(Graphics g, String text, int x, int y, int halign, int valign) {
    if (g == null)
        return new Rectangle(x, y, 0, 0);
    FontMetrics mets = g.getFontMetrics();
    int width = mets.stringWidth(text);
    int ascent = mets.getAscent();
    int height = ascent + mets.getDescent();
    Rectangle ret = new Rectangle(x, y, width, height);

    switch (halign) {
    case H_CENTER:
        ret.translate(-(width / 2), 0);// w  w  w  .ja  v  a 2  s .  c  om
        break;
    case H_RIGHT:
        ret.translate(-width, 0);
        break;
    default:
        ;
    }
    switch (valign) {
    case V_TOP:
        break;
    case V_CENTER:
        ret.translate(0, -(ascent / 2));
        break;
    case V_BASELINE:
        ret.translate(0, -ascent);
        break;
    case V_BOTTOM:
        ret.translate(0, -height);
        break;
    default:
        ;
    }
    return ret;
}

From source file:Main.java

public static Rectangle calculateBoundsForComponent(JComponent comp, String text, int xOffset, int yOffset) {

    FontMetrics fm = comp.getFontMetrics(comp.getFont());
    Rectangle2D bounds = fm.getStringBounds(text, comp.getGraphics());

    return new Rectangle(xOffset + (int) bounds.getX(), yOffset,
            (int) Math.ceil(bounds.getWidth()) + (PADDING * 2),
            (int) Math.ceil(bounds.getHeight() + fm.getDescent()) + (PADDING * 2));
}

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 ww.j a va 2  s. co 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:org.ut.biolab.medsavant.client.util.ClientMiscUtils.java

/**
 * Draws a string centred in the given box.
 *//*from w w w .ja  v a2s  .co  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.opensha.commons.util.FileUtils.java

/**
 * Prints a Text file//from   ww  w  .  ja va2s .c o  m
 * @param pjob PrintJob  created using getToolkit().getPrintJob(JFrame,String,Properties);
 * @param pg Graphics
 * @param textToPrint String
 */
public static void print(PrintJob pjob, Graphics pg, String textToPrint) {

    int margin = 60;

    int pageNum = 1;
    int linesForThisPage = 0;
    int linesForThisJob = 0;
    // Note: String is immutable so won't change while printing.
    if (!(pg instanceof PrintGraphics)) {
        throw new IllegalArgumentException("Graphics context not PrintGraphics");
    }
    StringReader sr = new StringReader(textToPrint);
    LineNumberReader lnr = new LineNumberReader(sr);
    String nextLine;
    int pageHeight = pjob.getPageDimension().height - margin;
    Font helv = new Font("Monaco", Font.PLAIN, 12);
    //have to set the font to get any output
    pg.setFont(helv);
    FontMetrics fm = pg.getFontMetrics(helv);
    int fontHeight = fm.getHeight();
    int fontDescent = fm.getDescent();
    int curHeight = margin;
    try {
        do {
            nextLine = lnr.readLine();
            if (nextLine != null) {
                if ((curHeight + fontHeight) > pageHeight) {
                    // New Page
                    if (linesForThisPage == 0)
                        break;

                    pageNum++;
                    linesForThisPage = 0;
                    pg.dispose();
                    pg = pjob.getGraphics();
                    if (pg != null) {
                        pg.setFont(helv);
                    }
                    curHeight = 0;
                }
                curHeight += fontHeight;
                if (pg != null) {
                    pg.drawString(nextLine, margin, curHeight - fontDescent);
                    linesForThisPage++;

                    linesForThisJob++;
                }
            }
        } while (nextLine != null);
    } catch (EOFException eof) {
        // Fine, ignore
    } catch (Throwable t) { // Anything else
        t.printStackTrace();
    }
}

From source file:edu.ku.brc.ui.GraphicsUtils.java

/**
 * Draws the given <code>String</code>, centered at <code>(x,y)</code>, using
 * the given graphics context.//from   w  w  w.  j ava  2  s  .c o m
 * 
 * @param s the string
 * @param g the graphics context to draw in
 * @param x the x coordinate for center of the <code>String</code>
 * @param y the y coordinate for center of the <code>String</code>
 */
public static void drawCenteredString(String s, Graphics g, int x, int y) {
    ((Graphics2D) g).addRenderingHints(hints);

    FontMetrics fm = g.getFontMetrics();
    int ht = fm.getAscent() + fm.getDescent();
    int width = fm.stringWidth(s);
    g.drawString(s, x - width / 2, y + (fm.getAscent() - ht / 2));
}