1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
   | package minijeu;
 
import java.awt.*;
import javax.swing.*;
 
public class MFontDisplayer {
 
	/**
         * Affiche une frame dans laquelle sont affichées toutes les polices du system.
         * @param args
         */
	public static void main(String[] args) {
 
		//Un petite JFrame
		JFrame frame = new JFrame ("FontDisplayer");
		Dimension screen = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
		frame.setSize(800, 600);
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setResizable(false);
		frame.setLayout(new FlowLayout());
		frame.setLocation((screen.width - frame.getSize().width)/2,(screen.height - frame.getSize().height)/2);
		FPanel panel = new FPanel();
		panel.setSize(frame.getContentPane().getSize());
		panel.setBackground(Color.WHITE);
		JScrollPane spane = new JScrollPane(panel);
		spane.setSize(frame.getContentPane().getSize());
		frame.add(spane);
		panel.setSize(panel.paint());
 
	}
 
	/**
         * Un panel qui affiche toutes les fonts du system. 
         * @author Radical Bob
         */
	static class FPanel extends JPanel {
 
		private static final long serialVersionUID = 1L;
		private Dimension size = new Dimension ();
 
		public Dimension paint() {
 
			this.repaint();
			return this.size;
 
		}
 
		public void paintComponent (Graphics g) {
 
			//L'environnement graphique.
			GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment();
			//La liste des fonts du system.
			String[ ] fonts = environment.getAvailableFontFamilyNames();
			//La ahteur a laquelle dessiner la prochaine font
			int lastHeigth = 14;
			//Ce qui sera afficher pour chaque font
			String alphabet = "abcdefghijklmnopqrstuvwxyz";
			//la font et son metrics
			Font font;
			FontMetrics metrics;
 
			for (int i = 0; i<fonts.length; i++) {
 
				font = new Font (fonts[i], Font.PLAIN, 14);
				metrics = getFontMetrics(font);
				g.setFont(font);
				g.drawString(fonts[i]+" : "+alphabet+alphabet.toUpperCase(), 10, lastHeigth+5);
				lastHeigth+=metrics.getHeight();
 
			}
 
			this.size.setSize((int)this.getSize().getWidth(), lastHeigth);
 
		}		
 
	}
 
} | 
Partager