Package chapter1;
Import javax.swing.*;
Import java.awt.*;
Import java.io.IOException;
Import Java.io.InputStream;
public class Fontdemo extends JPanel {
Private static final long serialversionuid = 1L;
Public Fontdemo () {
Super ();
}
public void Paintcomponent (Graphics g) {
Graphics2D g2d = (graphics2d) g.create ();
G2d.setrenderinghint (renderinghints.key_antialiasing,renderinghints.value_antialias_on);
G2d.setpaint (Color.Blue);
try {
G2d.setfont (Loadfont ());
} catch (IOException e) {
E.printstacktrace ();
} catch (Fontformatexception e) {
E.printstacktrace ();
}
G2d.drawstring ("Font Demo", 50,50);
G2d.dispose ();
}
Public Font Loadfont () throws IOException, Fontformatexception {
String fontfilename = "Amersn.ttf";
InputStream is = This.getclass (). getResourceAsStream (Fontfilename);
Font Actionjson = Font.createfont (Font.truetype_font,is);//Returns a font of the specified type and input data
Font actionjsonbase = Actionjson.derivefont (font.bold,16);//Create a new Font object by copying the Font object and applying the new style and size.
return actionjsonbase;
}
public static void Main (String args[]) {
JFrame UI = new JFrame ("Font Demo graphics2d");
Ui.setdefaultcloseoperation (Jframe.exit_on_close);
Ui.getcontentpane (). setlayout (New BorderLayout ());
Ui.getcontentpane (). Add (New Fontdemo (), borderlayout.center);
Ui.setpreferredsize (New Dimension (380,380));//Set the best size, change depending on the size of the interface
Ui.pack ();
Ui.setvisible (TRUE);
}
}
java--Customizing external font files