Java tutoraial: Fonts in java


hey,

In this tutorial we will take a look at fonts in java.

I’m using getAvalableFontFamily method for this:

Output is given below:

Refer to following code:

import java.awt.*;
mport javax.swing.*;
public class bouncingname {

public static void main(String argv[])
{

createframe frame=new createframe();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

frame.setResizable(true);
}
}

class createframe extends JFrame{
createframe()
{
setTitle(“Bouncing Name”);
setSize(1400,900);
createcomp component=new createcomp();

add(component);
}
}
class createcomp extends JComponent
{
public void paintComponent(Graphics g)
{
String[] fontname=GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
Graphics2D g2=(Graphics2D ) g;
g2.setBackground(Color.green);
g2.setColor(Color.red);

int a=10,b=40;
for(int i=0;i<fontname.length;i++)
{
Font abc=new Font(fontname[i],Font.BOLD,20);
g2.setFont(abc);
g2.drawString(i+” “+fontname[i], a, b);
if(a<1200)
a+=300;
else
a=10;
if(a>1200)
if(b<800)
b+=40;
}

}}

Main

    public static void main(String argv[])
{

createframe frame=new createframe();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setResizable(true);
}

createframe is a class which is extends JFrame

that is given below

class createframe extends JFrame{
createframe()
{
setTitle(“FONTS”);
setSize(1400,900);
createcomp component=new createcomp();
add(component);
}
}

createcomp is also a class which paints components using paintComponent(Graphics g)  method

class createcomp extends JComponent
{
public void paintComponent(Graphics g)
{
String[] fontname=GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
Graphics2D g2=(Graphics2D ) g;
g2.setBackground(Color.green);
g2.setColor(Color.red);

int a=10,b=40;
for(int i=0;i<fontname.length;i++)
{
Font abc=new Font(fontname[i],Font.BOLD,20);
g2.setFont(abc);
g2.drawString(i+” “+fontname[i], a, b);
if(a<1200)
a+=300;
else
a=10;
if(a>1200)
if(b<800)
b+=40;
      }}}

1.  String[] fontname=GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();

stores all font names available in fontname string array.

2.   Font abc=new Font(fontname[i],Font.BOLD,20);

this creates object abc of Font and font type is stored in it by new Font(fontname[i],Font.BOLD,20);

first field           is for font name. I wanted to know the font names so i have given array of fontname so that we can understand names of font supported in java.

second field     you can use Font.ITALIC ,FONT.PLAIN in second field to choose pattern.

third field        is for size of font.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s