Font Viewer
Download
.jar filesource code
Java Code
Main.java
/**
* Main.java
* Created by Stijn Strickx on May 7, 2008
*/
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
public class Main {
private static JTree tree;
public static void main(String[] args) {
JFrame frame = new JFrame();
JLabel fontLabel = new JLabel();
fontLabel.setHorizontalAlignment(JLabel.CENTER);
tree = new JTree(new FontTreeModel());
tree.setCellRenderer(new FontCellRenderer());
tree.addTreeSelectionListener(new FontTreeListener(fontLabel));
JScrollPane scrollPane = new JScrollPane(tree);
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(600,300));
panel.setLayout(new BorderLayout());
panel.add(fontLabel, BorderLayout.CENTER);
Container cp = frame.getContentPane();
cp.setLayout(new BorderLayout());
cp.add(scrollPane,BorderLayout.WEST);
cp.add(panel,BorderLayout.EAST);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
FontTreeModel.java
/**
* FontTreeModel.java
* Created by Stijn Strickx on May 7, 2008
*/
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import javax.swing.event.TreeModelListener;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
public class FontTreeModel implements TreeModel{
private Font[] fonts;
private int[] styles;
public FontTreeModel(){
fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
styles = new int[4];
styles[0] = Font.PLAIN;
styles[1] = Font.BOLD;
styles[2] = Font.ITALIC;
styles[3] = Font.BOLD+Font.ITALIC;
}
public Object getRoot() {
return GraphicsEnvironment.getLocalGraphicsEnvironment();
}
public Object getChild(Object o, int i) {
if(o instanceof GraphicsEnvironment){
return fonts[i];
}
else{
return new StyledFont(((Font)o).getName(),styles[i],10);
}
}
public int getChildCount(Object o) {
if(o instanceof GraphicsEnvironment){
return fonts.length;
}
else{
return styles.length;
}
}
public boolean isLeaf(Object o) {
return !(o instanceof GraphicsEnvironment || (!(o instanceof StyledFont) && o instanceof Font));
}
public void valueForPathChanged(TreePath t, Object o) {
//
}
public int getIndexOfChild(Object o1, Object o2) {
int index = 0;
if(o1 instanceof GraphicsEnvironment){
for(int i=0;i
FontTreeListener.java
/**
* FontTreeListener.java
* Created by Stijn Strickx on May 7, 2008
*/
import java.awt.Font;
import javax.swing.JLabel;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.TreePath;
class FontTreeListener implements TreeSelectionListener{
private JLabel fontLabel;
public FontTreeListener(JLabel fontLabel) {
this.fontLabel = fontLabel;
}
public void valueChanged(TreeSelectionEvent e) {
TreePath path = ((JTree)e.getSource()).getSelectionPath();
if(!(path == null) && (path.getLastPathComponent() instanceof Font)){
Font value = (Font)path.getLastPathComponent();
fontLabel.setFont(value.deriveFont(25f));
fontLabel.setText(value.getFontName());
}
}
}
FontCellRenderer.java
/**
* FontCellRenderer.java
* Created by Stijn Strickx on May 7, 2008
*/
import java.awt.Component;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import javax.swing.JTree;
import javax.swing.tree.DefaultTreeCellRenderer;
public class FontCellRenderer extends DefaultTreeCellRenderer{
@Override
public Component getTreeCellRendererComponent(JTree tree, Object o, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
super.getTreeCellRendererComponent(tree,o,selected,expanded,leaf,row,hasFocus);
if(o instanceof StyledFont){
setText(((StyledFont)o).toString());
}
else if(o instanceof Font){
setIcon(new FontIcon((Font)o));
setText(((Font)o).getFontName());
}
else if(o instanceof GraphicsEnvironment){
setText("Fonts");
}
return this;
}
}
StyledFont.java
/**
* StyledFont.java
* Created by Stijn Strickx on May 7, 2008
*/
import java.awt.Font;
import java.util.HashMap;
public class StyledFont extends Font{
private HashMap styles;
public StyledFont(String name, int style, int size){
super(name,style,size);
this.style = style;
styles = new HashMap();
styles.put(Font.PLAIN, "Plain");
styles.put(Font.BOLD, "Bold");
styles.put(Font.ITALIC, "Italic");
styles.put(Font.BOLD+Font.ITALIC, "Bold and Italic");
}
@Override
public String toString(){
return styles.get(style);
}
}
FontIcon.java
/**
* FontIcon.java
* Created by Stijn Strickx on May 7, 2008
*/
import java.awt.Component;
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.Icon;
class FontIcon implements Icon{
private Font font;
private int size;
public FontIcon(Font font){
this.font = font;
size = 25;
}
public void paintIcon(Component c, Graphics g, int x, int y) {
Font defaultFont = g.getFont();
g.setFont(font.deriveFont(15f));
g.drawString("A", x, y+(size)*3/4);
g.setFont(defaultFont);
}
public int getIconWidth() {
return size;
}
public int getIconHeight() {
return size;
}
}
