PaintAllFontsFromGraphicEvironment.java Source code

Java tutorial

Introduction

Here is the source code for PaintAllFontsFromGraphicEvironment.java

Source

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GraphicsEnvironment;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class PaintAllFontsFromGraphicEvironment extends JPanel {

    public static void main(String[] a) {
        JFrame f = new JFrame();
        f.setSize(400, 400);
        f.add(new PaintAllFontsFromGraphicEvironment());
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
    }

    public void paint(Graphics g) {
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();

        Font[] allFonts = ge.getAllFonts();

        for (int i = 0; i < allFonts.length; i++) {
            Font f = allFonts[i].deriveFont(10.0f);
            g.setFont(f);

            g.setColor(Color.black);
            g.drawString("Hello!", 10, 20 * i);

        }
    }
}