This Example shows how to use the 3rd party font (.ttf) in our java application. This sometime need when we want to use strictly to some of the other languages like Gujarati, etc. We just need to have a .ttf(True Type Font) file which is need to be loaded by programming in our application.
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.JOptionPane;
import java.io.IOException;
import java.io.InputStream;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.awt.Font;
import java.awt.FontFormatException;
class GujaratiType extends JFrame
{
public Font font,fontUsed;
public GujaratiType()
{
try
{
InputStream myStream = new BufferedInputStream(new FileInputStream("LMG_LAX1.ttf"));
font = Font.createFont(Font.TRUETYPE_FONT, myStream);
fontUsed = font.deriveFont(Font.PLAIN,20);
}
catch(FontFormatException ex)
{
JOptionPane.showMessageDialog(GujaratiType.this,ex,"Error",JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
catch(IOException ex)
{
JOptionPane.showMessageDialog(GujaratiType.this,"System can not find the file : LMG_LAX1.TTF","Error",JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
JTextArea txt = new JTextArea();
txt.setFont(fontUsed);
JScrollPane jsp = new JScrollPane(txt);
add(jsp);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(0,0,300,300);
//setSize(300,300);
setVisible(true);
}
public static void main(String[] a)
{
new GujaratiType();
}
}
ReplyDeletenice blog for beginners.thank you.
web programming tutorial
welookups
Thanks for sharing your opinion.
Delete