20-CS-694 Advanced Programming Techniques Spring 2012
Canvas

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

     Previous      Next      Applet Source      Try this!      All lectures

Fonts on a Canvas

import javax.swing.*;
import java.awt.*;
import java.applet.*;

public class FontsAppletFrame extends JFrame {
   public void paint(Graphics g) {
      g.setColor(Color.black);
      g.fillRect(0,0,this.getSize().width, this.getSize().height);

      Font tr = new Font("TimesRoman", Font.PLAIN, 18);
      Font trb = new Font("TimesRoman", Font.BOLD, 18);
      Font tri = new Font("TimesRoman", Font.ITALIC, 18);
      Font trbi = new Font("TimesRoman", Font.BOLD+Font.ITALIC, 18);
      Font h = new Font("Helvetica", Font.PLAIN, 18);
      Font c = new Font("Courier", Font.PLAIN, 18);
      Font d = new Font("Dialog", Font.PLAIN, 18);      
      Font z = new Font("ZapfDingbats", Font.PLAIN, 18);            

      g.setColor(Color.white);
      g.setFont(tr);
      g.drawString("Hello World (times roman plain)",10,75);
      g.setFont(trb);
      g.drawString("Hello World (times roman bold)",10,100);
      g.setFont(tri);
      g.drawString("Hello World (times roman italic)",10,125);
      g.setFont(trbi);
      g.drawString("Hello World (times roman bold & italic)",10,150);
      g.setFont(h);
      g.drawString("Hello World (helvetica)",10,175);
      g.setFont(c);
      g.drawString("Hello World (courier)",10,200);
      g.setFont(d);
      g.drawString("Hello World (dialog)",10,225);
      g.setFont(z);
      g.drawString("Hello World (zapf dingbats)",10,250);
   }
}

public class FontsApplet extends Applet {
   public void init () {
      FontsAppletFrame ff = new FontsAppletFrame();
      ff.setSize(450,450);
      ff.setVisible(true);
   }
}