How to render text using FRACTIONALMETRICS in Java 7

I have a Java desktop application that I'm working on and use JDK 6 for development through the IDE. I use the rendering hint VALUE_FRACTIONALMETRICS_ONalong with other hints to better display text in JLabels. These tips work fine on JVM 1.6, but I recently noticed that it seems to completely ignore VALUE_FRACTIONALMETRICS_ONit when I run the application with JRE 1.7. Am I doing something wrong here that makes Java 7 ignore these hints?

lblDate = new ATimeLabel(ATimeLabel.DATE_LETTERS) {
    private static final long serialVersionUID = 1L;

    @Override
    protected void paintComponent(Graphics g) {

        Graphics2D g2d = (Graphics2D) g;

        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
                RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_RENDERING,
                RenderingHints.VALUE_RENDER_QUALITY);
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
                RenderingHints.VALUE_FRACTIONALMETRICS_ON);

        super.paintComponent(g2d);
    }
};

FRACTIONALMETRICS has the most noticeable visual effect for text, so getting this to work is what I'm looking for.

, , IDE (), Java 6, Jar ( Right), JRE, Java 7. ( , , )

.

+5
1

VALUE_FRACTIONALMETRICS_ON. . Swing FontRenderContext. .

, , . - .

import java.awt.*;
import java.awt.event.*;
import java.util.Map;
import javax.swing.JLabel;

public class TextHints {
    public static void main(String[] args) {
        Frame frame = new Frame();
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        JLabel label = new JLabel("Sunday, March 17") {
            @Override
            protected void paintComponent(Graphics g) {

                Graphics2D g2d = (Graphics2D) g;

                Map desktopHints = (Map) Toolkit.getDefaultToolkit().getDesktopProperty("awt.font.desktophints");
                if (desktopHints != null) {
                    g2d.addRenderingHints(desktopHints);
                    System.out.println("Desktop: " + desktopHints);
                }
                java.awt.FontMetrics fm = getFontMetrics(getFont());
                Object swingHint = fm.getFontRenderContext().getAntiAliasingHint();
                System.out.println("Swing AntiAliasingHint: " + swingHint);

                super.paintComponent(g2d);
            }
        };
        label.setBackground(Color.BLACK);
        label.setForeground(Color.LIGHT_GRAY);
        label.setOpaque(true);

        label.setFont(label.getFont().deriveFont(Font.BOLD, 14f));

        frame.add(label);
        frame.setSize(new Dimension(160, 100));
        frame.setVisible(true);
    }
}
+2

All Articles