开发者

Unicode symbols (arrows) in Java

开发者 https://www.devze.com 2022-12-18 15:02 出处:网络
i want to use following symbols for buttons in my app: arrows http://img402.imageshack.us/img402/3176/arrowso.jpg

i want to use following symbols for buttons in my app:

arrows http://img402.imageshack.us/img402/3176/arrowso.jpg

here my code:

Button goToFirstButton = new Button("\uE318");
Button prevPageButton = new Button("\uE312");
Button nextPageButton = new Button("\uE313");
Button goToLastButton = new Button("\uE319");

and the result is

result http://img693.imageshack.us/img693/9063/resultbu.jpg

It seems, that \uE318 and \uE313 are wrong. What should i use instead? For goToLastButton and goToFirstButton i prefer to use this images

alt text htt开发者_开发知识库p://img3.imageshack.us/img3/5724/singlearrow.jpg

but i can't find, which code should i use.


I would suggest to use Icons on Buttons instead of special characters, because the ability to display may be strongly affected by availability of fonts on client workstation.


The unicode codepoints you want to use are part of a private use area, i.e. every font manufacturer is free to put whatever character they like at whatever position. The font you used to look up the arrow characters is simply a different font than the one used for displaying the button text. If the button text maps \uE318 and \uE313 to some Chinese (?) graphem, then that's not wrong, just different.


Although multiple people have made the argument you should avoid using these codepoints because you can't rely on the users' systems having a font which displays these characters, the reason you're getting the wrong characters in your example case has been entirely missed. All of the symbols you are trying to draw are in the "private use area" which means that the symbols involved will potentially be different in every single font.

The Unicode standard states:

Private Use Area (E000-F8FF) * The Private Use Area does not contain any character assignments, consequently no character code charts or namelists are provided for this area.

If you embed the particular font you want to use to insure you can use these codepoints, that's fine. But that does mean you should, indeed, use the \u#### notation in your code, because embedding the characters as Unicode directly means the source won't make sense unless somebody views it in the correct font.

All in all, it's probably better to use icons unless you already have a symbol font you think is simply far superior to any graphical work you would otherwise do.


◀ ▶

There are a couple of symbols close to what you want in the Geometric Shapes chart. However, as others have said, use icons and stay out of the private use area.


Java source files are UTF-8 encoded, so you can put the symbols you want directly in the source code (just copy 'n paste from a font viewer or web), as long as you use a decent editor. No need to use this confusing "\uXXXX" notation. For instance, I've found this useful for Greek letters commonly used in scientific notation (δ, ρ, ψ...) - you can even use them as variable names.

Of course, your font of choice must have the symbols you want, otherwise it won't work.


I would suggest that if you are worried about the client font's ability to display a particular character, use the following:

private static final HashMap<Character, Font> fontCache = new HashMap<Character, Font>();
public static Font getFontThatCanDisplay(char c, int style, float size) {
    Font f = fontCache.get(c);
    if (f == null) {
        f = UIManager.getFont("Label.font");
        for (Font font: GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts()) {
                if (font.canDisplay(c)) {
                    f = font;
                    break;
                }
            }
            fontCache.put(c, f);
        }
        return f.deriveFont(style, size);
    }


I also suggest using icons. Not all fonts have symbols for all unicode character points.

You may get it working by specifying a particular font. But what if that font is found on Windows 7 computers, but not on Mac OS X or Linux or Windows XP? Then the system will choose another font, based on the browser defaults, and that default might not have the symbols you want.


I will defend using Unicode glyphs on buttons, but it's really easy to implement the Icon interface and use paintIcon() to draw anything you want. The tutorial "Creating a Custom Icon Implementation" is a good example; this code shows a more complex, animated histogram.

0

精彩评论

暂无评论...
验证码 换一张
取 消