Using the NumericShaper.Range enumeration to support the display of digits : Swing « JDK 7 « Java

Using the NumericShaper.Range enumeration to support the display of digits

import java.awt.Container;
import javax.swing.JFrame;

import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.font.FontRenderContext;
import java.awt.font.NumericShaper;
import java.awt.font.TextAttribute;
import java.awt.font.TextLayout;
import java.util.HashMap;
import javax.swing.JPanel;

public class Test extends JFrame {
  public Test() {
    Container container = this.getContentPane();
    container.add("Center", new NumericShaperPanel());
    this.setSize(250, 120);

  public static void main(String[] args) {
    new Test();

class NumericShaperPanel extends JPanel {
  private TextLayout layout;

  public NumericShaperPanel() {
    String text = "java";
    HashMap map = new HashMap();
    Font font = new Font("Mongolian Baiti", Font.PLAIN, 32);
    map.put(TextAttribute.FONT, font);
    FontRenderContext fontRenderContext = new FontRenderContext(null, false,
    layout = new TextLayout(text, map, fontRenderContext);

  public void paintComponent(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    layout.draw(g2d, 10, 50);


Related examples in the same category

1.Creating a varying gradient translucent window
2.Handling multiple file selection in the FileDialog class
3.Managing the Opacity of a Window
4.Managing the Shape of a Window
5.Managing Window types
6.Mixing heavyweight and lightweight components
7.New border types in Java 7:RaisedSoftBevelBorder
8.New border types in Java 7:LineBorder width
9.New border types in Java 7:LoweredSoftBevelBorder
11.BorderFactory.createSoftBevelBorder(BevelBorder.LOWERED, Color.lightGray, Color.yellow)
12.Using the new JLayer Decorator for a password field
13.Managing extra mouse buttons and high resolution mouse wheels