Example usage for java.awt FontMetrics getAscent

List of usage examples for java.awt FontMetrics getAscent

Introduction

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

Prototype

public int getAscent() 

Source Link

Document

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

Usage

From source file:net.sourceforge.msscodefactory.cflib.v1_11.CFLib.Swing.CFDoubleColumnCellRenderer.java

public void paint(Graphics g) {
    if (g == null) {
        return;/*from w  w  w. j a  v  a  2  s. c  o  m*/
    }
    Rectangle bounds = getBounds();
    g.setColor(getBackground());
    g.fill3DRect(0, 0, bounds.width, bounds.height, true);
    g.setColor(getForeground());
    String str;
    if (value instanceof Double) {
        Double val = (Double) value;
        Format fmt = getDefaultFormat();
        str = fmt.format(val);
    } else if (value instanceof String) {
        str = (String) value;
    } else {
        str = null;
    }
    if (str != null) {
        int firstNewline = str.indexOf('\n');
        if (firstNewline < 0) {
            firstNewline = str.indexOf('\r');
            if (firstNewline < 0) {
                firstNewline = str.indexOf('\f');
                if (firstNewline < 0) {
                    firstNewline = str.length();
                }
            }
        }
        String firstLine = str.substring(0, firstNewline);
        FontMetrics fm = g.getFontMetrics();
        int ascent = fm.getAscent();
        int leading = fm.getLeading();
        int width = fm.stringWidth(firstLine);
        int x = (bounds.width - 8) - width;
        g.drawString(firstLine, x, leading + ascent + 4);
    }
}

From source file:net.sourceforge.msscodefactory.cflib.v1_11.CFLib.Swing.CFFloatColumnCellRenderer.java

public void paint(Graphics g) {
    if (g == null) {
        return;/*from w ww .j a va  2  s .  com*/
    }
    Rectangle bounds = getBounds();
    g.setColor(getBackground());
    g.fill3DRect(0, 0, bounds.width, bounds.height, true);
    g.setColor(getForeground());
    String str;
    if (value instanceof Float) {
        Float val = (Float) value;
        Format fmt = getDefaultFormat();
        str = fmt.format(val);
    } else if (value instanceof String) {
        str = (String) value;
    } else {
        str = null;
    }
    if (str != null) {
        int firstNewline = str.indexOf('\n');
        if (firstNewline < 0) {
            firstNewline = str.indexOf('\r');
            if (firstNewline < 0) {
                firstNewline = str.indexOf('\f');
                if (firstNewline < 0) {
                    firstNewline = str.length();
                }
            }
        }
        String firstLine = str.substring(0, firstNewline);
        FontMetrics fm = g.getFontMetrics();
        int ascent = fm.getAscent();
        int leading = fm.getLeading();
        int width = fm.stringWidth(firstLine);
        int x = (bounds.width - 8) - width;
        g.drawString(firstLine, x, leading + ascent + 4);
    }
}

From source file:net.sourceforge.msscodefactory.cflib.v1_11.CFLib.Swing.CFUInt64ColumnCellRenderer.java

public void paint(Graphics g) {
    if (g == null) {
        return;/*ww  w.j  a  v  a 2 s  . c  o  m*/
    }
    Rectangle bounds = getBounds();
    g.setColor(getBackground());
    g.fill3DRect(0, 0, bounds.width, bounds.height, true);
    g.setColor(getForeground());
    String str;
    if (value instanceof BigDecimal) {
        BigDecimal val = (BigDecimal) value;
        Format fmt = getDefaultFormat();
        str = fmt.format(val);
    } else if (value instanceof String) {
        str = (String) value;
    } else {
        str = null;
    }
    if (str != null) {
        int firstNewline = str.indexOf('\n');
        if (firstNewline < 0) {
            firstNewline = str.indexOf('\r');
            if (firstNewline < 0) {
                firstNewline = str.indexOf('\f');
                if (firstNewline < 0) {
                    firstNewline = str.length();
                }
            }
        }
        String firstLine = str.substring(0, firstNewline);
        FontMetrics fm = g.getFontMetrics();
        int ascent = fm.getAscent();
        int leading = fm.getLeading();
        int width = fm.stringWidth(firstLine);
        int x = (bounds.width - 8) - width;
        g.drawString(firstLine, x, leading + ascent + 4);
    }
}

From source file:net.sourceforge.msscodefactory.cflib.v1_11.CFLib.Swing.CFTimeColumnCellRenderer.java

public void paint(Graphics g) {
    if (g == null) {
        return;//from   w w w .  j a  v a  2  s.  c om
    }
    Rectangle bounds = getBounds();
    g.setColor(getBackground());
    g.fill3DRect(0, 0, bounds.width, bounds.height, true);
    g.setColor(getForeground());
    String str;
    if (value instanceof Calendar) {
        Calendar cal = (Calendar) value;
        Format fmt = getDefaultFormat();
        Calendar withoutDate = Calendar.getInstance();
        withoutDate.clear();
        withoutDate.set(Calendar.HOUR_OF_DAY, cal.get(Calendar.HOUR_OF_DAY));
        withoutDate.set(Calendar.MINUTE, cal.get(Calendar.MINUTE));
        withoutDate.set(Calendar.SECOND, cal.get(Calendar.SECOND));
        str = fmt.format(withoutDate.getTime());
    } else if (value instanceof String) {
        str = (String) value;
    } else {
        str = null;
    }
    if (str != null) {
        int firstNewline = str.indexOf('\n');
        if (firstNewline < 0) {
            firstNewline = str.indexOf('\r');
            if (firstNewline < 0) {
                firstNewline = str.indexOf('\f');
                if (firstNewline < 0) {
                    firstNewline = str.length();
                }
            }
        }
        String firstLine = str.substring(0, firstNewline);
        FontMetrics fm = g.getFontMetrics();
        int ascent = fm.getAscent();
        int leading = fm.getLeading();
        g.drawString(firstLine, 4, leading + ascent + 4);
    }
}

From source file:Main.java

public void render(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    g2d.setColor(Color.WHITE);// www.j  a  v  a 2  s. co  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:metrics.java

License:asdf

public void paint(Graphics g) {
    g.translate(100, 100);/*from  ww  w  . j  av a  2  s  .  com*/
    FontMetrics fm = null;
    int ascent, descent, leading, width1, width2, height;
    String string1 = "dsdas";
    String string2 = "asdf";
    int xPos = 25, yPos = 50;
    fm = g.getFontMetrics();
    ascent = fm.getAscent();
    descent = fm.getDescent();
    leading = fm.getLeading();
    height = fm.getHeight();
    width1 = fm.stringWidth(string1);
    width2 = fm.stringWidth(string2);
    g.drawString(string1, xPos, yPos);
    g.drawLine(xPos, yPos - ascent - leading, xPos + width1, yPos - ascent - leading);
    g.drawLine(xPos, yPos - ascent, xPos + width1, yPos - ascent);
    g.drawLine(xPos, yPos, xPos + width1, yPos);
    g.drawLine(xPos, yPos + descent, xPos + width1, yPos + descent);
    g.drawString(string2, xPos, yPos + height);
    g.drawLine(xPos, yPos - ascent - leading + height, xPos + width2, yPos - ascent - leading + height);
    g.drawLine(xPos, yPos - ascent + height, xPos + width2, yPos - ascent + height);
    g.drawLine(xPos, yPos + height, xPos + width2, yPos + height);
    g.drawLine(xPos, yPos + descent + height, xPos + width2, yPos + descent + height);

}

From source file:Main.java

public void paint(Graphics g) {
    g.translate(100, 100);//from  w w  w  . j ava  2 s.co  m
    FontMetrics fm = null;
    int ascent, descent, leading, width1, width2, height;
    String string1 = "www.java2s.com";
    String string2 = "java2s.com";
    int xPos = 25, yPos = 50;
    fm = g.getFontMetrics();
    ascent = fm.getAscent();
    descent = fm.getDescent();
    leading = fm.getLeading();
    height = fm.getHeight();
    width1 = fm.stringWidth(string1);
    width2 = fm.stringWidth(string2);
    g.drawString(string1, xPos, yPos);
    g.drawLine(xPos, yPos - ascent - leading, xPos + width1, yPos - ascent - leading);
    g.drawLine(xPos, yPos - ascent, xPos + width1, yPos - ascent);
    g.drawLine(xPos, yPos, xPos + width1, yPos);
    g.drawLine(xPos, yPos + descent, xPos + width1, yPos + descent);
    g.drawString(string2, xPos, yPos + height);
    g.drawLine(xPos, yPos - ascent - leading + height, xPos + width2, yPos - ascent - leading + height);
    g.drawLine(xPos, yPos - ascent + height, xPos + width2, yPos - ascent + height);
    g.drawLine(xPos, yPos + height, xPos + width2, yPos + height);
    g.drawLine(xPos, yPos + descent + height, xPos + width2, yPos + descent + height);

}

From source file:net.sourceforge.msscodefactory.cflib.v1_11.CFLib.Swing.CFNumberColumnCellRenderer.java

public void paint(Graphics g) {
    if (g == null) {
        return;/*from   w  w  w  .j  a v  a 2 s  .c o m*/
    }
    Rectangle bounds = getBounds();
    g.setColor(getBackground());
    g.fill3DRect(0, 0, bounds.width, bounds.height, true);
    g.setColor(getForeground());
    String str;
    if (value instanceof BigDecimal) {
        BigDecimal val = (BigDecimal) value;
        Format fmt = getNumberFormat(digits, precis);
        str = fmt.format(val);
    } else if (value instanceof String) {
        str = (String) value;
    } else {
        str = null;
    }
    if (str != null) {
        int firstNewline = str.indexOf('\n');
        if (firstNewline < 0) {
            firstNewline = str.indexOf('\r');
            if (firstNewline < 0) {
                firstNewline = str.indexOf('\f');
                if (firstNewline < 0) {
                    firstNewline = str.length();
                }
            }
        }
        String firstLine = str.substring(0, firstNewline);
        FontMetrics fm = g.getFontMetrics();
        int ascent = fm.getAscent();
        int leading = fm.getLeading();
        int width = fm.stringWidth(firstLine);
        int x = (bounds.width - 8) - width;
        g.drawString(firstLine, x, leading + ascent + 4);
    }
}

From source file:net.sourceforge.msscodefactory.cflib.v1_11.CFLib.Swing.CFBoolColumnCellRenderer.java

public void paint(Graphics g) {
    if (g == null) {
        return;/* w  w w .j ava 2  s.com*/
    }
    Rectangle bounds = getBounds();
    g.setColor(getBackground());
    g.fill3DRect(0, 0, bounds.width, bounds.height, true);
    g.setColor(getForeground());
    int xoffbox = (bounds.width - 16) / 2;
    int yoffbox = (bounds.height - 16) / 2;
    g.drawRect(xoffbox, yoffbox, 16, 16);
    Boolean useval;
    if (value instanceof Boolean) {
        useval = (Boolean) value;
    } else if (value instanceof String) {
        String s = (String) value;
        if (s.equals("true")) {
            useval = new Boolean(true);
        } else if (s.equals("false")) {
            useval = new Boolean(false);
        } else {
            useval = null;
        }
    } else {
        useval = null;
    }
    String str;
    if (useval == null) {
        str = "?";
    } else if (useval.booleanValue()) {
        str = "X";
    } else {
        str = null;
    }
    if (str != null) {
        FontMetrics fm = g.getFontMetrics();
        Rectangle sb = fm.getStringBounds(str, g).getBounds();
        int ascent = fm.getAscent();
        int leading = fm.getLeading();
        int sxoff = xoffbox + ((16 - sb.width) / 2);
        int syoff = yoffbox + ((16 - (leading + ascent)) / 2) + leading + ascent;
        g.drawString(str, sxoff, syoff);
    }
}

From source file:net.sourceforge.msscodefactory.cflib.v1_11.CFLib.Swing.CFHeaderColumnCellRenderer.java

public void paint(Graphics g) {
    if (g == null) {
        return;/*from  www . ja v a 2 s.  c  om*/
    }
    Rectangle bounds = getBounds();
    g.setColor(getBackground());
    g.fill3DRect(0, 0, bounds.width, bounds.height, true);
    g.setColor(getForeground());
    String str;
    if (value instanceof String) {
        str = (String) value;
    } else {
        str = null;
    }
    if (str != null) {
        int firstNewline = str.indexOf('\n');
        if (firstNewline < 0) {
            firstNewline = str.indexOf('\r');
            if (firstNewline < 0) {
                firstNewline = str.indexOf('\f');
                if (firstNewline < 0) {
                    firstNewline = str.length();
                }
            }
        }
        String firstLine = str.substring(0, firstNewline);
        FontMetrics fm = g.getFontMetrics();
        int width = fm.stringWidth(firstLine);
        int x = (bounds.width - width) / 2;
        int ascent = fm.getAscent();
        int leading = fm.getLeading();
        g.drawString(firstLine, x, leading + ascent + 4);
    }
}