2

I'm currently creating a domino game in java. The idea is to load the domino images, rotate them if necessary, and then display them on the screen. A few weeks ago I posted a question asking how to rotate an imageIcon in Java.

Someone replied as follows: "The Unicode charset includes dominoes".

So if I understand this correctly, instead of loading domino images I can use Unicode character set to display the dominoes on the screen? I cannot find any examples on Internet how to do this. Any one can give me an example of how this is done?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Farliesz
  • 43
  • 1
  • 3

3 Answers3

2

Dominoes are represented by UTF8 characters from U+1F030 to U+1F09F. There is a special block for them.

According to https://unicode-table.com:

Domino Tiles is a Unicode block containing characters for representing game situations in dominoes. The block includes symbols for the standard six dot tile set and backs in horizontal and vertical orientations

See https://unicode-table.com/en/blocks/domino-tiles/

According to The Unicode Standard, Version 10.0:

The domino tile symbols do not represent the domino pieces per se, but instead constitute graphical symbols for particular orientations of the dominoes, because orientation of the tiles is significant in discussion of dominoes play. Each visually distinct rotation of a domino tile is separately encoded. Thus, for example, both U+1F081 domino tile vertical-04-02 and U+1F04F
domino tile horizontal-04-02 are encoded, as well as U+1F075 domino tile vertical-02-04 and U+1F043 domino tile horizontal-02-04. All four of those symbols represent the same game tile, but each orientation of the tile is visually distinct and requires its own symbol for text. The digits in the character names for the domino tile symbols reflect the dot patterns on the tiles.

You also have to choose a font able to display them.

Ortomala Lokni
  • 56,620
  • 24
  • 188
  • 240
1

The previous answer is correct. However:

More convenient page for characters: http://www.alanwood.net/unicode/domino-tiles.html

Plus, the actual printing of those characters is described here.

An example of printing one of the dominoes:

class Scratch {
    public static void main(String[] args) {
        System.out.println(new String(Character.toChars(127026)));
    }
}

This prints on IntelliJ's console. Your results may vary, depending on your font.

0

The following code shows how to ascertain the installed fonts that support displaying this group of Unicode characters (3 out of over 250 fonts installed here), and displaying them in a text area.

enter image description here

import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.EmptyBorder;
import java.util.Vector;

public class DominoTiles {

    private JComponent ui = null;
    public static final int DOMINO_TILE_START = 127024;
    public static final int DOMINO_TILE_END = 127123;
    String dominoeTiles;
    JTextArea textArea = new JTextArea(4, 10);

    DominoTiles() {
        initUI();
    }

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

        ui = new JPanel(new BorderLayout(4, 4));
        ui.setBorder(new EmptyBorder(4, 4, 4, 4));

        StringBuilder sb = new StringBuilder();
        for (int ii = DOMINO_TILE_START; ii <= DOMINO_TILE_END; ii++) {
            String s = new String(Character.toChars(ii));
            sb.append(s);
        }
        textArea.setText(sb.toString());
        textArea.setLineWrap(true);
        ui.add(new JScrollPane(textArea));

        String[] fontFamilies = GraphicsEnvironment.
                getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
        final Vector<String> compatibleFonts = new Vector<>();
        for (String fontFamily : fontFamilies) {
            Font f = new Font(fontFamily, Font.PLAIN, 1);
            if (f.canDisplayUpTo(sb.toString()) < 0) {
                compatibleFonts.add(fontFamily);
            }
        }
        final JList list = new JList(compatibleFonts);
        ui.add(new JScrollPane(list), BorderLayout.LINE_START);
        ListSelectionListener listSelectionListener = new ListSelectionListener() {

            @Override
            public void valueChanged(ListSelectionEvent e) {
                if (!e.getValueIsAdjusting()) {
                    String fontFamily = list.getSelectedValue().toString();
                    Font f = new Font(fontFamily, Font.PLAIN, 60);
                    textArea.setFont(f);
                }
            }
        };
        list.addListSelectionListener(listSelectionListener);
        list.setSelectedIndex(0);
        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    }

    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) {
                }
                DominoTiles o = new DominoTiles();

                JFrame f = new JFrame(o.getClass().getSimpleName());
                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);
    }
}

This example shows how to turn Unicode characters into images (plus a few more tricks of rendering them).

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433