Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Painting Japanese characters using Arial fonts with method drawString(..) (Graphics2D)

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:

enter image description here

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

enter image description here

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

enter image description here

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

like image 565
Ernestas Gruodis Avatar asked Mar 04 '15 11:03

Ernestas Gruodis


1 Answers

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

enter image description here

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);
    }
}
like image 95
Andrew Thompson Avatar answered Oct 09 '22 00:10

Andrew Thompson