使用方法 drawString(..) 使用 Arial 字体绘制日文字符 (Graphics2D)
Painting Japanese characters using Arial fonts with method drawString(..) (Graphics2D)
一个String
可以这样画:
@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();
}
}
问题是如果我这样做 g2d.setFont(new Font("Arial", Font.PLAIN, 12));
- 日语字符不可见,只有正方形:
如果我将字体设置为 g2d.setFont(new Font("Serif", Font.PLAIN, 12));
- 一切正常:
例如,在 MS 写字板中,如果选择 Arial 字体,则字符可见:
但是我想用Arial
字体。也许我必须检测日文字符并切换到不同的字体,然后再返回?
它使用逻辑字体工作。 SANS_SERIF
将在 Windows 上(主要)使用 Arial。
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);
}
}
一个String
可以这样画:
@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();
}
}
问题是如果我这样做 g2d.setFont(new Font("Arial", Font.PLAIN, 12));
- 日语字符不可见,只有正方形:
如果我将字体设置为 g2d.setFont(new Font("Serif", Font.PLAIN, 12));
- 一切正常:
例如,在 MS 写字板中,如果选择 Arial 字体,则字符可见:
但是我想用Arial
字体。也许我必须检测日文字符并切换到不同的字体,然后再返回?
它使用逻辑字体工作。 SANS_SERIF
将在 Windows 上(主要)使用 Arial。
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);
}
}