Example usage for java.awt FontMetrics getHeight

List of usage examples for java.awt FontMetrics getHeight

Introduction

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

Prototype

public int getHeight() 

Source Link

Document

Gets the standard height of a line of text in this font.

Usage

From source file:Main.java

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g.create();
    String text = "I don't see the problem";
    FontMetrics fm = g2d.getFontMetrics();
    int x = (getWidth() - fm.stringWidth(text)) / 2;
    int y = ((getHeight() - fm.getHeight()) / 2) + fm.getDescent();
    g2d.setTransform(AffineTransform.getRotateInstance(Math.toRadians(45), getWidth() / 2, getHeight() / 2));
    g2d.drawString(text, x, y);/*  ww w.  jav  a2 s.  c o m*/
    g2d.dispose();
}

From source file:Main.java

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g.create();

    g2d.setColor(Color.RED);/* ww w  . j  a va  2  s  .c  o  m*/
    g2d.drawLine(getWidth() / 2, 0, getWidth() / 2, getHeight());
    g2d.drawLine(0, getHeight() / 2, getWidth(), getHeight() / 2);

    Font font = new Font("Arial", Font.BOLD, 48);
    g2d.setFont(font);
    FontMetrics fm = g2d.getFontMetrics();
    int x = ((getWidth() - fm.stringWidth(text)) / 2);
    int y = ((getHeight() - fm.getHeight()) / 2) + fm.getAscent();

    g2d.setColor(Color.BLACK);
    g2d.drawString(text, x, y);

    g2d.dispose();
}

From source file:TextBox3D.java

public synchronized void paint(Graphics g) {
    FontMetrics fm = g.getFontMetrics();
    Dimension size = getSize();/*  w  w  w.j  a  v  a  2s. c  o  m*/
    int x = (size.width - fm.stringWidth(text)) / 2;
    int y = (size.height - fm.getHeight()) / 2;
    g.setColor(SystemColor.control);
    g.fillRect(0, 0, size.width, size.height);
    g.setColor(SystemColor.controlShadow);
    g.drawLine(0, 0, 0, size.height - 1);
    g.drawLine(0, 0, size.width - 1, 0);
    g.setColor(SystemColor.controlDkShadow);
    g.drawLine(0, size.height - 1, size.width - 1, size.height - 1);
    g.drawLine(size.width - 1, 0, size.width - 1, size.height - 1);
    g.setColor(SystemColor.controlText);
    g.drawString(text, x, y);
}

From source file:de.codesourcery.eve.skills.ui.utils.ResizingTextField.java

public ResizingTextField(String text) {
    super(text);//from   w w  w .j  av  a 2 s  .  c  o m

    FontMetrics fm = this.getFontMetrics(this.getFont());
    int height = fm.getHeight();

    this.setMaximumSize(new java.awt.Dimension(10000, height + 6));
}

From source file:Main.java

public void render(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    g2d.setColor(Color.WHITE);//w  w w.  ja  va  2 s  .  c o m
    Font font = new Font("Verdana", Font.PLAIN, 20);
    g2d.setFont(font);
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    FontMetrics fm = g2d.getFontMetrics();

    String option = "This is a test";

    int x = (getWidth() - fm.stringWidth(option)) / 2;
    int y = ((getHeight() - fm.getHeight()) / 2);
    g2d.drawString(option, x, y + fm.getAscent());
    g2d.drawRect((int) x - 20, (int) y - 10, (int) fm.stringWidth(option) + 40, (int) fm.getHeight() + 20);
}

From source file:de.codesourcery.eve.skills.ui.utils.ResizingTextField.java

protected Dimension calcSize(String text) {
    final FontMetrics fm = this.getFontMetrics(this.getFont());
    final int height = fm.getHeight();

    final int colCount = Math.max(getColumns(), 1);
    final String realText = StringUtils.isBlank(text) ? StringUtils.leftPad("X", colCount, " ")
            : " " + text + " ";

    int width = (int) (1.2f * fm.stringWidth(realText));
    return new Dimension(width, height + 6);
}

From source file:Center.java

License:asdf

public void paint(Graphics g) {
    g.translate(100, 100);//  w  w  w  . ja va  2s  .co  m
    FontMetrics fm = g.getFontMetrics();
    for (int i = 0; i < text.length; i++) {
        int x, y;
        x = (getWidth() - fm.stringWidth(text[i])) / 2;
        y = (i + 1) * fm.getHeight() - 1;
        g.drawString(text[i], x, y);
    }

}

From source file:ImageLabel.java

public Dimension getPreferredSize() {
    FontMetrics metrics = img.getGraphics().getFontMetrics(font);
    int width = metrics.stringWidth(text) * 2;
    int height = metrics.getHeight() * 2;
    return new Dimension(width, height);
}

From source file:PaginationExample.java

public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException {

    Font font = new Font("Serif", Font.PLAIN, 10);
    FontMetrics metrics = g.getFontMetrics(font);
    int lineHeight = metrics.getHeight();

    if (pageBreaks == null) {
        initTextLines();/*  w  ww .  ja v a  2s. com*/
        int linesPerPage = (int) (pf.getImageableHeight() / lineHeight);
        int numBreaks = (textLines.length - 1) / linesPerPage;
        pageBreaks = new int[numBreaks];
        for (int b = 0; b < numBreaks; b++) {
            pageBreaks[b] = (b + 1) * linesPerPage;
        }
    }

    if (pageIndex > pageBreaks.length) {
        return NO_SUCH_PAGE;
    }

    /*
     * User (0,0) is typically outside the imageable area, so we must translate
     * by the X and Y values in the PageFormat to avoid clipping Since we are
     * drawing text we
     */
    Graphics2D g2d = (Graphics2D) g;
    g2d.translate(pf.getImageableX(), pf.getImageableY());

    /*
     * Draw each line that is on this page. Increment 'y' position by lineHeight
     * for each line.
     */
    int y = 0;
    int start = (pageIndex == 0) ? 0 : pageBreaks[pageIndex - 1];
    int end = (pageIndex == pageBreaks.length) ? textLines.length : pageBreaks[pageIndex];
    for (int line = start; line < end; line++) {
        y += lineHeight;
        g.drawString(textLines[line], 0, y);
    }

    /* tell the caller that this page is part of the printed document */
    return PAGE_EXISTS;
}

From source file:Main.java

License:asdf

public void paint(Graphics g, JComponent c) {
    FontMetrics metrics = c.getFontMetrics(g.getFont());
    g.setColor(c.getForeground());//from   w w  w . j  a  v a 2 s .  co m
    g.drawString(((JToolTip) c).getTipText(), 1, 1);
    g.drawImage(new ImageIcon("yourImage").getImage(), 1, metrics.getHeight(), c);
}