Java Program to Display Text in Different Fonts

This is a Java Program to Display Text in Different Fonts

Problem Description

We have to write a program in Java such that it displays text in 10 different fonts.

Expected Input and Output

For displaying text in different fonts, we can have the following set of input and output.

To View the Texts:

On every execution of the program,
it is expected that texts with different fonts are displayed.
Problem Solution

1. Get the list of all fonts available in the system using GraphicsEnvironment.
2. Create random fonts using the list generated.
3. Set the created font to the graphics object.
4. Draw the text using drawString method.

advertisement
advertisement
Program/Source Code

Here is source code of the Java Program to display text in different fonts. The program is successfully compiled and tested using javac compiler on Fedora 30. The program output is also shown below.

  1. /* Java Program to Display Text in Different Fonts */
  2. import java.applet.*;
  3. import java.awt.*;
  4. import java.awt.GraphicsEnvironment;
  5. import java.lang.Math;
  6. public class Text extends Applet
  7. {
  8.     String fonts[];
  9.     //Function to get the list of fonts available
  10.     public void init()
  11.     {
  12. 	setBackground(Color.white);
  13.  
  14. 	GraphicsEnvironment GE;
  15.         GE = GraphicsEnvironment.getLocalGraphicsEnvironment();
  16. 	fonts = GE.getAvailableFontFamilyNames();
  17.     }
  18.     //Function to draw the text
  19.     public void paint(Graphics g)
  20.     {
  21. 	int size = fonts.length;
  22. 	int pos;
  23. 	for(int i=1;i<=10;i++)
  24. 	{
  25. 	    pos = (int)(Math.random()*size);
  26. 	    Font my_font = new Font(fonts[pos],Font.PLAIN,20);
  27. 	    g.setFont(my_font);
  28. 	    g.drawString("Sample Text",100,(i*40));
  29. 	}
  30.     }
  31. }
  32. /*
  33. <applet code = Text.class width = 500 height = 600>
  34. </applet>
  35. */

To compile and execute the program type the following commands :

>>> javac Text.java
>>> appletviewer Text.java
Program Explanation

1. To get the local graphics environment, use the getLocalGraphicsEnvironment method.
2. To get the list of fonts, use getAvailableFontFamilyNames method.
3. Create a font using Font class with parameters font name, type of font and size of font.
4. Set the font to the graphics object using setFont.
5. Draw the string using drawString.

Runtime Test Cases

Here’s the run time test cases to display text in different fonts for different input cases.

advertisement

Test case 1 – To View Text in Different Fonts.
java-program-text-different-fonts-1

Test case 2 – To View Text in Different Fonts.
java-program-text-different-fonts-2

Sanfoundry Global Education & Learning Series – Java Programs.

advertisement

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.