How do I to display "sigma-hat" using Unicode or any alternative in Java? (I have a partial solution)

StackOverflow https://stackoverflow.com/questions/23163321

Pergunta

I'm trying to produce a sigma-hat symbol (for sample standard deviation).

On a Windows 7 system, the following code produces a JLabel with a misaligned sigma hat:

JLabel sigmaHat = new JLabel("\u03C3\u0302");

And it looks like this:

http://i.imgur.com/z4Nowwm.jpg

Am I using the wrong combining character or is the Unicode for sigma-hat broken? Also, is it possible to produce a symbol for sample variance (sigma-hat-squared)?

Foi útil?

Solução

Using U+0302 COMBINING CIRCUMFLEX ACCENT after the sigma character is the correct way. In Unicode, a combining mark appears after the base character in the data stream. And there is no other combining mark that could be conceivably used instead.

However, the result depends on the font(s) and on the rendering engine. Failures are common. Testing here: σ̂. (Does not look good.) Trying different fonts, when possible, may help. But in general, notations like this are usuall written using equation editors, LaTeX, or other tools that operate above the plain text level.

Outras dicas

Out of curiosity I wrote up a test:

import javax.swing.*;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.EventQueue;

public class FontCheck{
    final static String string = "\u03C3\u0302";
    public static void main(String[] args){

    EventQueue.invokeLater(()->{
        JFrame frame = new JFrame("font check!");
        JPanel content = new JPanel();
        content.setLayout(new BoxLayout(content, BoxLayout.PAGE_AXIS));
        content.add(new JLabel(string));
        String[] fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
        for(String font: fonts){
            JLabel label = new JLabel(font + " : " + string);
            label.setFont(new Font(font, Font.PLAIN, 12));
            content.add(label);
        }
        frame.setContentPane(new JScrollPane(content));
        frame.setSize(400, 600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        });
    }
}

That shows all of the available fonts, and the first one is the default font. (Which works fine for me.) screenshot of jframe with labels

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top