font definition - beginner's question

J

Joe

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();
}
}
 
B

Boudewijn Dijkstra

Joe said:
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.
 
P

Peter MacMillan

Joe said:
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");
//---
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top