使用Arial字体绘制日文字符drawString(..)(Graphics2D) [英] Painting Japanese characters using Arial fonts with method drawString(..) (Graphics2D)

查看:368
本文介绍了使用Arial字体绘制日文字符drawString(..)(Graphics2D)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

String 可以这样描绘:

  @Override 
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d =(Graphics2D)g.create();

尝试{

g2d.setColor(Color.BLACK);

g2d.setFont(new Font(Serif,Font.PLAIN,12)); //日文字符是可见的
//g2d.setFont(new Font(Arial, (Berryz Kobo [情色摇滚])(MV)(日语字幕)(日语字母不可见)(仅限正方形)

g2d.drawString(Berryz工房「ROCKエロティック」 ,10,45);

} finally {
g2d.dispose();




$ b $ p
$ b

问题是如果我做 g2d.setFont(new Font(Arial,Font.PLAIN,12)); - 日文字符不可见,只是正方形而已:


如果我设置字体像 g2d.setFont(new Font(Serif,Font.PLAIN,12)); - 一切工作正常:




<例如,在MS WordPad中,如果选择了Arial字体,则字符是可见的:


但我想用 Arial 字体。也许我必须检测日文字符并切换到不同的字体,然后再回来?

解决方案

它使用逻辑字体。 SANS_SERIF 在Windows上是Arial(主要)。

$ b

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

public class FontTestWithJapaneseCharacters {

private JComponent ui = null;

class PaintingSurface extends JPanel {

@Override
public Dimension getPreferredSize(){
return new Dimension(400,20);
}

@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d =(Graphics2D)g;
g2d.setColor(Color.BLACK);
g2d.setFont(new Font(Font.SANS_SERIF,Font.PLAIN,12));
g2d.drawString(Berryz工房「ROCKエロティック」
+(Berryz Kobo [色情摇滚])(MV),10,15)。



FontTestWithJapaneseCharacters(){
initUI();

$ b $ public void initUI(){
if(ui!= null){
return;
}

ui = new PaintingSurface();
}

public JComponent getUI(){
return ui;


public static void main(String [] args){
Runnable r = new Runnable(){
@Override
public void run ){
try {
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
} catch(Exception useDefault){
}
FontTestWithJapaneseCharacters o
= new FontTestWithJapaneseCharacters();

JFrame f = new JFrame(使用日文字符进行字体测试);
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationByPlatform(true);

f.setContentPane(o.getUI());
f.pack();
f.setMinimumSize(f.getSize());

f.setVisible(true);
}
};
SwingUtilities.invokeLater(r);
}
}


A String can be painted like that:

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g.create();

    try {

        g2d.setColor(Color.BLACK);

        g2d.setFont(new Font("Serif", Font.PLAIN, 12));//Japanese characters are visible
        //g2d.setFont(new Font("Arial", Font.PLAIN, 12));//Japanese characters are not visible (squares only)

        g2d.drawString("Berryz工房 『ROCKエロティック』(Berryz Kobo[Erotic ROCK]) (MV)", 10, 45);

    } finally {
        g2d.dispose();
    }
}

The problem is that if I do g2d.setFont(new Font("Arial", Font.PLAIN, 12)); - Japanese characters are not visible, just squares instead:

And if I set font like g2d.setFont(new Font("Serif", Font.PLAIN, 12)); - everything works fine:

For example, in MS WordPad the characters are visible if Arial font is selected:

But I want to use Arial font. Maybe I have to detect Japanese character and switch to different font, and then back again?

解决方案

It works using the logical fonts. SANS_SERIF will be Arial (primarily) on Windows.

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

public class FontTestWithJapaneseCharacters {

    private JComponent ui = null;

    class PaintingSurface extends JPanel {

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(400, 20);
        }

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g;
            g2d.setColor(Color.BLACK);
            g2d.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
            g2d.drawString("Berryz工房 『ROCKエロティック』"
                    + "(Berryz Kobo[Erotic ROCK]) (MV)", 10, 15);
        }
    }

    FontTestWithJapaneseCharacters() {
        initUI();
    }

    public void initUI() {
        if (ui != null) {
            return;
        }

        ui = new PaintingSurface();
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(
                            UIManager.getSystemLookAndFeelClassName());
                } catch (Exception useDefault) {
                }
                FontTestWithJapaneseCharacters o
                        = new FontTestWithJapaneseCharacters();

                JFrame f = new JFrame("Font test with Japanese characters");
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setLocationByPlatform(true);

                f.setContentPane(o.getUI());
                f.pack();
                f.setMinimumSize(f.getSize());

                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

这篇关于使用Arial字体绘制日文字符drawString(..)(Graphics2D)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆