Example usage for java.awt Font Font

List of usage examples for java.awt Font Font

Introduction

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

Prototype

private Font(String name, int style, float sizePts) 

Source Link

Usage

From source file:Main.java

public void paint(Graphics g) {
    int fontSize = 20;

    g.setFont(new Font("TimesRoman", Font.BOLD, fontSize));

    String s = "www.java2s.com";

    g.setColor(Color.black);/*from   w w  w .  j a va  2  s.c o m*/
    g.drawString(s, 30, 30);
}

From source file:Main.java

/**
 * Returns an array of fonts created using specified array of font names.
 *
 * @param fontNames//from  w w  w.java 2s  .  c  o  m
 *            array of font names
 * @return an array of fonts
 */
public static Font[] createFonts(final String[] fontNames) {
    final Font[] fonts = new Font[fontNames.length];
    for (int i = 0; i < fontNames.length; i++) {
        fonts[i] = new Font(fontNames[i], Font.PLAIN, 13);
    }
    return fonts;
}

From source file:Main.java

public void paint(Graphics g) {
    int fontSize = 20;
    Font font = new Font("TimesRoman", Font.PLAIN, fontSize);
    g.setFont(font);/*w  w w .j  a  v a2 s.co  m*/

    font.canDisplay('A');

    String s = "www.java2s.com";

    g.setColor(Color.black);
    g.drawString(s, 30, 30);
}

From source file:StringRectPaintPanel.java

public void paint(Graphics g) {
    g.setFont(new Font("", 0, 100));
    FontMetrics fm = getFontMetrics(new Font("", 0, 100));
    String s = "java2s";
    int x = 5;//from w  w  w .  j  a va 2  s.  c o  m
    int y = 5;

    for (int i = 0; i < s.length(); i++) {
        char c = s.charAt(i);

        int h = fm.getHeight();
        int w = fm.charWidth(c);

        g.drawRect(x, y, w, h);
        g.drawString(String.valueOf(c), x, y + h);

        x = x + w;
    }
}

From source file:Main.java

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

    Font f = new Font("Arial", Font.PLAIN, 48);
    g2.setFont(f);//from   w  ww  .  j a va 2s.c  om

    g2.drawString("www.java2s.com", 10, 60);

    RenderingHints rh = g2.getRenderingHints();
    rh.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setRenderingHints(rh);

    g2.drawString("Antialiased text", 10, 120);
}

From source file:Main.java

/**
 * Sets component font name.//w  w  w.j a  v  a  2s  . c o m
 *
 * @param component component font name
 * @param fontName  new font name
 * @param <C>       component type
 * @return modified component
 */
public static <C extends Component> C setFontName(final C component, final String fontName) {
    if (component != null && component.getFont() != null) {
        final Font oldFont = component.getFont();
        component.setFont(new Font(fontName, oldFont.getStyle(), oldFont.getSize()));
    }
    return component;
}

From source file:MainClass.java

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

    Font f = new Font("Arial", Font.PLAIN, 48);
    g2.setFont(f);/*from w  ww .  ja  v a2 s . c  om*/

    g2.drawString("www.java2s.com", 10, 60);

    RenderingHints rh = g2.getRenderingHints();
    rh.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setRenderingHints(rh);

    g2.drawString("Antialiased text", 10, 120);

}

From source file:MainClass.java

public void paint(Graphics g) {
    Dimension d = this.getPreferredSize();
    int fontSize = 20;

    g.setFont(new Font("TimesRoman", Font.PLAIN, fontSize));

    g.setColor(Color.red);/*from www  .  j  av  a2  s . c o  m*/

    g.drawString("www.java2s.com", 10, 20);
}

From source file:Main.java

public void paint(Graphics g) {

    int fontSize = 20;

    g.setFont(new Font("TimesRoman", Font.PLAIN, fontSize));

    FontMetrics fm = g.getFontMetrics();

    String s = "www.java2s.com";

    int stringWidth = fm.stringWidth(s);

    int w = 200;//from ww w.  java  2  s. c  o  m
    int h = 200;

    int x = (w - stringWidth) / 2;
    int baseline = fm.getMaxAscent() + (h - (fm.getAscent() + fm.getMaxDecent())) / 2;
    int ascent = fm.getMaxAscent();
    int descent = fm.getMaxDecent();
    int fontHeight = fm.getMaxAscent() + fm.getMaxDecent();

    g.setColor(Color.white);
    g.fillRect(x, baseline - ascent, stringWidth, fontHeight);

    g.setColor(Color.gray);
    g.drawLine(x, baseline, x + stringWidth, baseline);

    g.setColor(Color.red);
    g.drawLine(x, baseline + descent, x + stringWidth, baseline + descent);

    g.setColor(Color.blue);
    g.drawLine(x, baseline - ascent, x + stringWidth, baseline - ascent);
    g.setColor(Color.black);
    g.drawString(s, x, baseline);

}

From source file:Main.java

public void paint(Graphics g) {

    int fontSize = 20;

    Font font = new Font("TimesRoman", Font.PLAIN, fontSize);
    g.setFont(font);//from   w w w.j a  v  a  2  s. c  o  m

    FontMetrics fm = g.getFontMetrics(font);

    String s = "www.java2s.com";

    int stringWidth = fm.stringWidth(s);

    int w = 200;
    int h = 200;

    int x = (w - stringWidth) / 2;
    int baseline = fm.getMaxAscent() + (h - (fm.getAscent() + fm.getMaxDecent())) / 2;
    int ascent = fm.getMaxAscent();
    int descent = fm.getMaxDecent();
    int fontHeight = fm.getMaxAscent() + fm.getMaxDecent();

    g.setColor(Color.white);
    g.fillRect(x, baseline - ascent, stringWidth, fontHeight);

    g.setColor(Color.gray);
    g.drawLine(x, baseline, x + stringWidth, baseline);

    g.setColor(Color.red);
    g.drawLine(x, baseline + descent, x + stringWidth, baseline + descent);

    g.setColor(Color.blue);
    g.drawLine(x, baseline - ascent, x + stringWidth, baseline - ascent);
    g.setColor(Color.black);
    g.drawString(s, x, baseline);

}