Font Faces and Styles Not Set

M

Mike Westerfield

I'd like to see if anyone can offer an explanation for some font
issues before I report the issues as bugs to Sun and Apple. Please let
me know if you can explain the following as correct behavior.

It appears to me that a lot of fonts simply don't work under Java 1.4.
The simple application below should show fonts with a normal face,
bold, italic and underline. Underline seems to work on all systems,
but on both Windows and OS/X, bold and italic fail on many fonts. The
same fonts seem to have bold and italic faces in non-Java
applications, so it appears to be a problem with the JVM. For example,
STENCIL on the Mac and Century on Windows both have distinct bold and
italic faces in Word, but not in Java.

On OS/X, many fonts do not display the proper face; they seem to
default to serif or sans serif. Most of the Lucida fonts show this
problem.

Mike Westerfield

--- main.java ---

package fonttest;

public class Main {

public Main() {
Window window = new Window();
window.show();
}
public static void main(String[] args) {
Main main1 = new Main();
}
}

--- window.java ---

package fonttest;

import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;

public class Window extends JFrame {
private JScrollPane jScrollPane1 = new JScrollPane();
private JTextPane jTextPane = new JTextPane();

public Window() {
try {
jbInit();
pack();
populate();
} catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
jScrollPane1.setPreferredSize(new Dimension(640, 480));
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setTitle("Font Test");
this.getContentPane().add(jScrollPane1, BorderLayout.CENTER);
jScrollPane1.getViewport().add(jTextPane, null);
}

private void populate () {
String defaultFont = "SanSerrif";
String[] fontNames =
GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
for (int f = 0; f < fontNames.length; ++f) {
jTextPane.getCaret().setDot(Integer.MAX_VALUE);
setFont(defaultFont, false, false, false);
jTextPane.replaceSelection(fontNames[f] + "\t");

jTextPane.getCaret().setDot(Integer.MAX_VALUE);
setFont(fontNames[f], false, false, false);
jTextPane.replaceSelection("Abcdef" + "\t");

jTextPane.getCaret().setDot(Integer.MAX_VALUE);
setFont(fontNames[f], true, false, false);
jTextPane.replaceSelection("Abcdef" + "\t");

jTextPane.getCaret().setDot(Integer.MAX_VALUE);
setFont(fontNames[f], false, true, false);
jTextPane.replaceSelection("Abcdef" + "\t");

jTextPane.getCaret().setDot(Integer.MAX_VALUE);
setFont(fontNames[f], false, false, true);
jTextPane.replaceSelection("Abcdef" + "\n");
}
}
private void setFont(String name, boolean bold, boolean italic,
boolean underline) {
SimpleAttributeSet font = new SimpleAttributeSet();
font.addAttribute(StyleConstants.FontFamily, name);
font.addAttribute(StyleConstants.FontSize, new Integer(16));
font.addAttribute(StyleConstants.Bold, new Boolean(bold));
font.addAttribute(StyleConstants.Italic, new Boolean(italic));
font.addAttribute(StyleConstants.Underline, new
Boolean(underline));
jTextPane.setCharacterAttributes(font, false);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top