home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / JBuilder8.iso / Solaris / resource / jre / demo / jfc / Java2D / src / java2d / demos / Fonts / AllFonts.java next >
Encoding:
Java Source  |  2002-09-06  |  5.7 KB  |  177 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.  * @(#)AllFonts.java    1.30 02/06/13
  38.  */
  39.  
  40. package java2d.demos.Fonts;
  41.  
  42. import java.awt.*;
  43. import java.awt.event.*;
  44. import java.awt.font.FontRenderContext;
  45. import java.awt.font.TextLayout;
  46. import java.util.Vector;
  47. import javax.swing.*;
  48. import javax.swing.border.*;
  49. import javax.swing.event.*;
  50. import java2d.AnimatingControlsSurface;
  51. import java2d.CustomControls;
  52.  
  53.  
  54.  
  55. /**
  56.  * Scrolling text of fonts returned from GraphicsEnvironment.getAllFonts().
  57.  */
  58. public class AllFonts extends AnimatingControlsSurface {
  59.  
  60.     private static Vector fonts = new Vector();
  61.     static {
  62.         GraphicsEnvironment ge = 
  63.             GraphicsEnvironment.getLocalGraphicsEnvironment();
  64.         Font allfonts[] = ge.getAllFonts();
  65.         for (int i = 0; i < allfonts.length; i++) {
  66.             if (allfonts[i].canDisplayUpTo(allfonts[i].getName()) != 0) {
  67.                 fonts.addElement(allfonts[i]);
  68.             }
  69.         }
  70.     }
  71.     private int nStrs;
  72.     private int strH;
  73.     private int fi;
  74.     protected int fsize = 14;
  75.     protected Vector v = new Vector();
  76.  
  77.  
  78.     public AllFonts() {
  79.         setBackground(Color.white);
  80.         setSleepAmount(500);
  81.         setControls(new Component[] { new DemoControls(this) });
  82.     }
  83.  
  84.  
  85.     public void handleThread(int state) { }
  86.  
  87.  
  88.     public void reset(int w, int h) {
  89.         v.clear();
  90.         Font f = ((Font) fonts.get(0)).deriveFont(Font.PLAIN,fsize);
  91.         FontMetrics fm = getFontMetrics(f);
  92.         strH = (int) (fm.getAscent()+fm.getDescent());
  93.         nStrs = h/strH + 1;
  94.         fi = 0;
  95.     }
  96.  
  97.  
  98.     public void step(int w, int h) {
  99.         if (fi < fonts.size()) {
  100.             v.addElement(((Font) fonts.get(fi)).deriveFont(Font.PLAIN,fsize));
  101.         }
  102.         if (v.size() == nStrs && v.size() != 0 || fi > fonts.size()) {
  103.             v.removeElementAt(0);
  104.         }
  105.         fi = (v.size() == 0) ? 0 : ++fi;
  106.     }
  107.  
  108.  
  109.     public void render(int w, int h, Graphics2D g2) {
  110.  
  111.         g2.setColor(Color.black);
  112.  
  113.         int yy = (fi >= fonts.size()) ? 0 : h - v.size() * strH - strH/2;
  114.  
  115.         for (int i = 0; i < v.size(); i++) {
  116.             Font f = (Font) v.get(i);
  117.             int sw = getFontMetrics(f).stringWidth(f.getName());
  118.             g2.setFont(f);
  119.             g2.drawString(f.getName(), (int) (w/2-sw/2),yy += strH);
  120.         }
  121.     }
  122.  
  123.  
  124.     public static void main(String argv[]) {
  125.         createDemoFrame(new AllFonts());
  126.     }
  127.  
  128.  
  129.     static class DemoControls extends CustomControls implements ActionListener, ChangeListener {
  130.  
  131.         AllFonts demo;
  132.         JSlider slider;
  133.         int fsize[] = { 8, 14, 18, 24 };
  134.         JMenuItem menuitem[] = new JMenuItem[fsize.length];
  135.         Font font[] = new Font[fsize.length];
  136.  
  137.  
  138.         public DemoControls(AllFonts demo) {
  139.             this.demo = demo;
  140.             setBackground(Color.gray);
  141.  
  142.             int sleepAmount = (int) demo.getSleepAmount();
  143.             slider = new JSlider(JSlider.HORIZONTAL, 0, 999, sleepAmount);
  144.             slider.setBorder(new EtchedBorder());
  145.             slider.setPreferredSize(new Dimension(90,22));
  146.             slider.addChangeListener(this);
  147.             add(slider);
  148.             JMenuBar menubar = new JMenuBar();
  149.             add(menubar);
  150.             JMenu menu = (JMenu) menubar.add(new JMenu("Font Size"));
  151.             for (int i = 0; i < fsize.length; i++) {
  152.                 font[i] = new Font("serif", Font.PLAIN, fsize[i]);
  153.                 menuitem[i] = menu.add(new JMenuItem(String.valueOf(fsize[i])));
  154.                 menuitem[i].setFont(font[i]);
  155.                 menuitem[i].addActionListener(this);
  156.             }
  157.         }
  158.  
  159.  
  160.         public void actionPerformed(ActionEvent e) {
  161.             for (int i = 0; i < fsize.length; i++) {
  162.                 if (e.getSource().equals(menuitem[i])) {
  163.                     demo.fsize = fsize[i];
  164.                     Dimension d = demo.getSize();
  165.                     demo.reset(d.width, d.height);
  166.                     break;
  167.                 }
  168.             }
  169.         }
  170.  
  171.  
  172.         public void stateChanged(ChangeEvent e) {
  173.             demo.setSleepAmount(slider.getValue());
  174.         }
  175.     }
  176. }
  177.