font definition - beginner's question

Discussion in 'Java' started by Joe, Apr 25, 2005.

  1. Joe

    Joe Guest

    I'm trying to write a program where the user enters the font name (e.g.
    SansSerif), font style (e.g. italic), and font size, and then in a
    suitably-sized new window, in the specified font, it displays various
    information about the font (its ascent, descent, leading, etc.).

    I can do the font name and size, but I can't work out how to convert
    the user's input for font style into a form that can be used in the
    definition of the new font. I.e. the first and third of the two
    arguments in the following statement work, but the middle one doesn't:
    Font f = new Font(fontName, "Font." + fontStyle, fontSize);

    Thanks very much for any help.

    ***************************************************

    Actually this is Programming Project 1 on p250 of "Java Programming
    >From the Beginning" (ISBN 0393974375). What I have to do is modify the

    program below. (This uses some classes written by the author of the
    book.)

    // Displays information about a font. Uses the FontMetrics
    // class to obtain the information to be displayed.

    import java.awt.*;
    import jpb.*;

    public class FontInfo {
    public static void main(String[] args) {
    // Create drawable frame
    DrawableFrame df = new DrawableFrame("Font Information");
    df.show();
    df.setSize(215, 175);

    // Get graphics context
    Graphics g = df.getGraphicsContext();

    // Create font, get metrics for font, and compute height
    // of font
    Font f = new Font("SansSerif", Font.PLAIN, 20);
    FontMetrics fm = g.getFontMetrics(f);
    int height = fm.getHeight();

    // Display information about font
    g.setFont(f);
    g.drawString("Ascent: " + fm.getAscent(), 10, height);
    g.drawString("Descent: " + fm.getDescent(), 10,
    height * 2);
    g.drawString("Height: " + height, 10, height * 3);
    g.drawString("Leading: " + fm.getLeading(), 10,
    height * 4);
    g.drawString("Maximum advance: " + fm.getMaxAdvance(), 10,
    height * 5);
    g.drawString("Width of \"Why?\": " +
    fm.stringWidth("Why?"), 10, height * 6);

    // Repaint frame
    df.repaint();
    }
    }
     
    Joe, Apr 25, 2005
    #1
    1. Advertising

  2. "Joe" <> schreef in bericht
    news:...
    > I'm trying to write a program where the user enters the font name (e.g.
    > SansSerif), font style (e.g. italic), and font size, and then in a
    > suitably-sized new window, in the specified font, it displays various
    > information about the font (its ascent, descent, leading, etc.).
    >
    > I can do the font name and size, but I can't work out how to convert
    > the user's input for font style into a form that can be used in the
    > definition of the new font. I.e. the first and third of the two
    > arguments in the following statement work, but the middle one doesn't:
    > Font f = new Font(fontName, "Font." + fontStyle, fontSize);


    As you can see in the Font constructor specification, the middle argument is
    an int, not a String. You would have to convert your fontStyle to the
    appropriate int value. I hope you can do that yourself.
     
    Boudewijn Dijkstra, Apr 25, 2005
    #2
    1. Advertising

  3. Joe wrote:
    > I'm trying to write a program where the user enters the font name (e.g.
    > SansSerif), font style (e.g. italic), and font size, and then in a
    > suitably-sized new window, in the specified font, it displays various
    > information about the font (its ascent, descent, leading, etc.).
    >
    > I can do the font name and size, but I can't work out how to convert
    > the user's input for font style into a form that can be used in the
    > definition of the new font. I.e. the first and third of the two
    > arguments in the following statement work, but the middle one doesn't:
    > Font f = new Font(fontName, "Font." + fontStyle, fontSize);
    >


    try:
    http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Font.html#decode(java.lang.String)

    Format your font request into a string and then pass it into Font.decode.

    eg.

    //---
    Font f = Font.decode("SansSerif ITALIC 12");
    //---

    --
    Peter MacMillan
    e-mail/msn:
     
    Peter MacMillan, Apr 25, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Karl Hungus

    Font class, external font files question

    Karl Hungus, Feb 4, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    535
    Karl Hungus
    Feb 4, 2004
  2. =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=

    No Class at ALL!!! beginner/beginner question

    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=, Feb 2, 2005, in forum: ASP .Net
    Replies:
    7
    Views:
    597
    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=
    Feb 3, 2005
  3. Jianli Shen
    Replies:
    1
    Views:
    594
    Victor Bazarov
    Mar 13, 2005
  4. Ark
    Replies:
    1
    Views:
    421
    Chris Torek
    Aug 7, 2004
  5. mttc
    Replies:
    2
    Views:
    2,408
    Roedy Green
    Jul 3, 2009
Loading...

Share This Page