home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Java by Example
/
jbecd.bin
/
JBE-CD
/
NTUsers
/
JBECODE.ZIP
/
JavaByExample
/
chap17
/
MetricsApplet.java
< prev
Wrap
Text File
|
1996-02-22
|
1KB
|
49 lines
import java.awt.*;
import java.applet.*;
public class MetricsApplet extends Applet
{
TextField textField;
public void init()
{
textField = new TextField(20);
add(textField);
textField.setText("Default string");
}
public void paint(Graphics g)
{
Font font = getFont();
g.setFont(font);
FontMetrics fontMetrics = g.getFontMetrics(font);
int n = fontMetrics.getLeading();
String leading = String.valueOf(n);
n = fontMetrics.getAscent();
String ascent = String.valueOf(n);
n = fontMetrics.getDescent();
String descent = String.valueOf(n);
n = fontMetrics.getHeight();
String height = String.valueOf(n);
String s = textField.getText();
n = fontMetrics.stringWidth(s);
String width = String.valueOf(n);
g.drawString("FONT INFO:", 55, 60);
g.drawString("Leading: " + leading, 70, 80);
g.drawString("Ascent: " + ascent, 70, 95);
g.drawString("Descent: " + descent, 70, 110);
g.drawString("Height: " + height, 70, 125);
g.drawString("STRING INFO:", 55, 155);
g.drawString("Width: " + width, 70, 175);
}
public boolean action(Event event, Object arg)
{
repaint();
return true;
}
}