import java.awt.*;
import java.awt.event.*;
public class FontShow extends java.applet.Applet {
static final int LPAD=25; // Frilly line padding
boolean bigFont = true;
public void init() {
addMouseListener( new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
bigFont = !bigFont;
repaint();
}
} );
}
public void paint( Graphics g ) {
String message = getParameter( "word" );
g.drawRect(0, 0, getSize().width-1, getSize().height-1);
if ( bigFont )
g.setFont( new Font("Dialog",Font.PLAIN,24) );
else
g.setFont( new Font("Dialog",Font.PLAIN,12) );
FontMetrics metrics = g.getFontMetrics();
int fontAscent = metrics.getMaxAscent ();
int fontDescent = metrics.getMaxDescent();
int messWidth = metrics.stringWidth ( message );
// Center text
int startX = getSize().width/2 - messWidth/2;
int startY = getSize().height/2 - fontDescent/2 + fontAscent/2;
g.drawString(message, startX, startY);
g.setColor( Color.white ); // Base lines
g.drawLine( startX-LPAD, startY, startX+messWidth+LPAD, startY );
g.drawLine( startX, startY+ LPAD, startX, startY-fontAscent-LPAD );
g.setColor( Color.green ); // Ascent line
g.drawLine( startX-LPAD, startY-fontAscent, startX+messWidth+LPAD, startY-fontAscent );
g.setColor( Color.red ); // Descent line
g.drawLine( startX-LPAD, startY+fontDescent, startX+messWidth+LPAD, startY+fontDescent );
}
}
|