0

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 ?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Frank
  • 30,590
  • 58
  • 161
  • 244
  • 1
    JPanel + paintComponent, paintComponent + super.paintComponent as 1st code line, override getPreferredSize insted of setPreferredSize, f.pack() by replacing f.setSize(600,1400); – mKorbel May 25 '15 at 15:56
  • 1
    Maybe better than painting, go with something like [this](http://stackoverflow.com/a/6965149/2587435) – Paul Samsotha May 25 '15 at 15:57
  • *"display all available fonts and show me how they look "* Tip: Use a list or [combo. to display them](http://stackoverflow.com/a/6965149/418556)! – Andrew Thompson May 25 '15 at 23:42

1 Answers1

3

Probably Your first line of paint should probably be super.paint(g). The parent paint() method will clear the panel.

When you scroll down, you paint on the component again. If you do not clear what you've already drawn, you'll just keep painting ontop of other stuff, leading to a mess.

Edit: I added this line and it works on my computer now.

Edit: As mKorbel suggests, you should really be overriding paintComponent instead of paint. paint is more for AWT, but it "works" for your program. If you work with Java Swing, you generally override paintComponent. So really, you should delete you paint method and replace it with

  @Override
  public void paintComponent(Graphics g)
  {
      super.paintComponent( 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);
      }
    }
  }
user2570465
  • 2,437
  • 2
  • 18
  • 22