How to use Wingdings font in Java Swing - java

When I try to use the Wingdings font (or other symbol fonts), the text comes out as rectangles instead of the correct text. How do I get the correct characters to show?
import java.awt.*;
import javax.swing.*;
public class WingdingsFontDisplay extends JFrame
{
public static void main(String[] args)
{
new WingdingsFontDisplay();
}
public WingdingsFontDisplay()
{
this.setSize(500,150);
this.setTitle("Fun with Fonts");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//This shows that I do have "Wingdings" available
GraphicsEnvironment g;
g = GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] fonts = g.getAvailableFontFamilyNames();
for(String f : fonts)
{
System.out.println(f);
}
//Displaying text in the Wingdings font shows rectangles
JLabel wingdingsText = new JLabel("All work and no play makes Jack a dull boy");
Font f1 = new Font("Wingdings", Font.PLAIN, 14);
wingdingsText.setFont(f1);
this.add(wingdingsText, BorderLayout.NORTH);
//Displaying text in Arial works correctly
JLabel arialText = new JLabel("All work and no play makes Jack a dull boy");
Font f2 = new Font("Arial", Font.PLAIN, 14);
arialText.setFont(f2);
this.add(arialText, BorderLayout.SOUTH);
this.setVisible(true);
}
}

You need to use the appropriate Unicode range for the symbols you seek. In Java, symbols are not overlaid on the ASCII range, but have their own distinct character codes.
You can find a reference to the appropriate symbol codes at http://unicode.org/~asmus/web-wing-ding-ext.pdf . Most common symbols are in the 0x2200 and 0x2700 Unicode ranges.
Your Java install may include the SymbolTest sample applet, which makes it straightforward to preview the presentation of Unicode ranges with available fonts. Be warned, however, that better Java implementations will use font substitutions for symbols or characters not in the specified font, so you'll want to be sure you're actually getting the specified font.

Related

How could one draw a semibreve (whole note) in Java Swing?

I am creating a class to handle the creation of musical notation in my music training app, which is being built with Java Swing. As such, I am using the font Bravura for most of the symbols such as the treble clef and accidentals (using the Graphics drawString method with unicode characters).
However, I am unable to find a way to draw a semibreve, or a whole note, with this method; I get a rectangle instead of the desired character when I input the sequence "\uD834\uDD5D", which should correspond to a whole note, according to my research using fileformat.info.
My code for the JFrame is below:
public MusicalNotation() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JTextArea semibreveTextArea = new JTextArea();
semibreveTextArea.setBounds(50, 100, 200, 200);
semibreveTextArea.setFont(new Font("Bravura", Font.PLAIN, 24));
semibreveTextArea.setText("\uD834\uDD5D");
contentPane.add(semibreveTextArea);
semibreveTextArea.setBackground(getBackground());
semibreveTextArea.setEditable(false);
}
However, the resulting window looks like the below:
Are there any other fonts which have this functionality, or other ways to draw a semibreve?
Any help is much appreciated. Thank you in advance.
Are there any other fonts which have this functionality?. Yes there are (several) and and one of them is the font you are already using.
The Bravura music font already contains the Semibreve and you shouldn't need to draw anything since Bravura is a font. All you need to do is provide the appropriate Unicode value for the notes, lines, and spacing you want and of course, the "\uD834\uDD5D" is valid for displaying the Semibreve note providing the Text component you are displaying the characters in has the Bravura font set to it (not all fonts support all of these Unicode music characters), for example:
try {
// Load the "Bravura.otf" font file.
Font bravura = Font.createFont(Font.TRUETYPE_FONT, new File("Bravura.otf"));
// Font Size - NEEDED! I believe default is 1.
// Set it to what you want but you may find
// size 12 too small.
bravura = bravura.deriveFont(36f);
// Set the font to a JTextArea (or whatever).
jTextArea1.setFont(bravura);
// Display the Semibreve.
jTextArea1.setText("\uD834\uDD5D");
}
catch (FontFormatException | IOException ex) {
ex.printStackTrace();
}
You should see a Semibreve within the JTextAea.

How can I get Emoji to render in Java Swing widgets on Mac OS?

I have here a simple, complete, self-contained, pure-Java program that displays a frame containing a button with a Unicode emoji character as its text. This program runs correctly and displays the emoji on Windows, but not on Mac OS (Catalina, AdoptOpenJDK15).
Here is the source code; filename is EmojiTest.java:
import java.awt.*;
import javax.swing.*;
public class EmojiTest {
public static void main(String[] args) {
// listFonts(); // Uncomment if you want to see all registered fonts.
JFrame frame = new JFrame();
frame.setTitle("Emoji Demo: " + System.getProperty("os.name"));
frame.setSize(400, 300);
JButton button = new JButton();
String emoji = "\ud83d\udc36"; // dog face
button.setText(emoji);
button.setFont(getFont(emoji));
JPanel holder = new JPanel(); // Let button be natural size
holder.add(button);
frame.getContentPane().add(holder);
frame.setVisible(true);
}
static Font getFont(String emoji) {
String os = System.getProperty("os.name");
String fontFamily = os.equals("Mac OS X") ? "Apple Color Emoji" : "Segoe UI Emoji";
Font font = new Font(fontFamily, Font.PLAIN, 20);
System.out.println("Font: " + font);
System.out.println("can display result: " + font.canDisplayUpTo(emoji));
return font;
}
static void listFonts() {
String fonts[] = GraphicsEnvironment.getLocalGraphicsEnvironment()
.getAvailableFontFamilyNames();
for (int i = 0; i < fonts.length; i++) {
System.out.println(fonts[i]);
}
}
}
A few things to observe here:
The getFont() helper method prints out whether the font thinks it can display the text. On MacOS, it always prints -1, meaning it thinks it can display the text.
All 279 fonts on my Apple system claim (via canDisplayUpTo) that they can display it, but they fail to display it. Yes, I tested all of them visually, and no, none worked.
As you can see from the picture, this program works correctly on Windows. So the code is "correct", in the sense that it should work on Macs.
I'm fairly convinced that this is a JDK bug. However, if you debug the EmojiTest program in IntelliJ, it actually shows you in the debug pane that the text is in fact the correct emoji:
So IntelliJ, a Java program running on Mac OS Catalina, is able to render the emoji correctly in its font—admittedly, not in a JButton. But I have also tried rendering emoji in JTextPanes and other Swing widgets on MacOS, to no avail.
This question has been asked several times over the past few years, often indirectly, but the answer is always "You need to make sure your font can display the emoji".
I think this program pretty clearly demonstrates that the font being able to display the emoji is not the issue. The Apple Color Emoji font can absolutely display emoji, including this one. It just doesn't seem to be able to do it in Java.
Here's my question: How can I modify this EmojiTest.java program to show the emoji on MacOS?

How to print the Indian Rupee Symbol in java

I tried to display rupee symbol by using below code. But in output I'm getting a box symbol instead of rupee:
Code:
public class Applet extends JApplet {
public static final String RUPEE = "\u20B9";
public void init () {
setLayout(new FlowLayout());
JLabel b = new JLabel("rupee : " + RUPEE +123);
b.setFont(new Font("Arial", Font.PLAIN, 14));
add(b);
}
}
This box symbol is usually a sign that your font doesn't support this character. There's nothing you can do short of using a font that supports it instead of the current one.
Newer versions of Arial support this, so maybe your system needs an update.

how to print a glyph of supplementary characters in java onto my JTextField when i just click the button

I have a simple program just need to set the character whose Unicode value larger the character data type (supplementary character) on JTextField when the button is click .Tell me i am really fed up and how i will do it .This problem have already taken my 4 days.
//importing the packages
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.awt.*;
//My own custom class
public class UnicodeTest implements ActionListener
{
JFrame jf;
JLabel jl;
JTextField jtf;
JButton jb;
UnicodeTest()
{
jf=new JFrame();// making a frame
jf.setLayout(null); //seting the layout null of this frame container
jl=new JLabel("enter text"); //making the label
jtf=new JTextField();// making a textfied onto which a character will be shown
jb=new JButton("enter");
//setting the bounds
jl.setBounds(50,50,100,50);
jtf.setBounds(50,120,400,100);
jb.setBounds(50, 230, 100, 100);
jf.add(jl);jf.add(jtf);jf.add(jb);
jf.setSize(400,400);
jf.setVisible(true); //making frame visible
jb.addActionListener(this); // registering the listener object
}
public void actionPerformed(ActionEvent e) // event generated on the button click
{ try{
int x=66560; //to print the character of this code point
jtf.setText(""+(char)x);// i have to set the textfiled with a code point character which is supplementary in this case
}
catch(Exception ee)// caughting the exception if arrived
{ ee.printStackTrace(); // it will trace the stack frame where exception arrive
}
}
// making the main method the starting point of our program
public static void main(String[] args)
{
//creating and showing this application's GUI.
new UnicodeTest();
}
}
Since you are not giving enough information on what's wrong, I can only guess that either or both:
You are not using a font that can display the character.
You are not giving the text field the correct string representation of the text.
Setting a font that can display the character
Not all fonts can display all characters. You have to find one (or more) that can and set the Swing component to use that font. The fonts available to you are system dependent, so what works for you might not work for others. You can bundle fonts when you deploy your application to ensure it works for everyone.
To find a font on your system that can display your character, I used
Font[] fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
for (Font f : fonts) {
if (f.canDisplay(66560)) {
System.out.println(f);
textField.setFont(f.deriveFont(20f));
}
}
The output (for me) is a single font, so I allowed myself to set it in the loop:
java.awt.Font[family=Segoe UI Symbol,name=Segoe UI Symbol,style=plain,size=1]
as also noted in the comments to the question by Andrew Thompson.
Giving the text field the correct string representation
The text fields require UTF-16. Supplementary characters in UTF-16 are encoded in two code units (2 of these: \u12CD). Assuming you start from a codepoint, you can convert it to characters and then make a string from them:
int x = 66560;
char[] chars = Character.toChars(x); // chars [0] is \uD801 and chars[1] is \uDC00
textField.setText(new String(chars)); // The string is "\uD801\uDC00"
// or just
textField.setText(new String(Character.toChars(x)));
as notes by Andrew Thompson in the comments to this answer (previously I used a StringBuilder).

Java - Change font in a JTextPane containing HTML

I have a JTextPane and I have some text within that JTextPane. However, because I have been using HTML within the Pane, the text seems to have been automatically changed to Times New Roman.
I'm trying to set the font type within the JTextPane to the default font of the GUI (the font of the JTextPane when it's not HTML). However I can't just set the font to one font because it differs from operating system, therefore I want to find a way to get the default font and then change the text I have to the default font.
To demonstrate how the text is swapped to Times New Roman when converted, the following code is the format I have used. How could I change it to achieve my goal?
import javax.swing.JFrame;
import javax.swing.JTextPane;
public class GUIExample {
public static void main(String[] args) {
JFrame frame = new JFrame("My App");
frame.setSize(300,300);
JTextPane pane = new JTextPane();
pane.setContentType("text/html");
pane.setText("<html><b>This is some text!</b></html>");
frame.add(pane);
frame.setVisible(true);
}
}
Thanks!
The following will do the trick:
pane.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, true);
(Note that JTextPane extends JEditorPane.)
Update (Aug 2016):
For the setting to survive Look & Feel and system changes (e.g. Fonts changed in the Windows Control Panel) the line can be placed here:
#Override
public void updateUI() {
super.updateUI();
putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, true);
}
(This is also called during construction.)
Simplest way is probably something like this:
string fontfamily = pane.getFont().getFamily();
That will give you the default font. Then just apply it using CSS:
pane.setText("<html><body style=\"font-family: " + fontfamily + "\"<b>This is some text!</b></html>");
The JComponent html renderer uses its own font, and not that of the JComponent. In order to get the component to render the same, you need to set the font attributes in the html string. Among other things, you will need to set the font family, size, bold/italics, etc.
As an example, you could do the following:
JTextPane pane = new JTextPane();
Font font = pane.getFont();
pane.setContentType("text/html");
pane.setText("<html><font face=\"" + font.getFamily() + "\" size=\"" + font.getSize() + "\"></font>This is some text!</html>");
It would be fairly trivial to create a function that does this for you. Pass it a JComponent and a string, and it would create the html text for you, including all the font tags.

Categories

Resources