java--Customizing external font files

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.