Graphis2d drawString to generate german umlauts
I am trying 开发者_运维问答to use Graphics2d to generate german unlauts and http://en.wikipedia.org/wiki/%C3%9F.
The output that I always see is two question marks. Any ideas about how to solve this?
Below is some code to print out the characters you want. My guess is that the font you are using may not have those characters if you are getting question marks. The font being reported when I run the example is LucidaGrande.
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
public class DrawStringUmlaut extends JPanel {
public DrawStringUmlaut() {
setPreferredSize(new Dimension(getPreferredSize().width, 200));
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawString("\u00f6", 10, 20);
g.drawString("\u00df", 40, 20);
g.drawString(g.getFont().getFontName(), 10, 40);
g.drawString(Integer.toString(g.getFont().getSize()) + " pt", 10, 60);
}
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new DrawStringUmlaut(), BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
});
}
}
If your system uses a font which can display these characters (ü
and ß
) , it should work out of the box.
Try the following example:
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Graphics2dUmlaut extends Frame {
public void paint(Graphics g) {
Graphics2D g1 = (Graphics2D) g;
g1.drawString("\u00fc\u00df", 100, 100);
}
public static void main(String args[]) {
Frame frame = new Graphics2dUmlaut();
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
frame.setSize(200, 200);
frame.setVisible(true);
}
}
精彩评论