20-CS-694 Advanced Programming Techniques Spring 2012
Graphics

Interfaces, Exceptions, Graphics, Animation, Threads, Reflection, Networking, RMI, JDBC, JNI

Applet Source     All lectures    
Fonts Applet

Observe: Fonts show even if they are not installed on your system because they are obtained via the following (using a specific font file prefix) over the internet:

  Font f;
  int size = 18;
  InputStream is;
  String host = "http://gauss.ececs.uc.edu/Courses/c694/fonts/";
  String font = "Aliens_ate_my_mum";
  try {
     is = (new URL(host+font+".ttf")).openStream();
  } catch (Exception e) {
     System.out.println("Cannot get connection: ["+host+font+"]");
     return;
  }
  try {
     f = Font.createFont(Font.TRUETYPE_FONT, is);
  } catch (Exception e) {
     f = new Font(font, 0, fp.size);
     System.out.println("Using local font");
  }
  f = f.deriveFont((float)(size*1.0));
The font files are normally in:
  /usr/java/jre1.7.0_04/lib/fonts
Font files for this demo are in gauss/Courses/c694/fonts for convenience.

If the Frame does not close on "Quit" then adding

   permission java.lang.RuntimePermission "exitVM";
to your .java.policy file might do the trick.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.*;
import java.net.*;
import java.io.*;

class FontsPanel extends JPanel {
   Font f = null;
   int size = 16;

   public void paint (Graphics g) {
      FontMetrics fm = getFontMetrics(f);
      //System.out.println("Printing with font: "+f.toString());
      g.setFont(f);
      g.setColor(Color.blue);
      String symbs[] = {
         "\000", "\001", "\002", "\003", "\004", "\005", "\006", "\007",
         "\010", "\011", "\012", "\013", "\014", "\015", "\016", "\017",
         "\020", "\021", "\022", "\023", "\024", "\025", "\026", "\027",
         "\030", "\031", "\032", "\033", "\034", "\035", "\036", "\037",
         "\040", "\041", "\042", "\043", "\044", "\045", "\046", "\047",
         "\050", "\051", "\052", "\053", "\054", "\055", "\056", "\057",
         "\060", "\061", "\062", "\063", "\064", "\065", "\066", "\067",
         "\070", "\071", "\072", "\073", "\074", "\075", "\076", "\077",
         "\100", "\101", "\102", "\103", "\104", "\105", "\106", "\107",
         "\110", "\111", "\112", "\113", "\114", "\115", "\116", "\117",
         "\120", "\121", "\122", "\123", "\124", "\125", "\126", "\127",
         "\130", "\131", "\132", "\133", "\134", "\135", "\136", "\137",
         "\140", "\141", "\142", "\143", "\144", "\145", "\146", "\147",
         "\150", "\151", "\152", "\153", "\154", "\155", "\156", "\157",
         "\160", "\161", "\162", "\163", "\164", "\165", "\166", "\167",
         "\170", "\171", "\172", "\173", "\174", "\175", "\176", "\177",
         "\200", "\201", "\202", "\203", "\204", "\205", "\206", "\207",
         "\210", "\211", "\212", "\213", "\214", "\215", "\216", "\217",
         "\220", "\221", "\222", "\223", "\224", "\225", "\226", "\227",
         "\230", "\231", "\232", "\233", "\234", "\235", "\236", "\237",
         "\240", "\241", "\242", "\243", "\244", "\245", "\246", "\247",
         "\250", "\251", "\252", "\253", "\254", "\255", "\256", "\257",
         "\260", "\261", "\262", "\263", "\264", "\265", "\266", "\267",
         "\270", "\271", "\272", "\273", "\274", "\275", "\276", "\277",
         "\300", "\301", "\302", "\303", "\304", "\305", "\306", "\307",
         "\310", "\311", "\312", "\313", "\314", "\315", "\316", "\317",
         "\320", "\321", "\322", "\323", "\324", "\325", "\326", "\327",
         "\330", "\331", "\332", "\333", "\334", "\335", "\336", "\337",
         "\340", "\341", "\342", "\343", "\344", "\345", "\346", "\347",
         "\350", "\351", "\352", "\353", "\354", "\355", "\356", "\357",
         "\360", "\361", "\362", "\363", "\364", "\365", "\366", "\367",
         "\370", "\371", "\372", "\373", "\374", "\375", "\376", "\377"
      };
      
      for (int i=0 ; i < 16 ; i++) {
         for (int j=0 ; j < 16 ; j++) {
            g.drawString(symbs[i*16+j], 40+(j*size*30)/20, 100+(i*size*40)/20);
         }
      }
      g.drawString("The Quick Brown Fox Jumped Over The Lazy Dog's Back",20,110+(16*size*40)/20);
   }
}

class FontTestMenuTitle extends JFrame implements ItemListener, ActionListener {
   String font = "Aliens ate my mum";
   int type = 0;
   JComboBox fontsA, sizes;
   JButton button;
   FontsPanel fp;
   
   public FontTestMenuTitle () {
      setLayout(new BorderLayout());
      setBackground(new Color(255,255,255));
      JPanel q = new JPanel();
      q.add(fontsA = new JComboBox());
      fontsA.setBackground(new Color(255,239,213));

      fontsA.setFont(new Font("TimesRoman",Font.PLAIN, 14));
      fontsA.addItem("Aliens_ate_my_mum");
      fontsA.addItem("alienwarping");
      fontsA.addItem("Alison_Regular");
      fontsA.addItem("Alix2");
      fontsA.addItem("All_Caps");
      fontsA.addItem("AllHookedUp");
      fontsA.addItem("Alpha_Dance");
      fontsA.addItem("Alpha_CLOWN");
      fontsA.addItem("Becker-Medium");
      fontsA.addItem("Bellyfish");
      fontsA.addItem("Bernadette_Italic");
      fontsA.addItem("Blood_Omen");
      fontsA.addItem("Celeste_Normal");
      fontsA.addItem("Creepygirl");
      fontsA.addItem("Cretino");
      fontsA.addItem("CropBats_II_AOE");
      fontsA.addItem("Crumble");
      fontsA.addItem("Dahrlin_Regular");
      fontsA.addItem("DavysDingbats1");
      fontsA.addItem("DavysDingbats2");
      fontsA.addItem("Denial2RegularItalics");
      fontsA.addItem("DiPed_Thick");
      fontsA.addItem("Dingbats1");
      fontsA.addItem("Diploma_Regular");
      fontsA.addItem("DoodleDingbatsFiveSSK");
      fontsA.addItem("DoodleDingbatsFourSSK");
      fontsA.addItem("DoodleDingbatsSevenSSK");
      fontsA.addItem("DoodleDingbatsThreeSSK");
      fontsA.addItem("DoodleDingbatsTwoSSK");
      fontsA.addItem("Draconian_Regular");
      fontsA.addItem("GohaTibebZemen");
      fontsA.addItem("jungodingbats");
      fontsA.addItem("LucidaBrightDemiBold");
      fontsA.addItem("LucidaBrightDemiItalic");
      fontsA.addItem("LucidaBrightItalic");
      fontsA.addItem("LucidaBrightRegular");
      fontsA.addItem("LucidaSansDemiBold");
      fontsA.addItem("LucidaSansRegular");
      fontsA.addItem("LucidaTypewriterBold");
      fontsA.addItem("LucidaTypewriterRegular");
      fontsA.addItem("ZapfDingbats");
      q.add(sizes = new JComboBox());
      sizes.setBackground(new Color(255,239,213));
      sizes.setFont(new Font("TimesRoman",Font.PLAIN, 14));                
      sizes.addItem("16");
      sizes.addItem("24");
      sizes.addItem("36");
      sizes.addItem("48");
      sizes.addItem("60");
      sizes.addItem("72");
      sizes.addItem("84");
      sizes.addItem("96");
      q.add(button = new JButton("Quit"));
      q.add(new JLabel());
      add("North", q);
      add("Center", fp = new FontsPanel());
      fontsA.addItemListener(this);
      sizes.addItemListener(this);
      button.addActionListener(this);

      InputStream is = null;
      String host = "http://gauss.ececs.uc.edu/Courses/c694/fonts/";
      String font = "Aliens_ate_my_mum";
      try {
         is = (new URL(host+font+".ttf")).openStream();
      } catch (Exception e) {
         System.out.println("Cannot get connection: ["+host+font+"]");
         return;
      }
      try {
         fp.f = Font.createFont(Font.TRUETYPE_FONT, is);
      } catch (Exception e) {
         fp.f = new Font(font, 0, fp.size);
         System.out.println("Using local font");
      }
      fp.f = fp.f.deriveFont((float)(fp.size*1.0));
   }
   
   public void go () {
      setSize((620*fp.size)/19, 60+(750*fp.size)/19);
      setVisible(true);
      repaint();
   }
   
   public void itemStateChanged (ItemEvent evt) {
      if (evt.getSource() == fontsA) {
         font = (String)fontsA.getSelectedItem();
         InputStream is = null;
         String host = "http://gauss.ececs.uc.edu/Courses/c694/fonts/";
         try {
            is = (new URL(host+font+".ttf")).openStream();
         } catch (Exception e) {
            System.out.println("Cannot get connection: ["+host+font+"]");
            return;
         }
         try {
            fp.f = Font.createFont(Font.TRUETYPE_FONT, is);
         } catch (Exception e) {
            fp.f = new Font(font, 0, fp.size);
            System.out.println("Using local font");
         }
         fp.f = fp.f.deriveFont((float)(fp.size*1.0));
         setSize((620*fp.size)/19, 60+(750*fp.size)/19);
         setVisible(true);
         repaint();
      } else if (evt.getSource() == sizes) {
         fp.size = Integer.parseInt((String)sizes.getSelectedItem());
         String host = "http://gauss.ececs.uc.edu/Courses/c694/fonts/";
         InputStream is = null;
         try {
            is = (new URL(host+font+".ttf")).openStream();
         } catch (Exception e) {
            System.out.println("Cannot get connection");
            return;
         }
         try {
            fp.f = Font.createFont(Font.TRUETYPE_FONT, is);
         } catch (Exception e) {
            fp.f = new Font(font, 0, fp.size);
            System.out.println("Using local font");
         }
         fp.f = fp.f.deriveFont((float)(fp.size*1.0));
         setSize((620*fp.size)/19, 60+(750*fp.size)/19);
         setVisible(true);
         repaint();
      }
   }
   
   public void actionPerformed (ActionEvent evt) {
      if (evt.getSource() == button) {
         System.exit(0);
      }
   }
}

public class FontTestMenuApplet extends Applet implements ActionListener {
   JButton go;

   public void init () {
      setLayout(new BorderLayout());
      add("Center", go = new JButton("Applet"));
      go.addActionListener(this);
   }

   public void actionPerformed (ActionEvent evt) {
      FontTestMenuTitle fonttest = new  FontTestMenuTitle();
      fonttest.go();
   }
}