home *** CD-ROM | disk | FTP | other *** search
/ Chip 1999 July / CHIP-1999-07.iso / software / jdk / jdk121.exe / disk1 / data1.cab / demos / demo / applets / SymbolTest / SymbolTest.java < prev    next >
Encoding:
Java Source  |  1999-03-27  |  5.2 KB  |  160 lines

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