Get Available Font Objects Example
- /*
- Get Available Font Objects Example
- This java example shows how to get available font objects using
- getAllFonts method of GraphicsEnvironment class.
- */
- import java.applet.Applet;
- import java.awt.Font;
- import java.awt.Graphics;
- import java.awt.GraphicsEnvironment;
- /*
- <applet code="GetAllFontObjectsExample" width=200 height=200>
- </applet>
- */
- public class GetAllFontObjectsExample extends Applet{
- public void paint(Graphics g){
- /*
- * To get an object of GraphicsEnvironment, use
- * static GraphicsEnvironment getLocalGraphicsEnvironment()
- * method of GraphicsEnvironment class.
- *
- * This is a static method.
- */
- GraphicsEnvironment graphicsEnvironment =
- GraphicsEnvironment.getLocalGraphicsEnvironment();
- /*
- * To get available font objects use,
- * Font[] getAllFonts()
- * method of GraphicsEnvironment class.
- *
- * This method returns an array of Font objects.
- *
- */
- Font fonts[] = graphicsEnvironment.getAllFonts();
- int y = 20;
- for(int i=0; i < fonts.length; i++){
- //print font names
- g.drawString(fonts[i].getName(), 10, y);
- y += 20;
- }
- }
- }
Example Output




