FOLLOW ME Twitter Facebook Вконтакте LinkedIn RSS Feed
Browsing articles tagged with " 1.7.0_40"

Bug with stringWidth() in JDK 1.7.0_40

Sep 13, 2013
Category: Java

Today I found that JDK 1.7.0_40 has a bug in the strings width calculation for some fonts. The problem easily reproduces with Retina displays. I wrote a test to do this:

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

/**
 * @author Konstantin Bulenkov
 */
public class TextWidthTest extends JFrame {
  public TextWidthTest() throws HeadlessException {
    super("Text Width Test in JDK 1.7.0_40");
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(280, 40);
    final String str = "nnnnnnnnnnnnnnnnnnnn.txt";
    JComponent customComp = new JComponent() {
      @Override
      protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        FontMetrics fm = getFontMetrics(getFont());
        g.setColor(Color.orange);
        g.fillRect(0, 0, fm.stringWidth(str), fm.getHeight());
        g.setColor(Color.blue);
        g.drawString(str, 0, fm.getHeight());
      }
    };
    Font font = new Font("Lucida Grande", Font.PLAIN, 12);
    customComp.setFont(font);
    getContentPane().add(customComp);
    setVisible(true);
  }

  public static void main(String[] args) {
    new TextWidthTest();
  }
}

Oracle_JDK_Wrong_String_Width
I was playing with the test and found out that FontMetrics returns a wrong width for some combinations of symbols and fonts. I decided to have a break after this:

00000410


Font rendering: Apple JDK 6 vs Oracle JDK 1.7.0_40

Sep 12, 2013
Category: Java

The Oracle JDK version 1.7.0_40 contains significant changes in font rendering comparing to latest builds of Apple JDK 1.6

Apple JDK with Quartz
apple-jdk-font-rendering

Oracle JDK 1.7.0_40
oracle-jdk-font-rendering

First screenshot was made with Apple JDK. Quartz Graphics Framework enabled by passing apple.awt.graphics.UseQuartz=true as a system property to render text and graphics. Anyway, fonts and colours under Apple JDK with Quartz look so different in java applications comparing to native ones. The good news is that fonts look natively with new Oracle JDK. It uses new approaches to render texts. Unfortunately, OpenGL doesn’t support SubPixel anti-aliasing, so you can have some problems with non-Retina displays.

Also Apple JDK with Quartz modifies colours while rendering and makes them more soft and warm. Look at the difference in backgrounds on the pictures. They have the same background colour… sort of…