Issue displaying font with java swing

Hi,

So there is my problem : I can't understand why this little program :

package tests;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.GridLayout;
import java.text.AttributedCharacterIterator.Attribute;
import java.util.Map;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

final class FontTest implements Runnable {
    public static void main(String[]arguments_) {
        SwingUtilities.invokeLater(new FontTest());
    }
    private String newLine = System.getProperty("line.separator");
    @Override
    public void run() {
        JFrame frame_ = new JFrame("FontTest");
        JPanel panel_ = new JPanel(new GridLayout(1, 2));
        JTextArea area_1 = new JTextArea(), area_2 = new JTextArea();
        Font[]all_fonts = GraphicsEnvironment
                       .getLocalGraphicsEnvironment().getAllFonts();
        // picking 2 fonts with different predefined specificities
        // Arial Bold Italic and Bahnschrift Bold Condensed
        Font font_1 = all_fonts[6], font_2 = all_fonts[13];
        Map<? extends Attribute, ?> map_1 = font_1.getAttributes()
                                  , map_2 = font_2.getAttributes();
        setTextArea(area_1, new Font(map_1));
        setTextArea(area_2, new Font(map_2));
        panel_.setPreferredSize(new Dimension(1080, 240));
        panel_.add(area_1);
        panel_.add(area_2);
        frame_.add(panel_);
        frame_.pack();
        frame_.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame_.setLocationRelativeTo(null);
        frame_.setVisible(true);
    }
    private void setTextArea(JTextArea area_, Font font_) {
        area_.setFont(font_.deriveFont(14.f));
        area_.setBorder(BorderFactory.createTitledBorder(area_.getFont().getName()));
        area_.append(newLine);
        for(Object o : area_.getFont().getAttributes().entrySet()) {
            area_.append(o.toString());
            area_.append(newLine);
        }
        area_.append(newLine);
        area_.append(area_.getFont().toString());
    }
}

is showing this when executing : JFrame showing 2 Fonts and their name, attributes, and constructor, both irrelevant

  • Why TextAttribute are null for width, posture and weight (and all others except size and family) since font's name suggests some value ?

  • Why the constructors (font_.toString()) describe a plain style since they should be bold+italic or bold ?

  • Why the first Font (Arial) is actually bold and italic since its attributes AND constructor say it's not ?

  • Why the second Font (Bahnschrift) is neither bold nor condensed, since its attributes AND constructor are the same than the first Font ?

Please, thanks to help me understanding that...



Read more here: https://stackoverflow.com/questions/64950071/issue-displaying-font-with-java-swing

Content Attribution

This content was originally published by Oliv at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: