Java AWT NumericShaper class

Description

Java AWT NumericShaper class


import java.awt.Container;
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.JFrame;
import javax.swing.JPanel;

class NumericShaperPanel extends JPanel {
   private TextLayout layout;
   public NumericShaperPanel() {
      String text = "0 1 2 3 4 5 6 7 8 9";
      HashMap map = new HashMap();
      Font font = new Font("Mongolian Baiti", Font.PLAIN, 32);
      map.put(TextAttribute.FONT, font);
      map.put(TextAttribute.NUMERIC_SHAPING, NumericShaper.getShaper(NumericShaper.Range.MONGOLIAN));
      FontRenderContext fontRenderContext = new FontRenderContext(null, false, false);
      layout = new TextLayout(text, map, fontRenderContext);
   }/*from   w  w w. j a v a 2s . c  o  m*/

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

public class Main extends JFrame {

   public Main() {
      Container container = this.getContentPane();
      container.add("Center", new NumericShaperPanel());

      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      this.setTitle("NumericShaper Example");
      this.setSize(250, 120);
      this.setVisible(true);
   }

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



PreviousNext

Related