home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / JBuilder8.iso / Solaris / resource / jre / demo / applets / SymbolTest / SymbolTest.java < prev    next >
Encoding:
Java Source  |  2002-09-06  |  5.3 KB  |  169 lines

  1. /*
  2.  * Copyright (c) 2002 Sun Microsystems, Inc. All  Rights Reserved.
  3.  * 
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  * 
  8.  * -Redistributions of source code must retain the above copyright
  9.  *  notice, this list of conditions and the following disclaimer.
  10.  * 
  11.  * -Redistribution in binary form must reproduct the above copyright
  12.  *  notice, this list of conditions and the following disclaimer in
  13.  *  the documentation and/or other materials provided with the distribution.
  14.  * 
  15.  * Neither the name of Sun Microsystems, Inc. or the names of contributors
  16.  * may be used to endorse or promote products derived from this software
  17.  * without specific prior written permission.
  18.  * 
  19.  * This software is provided "AS IS," without a warranty of any kind. ALL
  20.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
  21.  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
  22.  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT
  23.  * BE LIABLE FOR ANY DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT
  24.  * OF OR RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THE SOFTWARE OR ITS
  25.  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
  26.  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
  27.  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
  28.  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN
  29.  * IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  30.  * 
  31.  * You acknowledge that Software is not designed, licensed or intended for
  32.  * use in the design, construction, operation or maintenance of any nuclear
  33.  * facility.
  34.  */
  35.  
  36. /*
  37.  * @(#)SymbolTest.java    1.7 02/06/13
  38.  */
  39. import java.awt.*;
  40. import java.awt.event.*;
  41. import java.applet.Applet;
  42.  
  43. public class SymbolTest extends Applet implements ActionListener, ItemListener {
  44.  
  45.     SymbolCanvas symbols; 
  46.     TextField baseText;
  47.  
  48.     static final int SYMBOL_BASE = 0x2200;
  49.     static final int DINGBAT_BASE = 0x2700;
  50.     static final int GREEK_BASE = 0x0370;
  51.  
  52.     public void init() {
  53.     setLayout(new BorderLayout());
  54.  
  55.         Panel panel = new Panel();
  56.  
  57.         panel.add(new Label("Font:"));
  58.         Choice fontList = new Choice();
  59.  
  60.         String[] fontNames = getToolkit().getFontList();
  61.  
  62.         for (int i = 0; i < fontNames.length; i++) {
  63.             fontList.addItem(fontNames[i]);
  64.         }
  65.         fontList.addItemListener(this);
  66.         panel.add(fontList);
  67.         Font defaultFont = new Font(fontNames[0], Font.PLAIN, 16);
  68.  
  69.         panel.add(new Label("Unicode base:"));
  70.         baseText = new TextField(Integer.toHexString(DINGBAT_BASE), 4);
  71.         baseText.setFont(new Font("Monospaced", Font.PLAIN, 12));
  72.         baseText.addActionListener(this);
  73.         panel.add(baseText);
  74.         add("North", panel);
  75.  
  76.         ScrollPane sp = new ScrollPane();
  77.         symbols = new SymbolCanvas(defaultFont, DINGBAT_BASE);
  78.         sp.add(symbols);
  79.         add("Center", sp);
  80.  
  81.         add("South", new Label("Symbols=0x2200, Dingbats=0x2700, Greek=0x0370"));
  82.     }
  83.  
  84.     public void itemStateChanged(ItemEvent e) {
  85.         if (e.getStateChange() == ItemEvent.SELECTED) {
  86.             String fontName = (String)e.getItem();
  87.             symbols.setFont(new Font(fontName, Font.PLAIN, 16));
  88.         }
  89.     }
  90.  
  91.     public void actionPerformed(ActionEvent e) {
  92.         try {
  93.             int newBase = Integer.valueOf(e.getActionCommand(), 16).intValue();
  94.             symbols.setBase(newBase);
  95.         } catch (NumberFormatException nfe) {
  96.             Toolkit.getDefaultToolkit().beep();
  97.             baseText.select(0, Integer.MAX_VALUE);
  98.         }
  99.     }
  100.  
  101.     /*
  102.      * This class demonstrates how adapter classes can be used to avoid
  103.      * creating empty methods to satisfy an event listener interface.
  104.      */
  105.     static class MyAdapter extends WindowAdapter {
  106.         public void windowClosing(WindowEvent e) {
  107.             System.exit(0);
  108.         }
  109.     }
  110.  
  111.     public static void main(String args[]) {
  112.     Frame f = new Frame("SymbolTest");
  113.     SymbolTest symbolTest = new SymbolTest();
  114.  
  115.     symbolTest.init();
  116.     symbolTest.start();
  117.  
  118.     f.add("Center", symbolTest);
  119.     f.pack();
  120.     f.setSize(400, 500);
  121.     f.show();
  122.  
  123.         f.addWindowListener(new MyAdapter());
  124.     }
  125. }
  126.  
  127. class SymbolCanvas extends Canvas {
  128.     Font font;
  129.     int charHeight;
  130.     int charWidth;
  131.     int charBase;
  132.  
  133.     public SymbolCanvas(Font font, int base) {
  134.         FontMetrics fm = Toolkit.getDefaultToolkit().getFontMetrics(font);
  135.         charHeight = fm.getHeight() + 3;
  136.         charWidth = fm.getMaxAdvance() + 4;
  137.         charBase = base;
  138.         setSize(charWidth * 16 + 60, charHeight * 16 + 10);
  139.     }
  140.  
  141.     public void setBase(int base) {
  142.         charBase = base;
  143.         repaint();
  144.     }
  145.  
  146.     public void setFont(Font font) {
  147.         this.font = font;
  148.         repaint();
  149.     }
  150.  
  151.     public void paint(Graphics g) {
  152.         g.setFont(font);
  153.         g.setColor(Color.black);
  154.         char[] carray = new char[1];
  155.         int c = charBase;
  156.         int y = 20;
  157.         for (int v = 0; v < 16; v++) {
  158.             g.drawString(Integer.toHexString(c), 10, y);
  159.             int x = 60;
  160.             for (int h = 0; h < 16; h++) {
  161.                 carray[0] = (char)c++;
  162.                 g.drawChars(carray, 0, 1, x, y);
  163.                 x += charWidth;
  164.             }
  165.             y += charHeight;
  166.         }
  167.     }
  168. }
  169.