home *** CD-ROM | disk | FTP | other *** search
/ Online Praxis 1998 March / Image.iso / CD-ROM / HOMEPAGE / JAVA / ICE40.EXE / examples / fontApplet.java < prev    next >
Encoding:
Java Source  |  1998-03-05  |  1.5 KB  |  56 lines

  1. import java.awt.*;
  2.  
  3. /** read fonts used by the browser and use the same ones
  4.   */
  5. public class fontApplet extends java.applet.Applet
  6. {  
  7.         Font boldFont, bigBoldFont, smallBoldItalicFont;
  8.  
  9.    public void init() {
  10.       Font browserFont = getFont();
  11.       System.out.println("Browser font is "+browserFont);
  12.       boldFont = new Font(browserFont.getName(), Font.BOLD, browserFont.getSize());
  13.       bigBoldFont = new Font(browserFont.getName(), Font.BOLD, browserFont.getSize()+4);
  14.       smallBoldItalicFont = new Font(browserFont.getName(), Font.BOLD+Font.ITALIC, browserFont.getSize()-4);
  15.  
  16.    }
  17.  
  18.    public void paint(Graphics g)
  19.    {  
  20.       fm1 = g.getFontMetrics(boldFont);
  21.       fm2 = g.getFontMetrics(bigBoldFont);
  22.       fm3 = g.getFontMetrics(smallBoldItalicFont);
  23.       String s1 = "Not a ";
  24.       String s2 = "Hello, World";
  25.       String s3 = " Program";
  26.       int w1 = fm1.stringWidth(s1);
  27.       int w2 = fm2.stringWidth(s2);
  28.       int w3 = fm3.stringWidth(s3);
  29.  
  30.       Dimension d = size();
  31.       Insets in = insets();
  32.       int client_width = d.width - in.right - in.left;
  33.       int client_height = d.height - in.bottom - in.top;
  34.       int cx = (client_width - w1 - w2 - w3) / 2;
  35.       int cy = client_height / 2;
  36.       g.drawRect(0, 0, client_width - 1, client_height - 1);
  37.       
  38.       g.setFont(boldFont);
  39.       g.drawString(s1, cx, cy);
  40.       cx += w1;
  41.       g.setFont(bigBoldFont);
  42.       g.drawString(s2, cx, cy);
  43.       cx += w2;
  44.       g.setFont(smallBoldItalicFont);
  45.       g.drawString(s3, cx, cy);
  46.    }
  47.  
  48.    
  49.    private FontMetrics fm1, fm2, fm3;
  50.   
  51. }
  52.  
  53.  
  54.  
  55.  
  56.