KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java2d > demos > Fonts > AllFonts


1 /*
2  * @(#)AllFonts.java 1.36 06/08/29
3  *
4  * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * -Redistribution of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  *
12  * -Redistribution in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * Neither the name of Sun Microsystems, Inc. or the names of contributors may
17  * be used to endorse or promote products derived from this software without
18  * specific prior written permission.
19  *
20  * This software is provided "AS IS," without a warranty of any kind. ALL
21  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
22  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
23  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
24  * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
25  * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
26  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
27  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
28  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
29  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
30  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
31  *
32  * You acknowledge that this software is not designed, licensed or intended
33  * for use in the design, construction, operation or maintenance of any
34  * nuclear facility.
35  */

36
37 /*
38  * @(#)AllFonts.java 1.36 06/08/29
39  */

40
41 package java2d.demos.Fonts;
42
43 import static java.awt.Color JavaDoc.*;
44 import java.awt.*;
45 import java.awt.event.*;
46 import java.awt.font.FontRenderContext JavaDoc;
47 import java.awt.font.TextLayout JavaDoc;
48 import java.util.Vector JavaDoc;
49 import javax.swing.*;
50 import javax.swing.border.*;
51 import javax.swing.event.*;
52 import java2d.AnimatingControlsSurface;
53 import java2d.CustomControls;
54
55
56
57 /**
58  * Scrolling text of fonts returned from GraphicsEnvironment.getAllFonts().
59  */

60 public class AllFonts extends AnimatingControlsSurface {
61
62     private static Vector JavaDoc<Font> fonts = new Vector JavaDoc<Font>();
63     static {
64         GraphicsEnvironment ge =
65             GraphicsEnvironment.getLocalGraphicsEnvironment();
66         for (Font font : ge.getAllFonts()) {
67             if (font.canDisplayUpTo(font.getName()) != 0) {
68                 fonts.addElement(font);
69             }
70         }
71     }
72     private int nStrs;
73     private int strH;
74     private int fi;
75     protected int fsize = 14;
76     protected Vector JavaDoc<Font> v = new Vector JavaDoc<Font>();
77
78
79     public AllFonts() {
80         setBackground(WHITE);
81         setSleepAmount(500);
82         setControls(new Component[] { new DemoControls(this) });
83     }
84
85
86     public void handleThread(int state) { }
87
88
89     public void reset(int w, int h) {
90         v.clear();
91         Font f = fonts.get(0).deriveFont(Font.PLAIN,fsize);
92         FontMetrics fm = getFontMetrics(f);
93         strH = (int) (fm.getAscent() + fm.getDescent());
94         nStrs = h/strH + 1;
95         fi = 0;
96     }
97
98
99     public void step(int w, int h) {
100         if (fi < fonts.size()) {
101             v.addElement(fonts.get(fi).deriveFont(Font.PLAIN,fsize));
102         }
103         if (v.size() == nStrs && v.size() != 0 || fi > fonts.size()) {
104             v.removeElementAt(0);
105         }
106         fi = (v.size() == 0) ? 0 : ++fi;
107     }
108
109
110     public void render(int w, int h, Graphics2D g2) {
111
112         g2.setColor(BLACK);
113
114         int yy = (fi >= fonts.size()) ? 0 : h - v.size() * strH - strH/2;
115
116         for (int i = 0; i < v.size(); i++) {
117             Font f = v.get(i);
118             int sw = getFontMetrics(f).stringWidth(f.getName());
119             g2.setFont(f);
120             g2.drawString(f.getName(), (int) (w/2-sw/2),yy += strH);
121         }
122     }
123
124
125     public static void main(String JavaDoc argv[]) {
126         createDemoFrame(new AllFonts());
127     }
128
129
130     static class DemoControls extends CustomControls implements ActionListener, ChangeListener {
131
132         AllFonts demo;
133         JSlider slider;
134         int fsize[] = { 8, 14, 18, 24 };
135         JMenuItem menuitem[] = new JMenuItem[fsize.length];
136         Font font[] = new Font[fsize.length];
137
138
139         public DemoControls(AllFonts demo) {
140             this.demo = demo;
141             setBackground(GRAY);
142
143             int sleepAmount = (int) demo.getSleepAmount();
144             slider = new JSlider(JSlider.HORIZONTAL, 0, 999, sleepAmount);
145             slider.setBorder(new EtchedBorder());
146             slider.setPreferredSize(new Dimension(90,22));
147             slider.addChangeListener(this);
148             add(slider);
149             JMenuBar menubar = new JMenuBar();
150             add(menubar);
151             JMenu menu = (JMenu) menubar.add(new JMenu("Font Size"));
152             for (int i = 0; i < fsize.length; i++) {
153                 font[i] = new Font("serif", Font.PLAIN, fsize[i]);
154                 menuitem[i] = menu.add(new JMenuItem(String.valueOf(fsize[i])));
155                 menuitem[i].setFont(font[i]);
156                 menuitem[i].addActionListener(this);
157             }
158         }
159
160
161         public void actionPerformed(ActionEvent e) {
162             for (int i = 0; i < fsize.length; i++) {
163                 if (e.getSource().equals(menuitem[i])) {
164                     demo.fsize = fsize[i];
165                     Dimension d = demo.getSize();
166                     demo.reset(d.width, d.height);
167                     break;
168                 }
169             }
170         }
171
172
173         public void stateChanged(ChangeEvent e) {
174             demo.setSleepAmount(slider.getValue());
175         }
176     }
177 }
178
Popular Tags