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
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();
}
}
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
program below. (This uses some classes written by the author of theFrom the Beginning" (ISBN 0393974375). What I have to do is modify 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();
}
}