I'm trying to come up with a Swing app that can display all available fonts and show me how they look :
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Demo_Fonts extends JPanel
{
static String[] font_type;
// int[] styles={Font.PLAIN,Font.ITALIC,Font.BOLD,Font.ITALIC+Font.BOLD};
int[] styles={Font.PLAIN};
String[] stylenames={"Plain","Italic","Bold","Bold & Italic"};
Demo_Fonts()
{
setPreferredSize(new Dimension(500,3000));
}
public void paint(Graphics g)
{
for (int f=0;f<font_type.length;f++)
{
for (int s=0;s<styles.length;s++)
{
Font font=new Font(font_type[f],styles[s],18);
g.setFont(font);
String name=font_type[f]+" "+stylenames[s];
// g.drawString(name,20,(f*4+s+1)*20);
g.drawString(name,20,(f+s+1)*20);
}
}
}
public static void main(String[] a)
{
font_type=GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
for (int i=0;i<font_type.length;i++) System.out.println(font_type[i]);
JFrame f=new JFrame();
f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } });
JScrollPane areaScrollPane=new JScrollPane(new Demo_Fonts());
areaScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
areaScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
// areaScrollPane.setMinimumSize(new java.awt.Dimension(600,500));
areaScrollPane.setPreferredSize(new Dimension(500,1400));
f.setContentPane(areaScrollPane);
f.setSize(600,1400);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
But when I scroll down, the content is all messed up, why, and how to fix ?