KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > deliver > util > graphics > FontSaver


1 /* ===============================================================================
2  *
3  * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4  *
5  * ===============================================================================
6  *
7  * Copyright (C)
8  *
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License version 2, as published by the
11  * Free Software Foundation. See the file LICENSE.html for more information.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20  *
21  * ===============================================================================
22  */

23
24 package org.infoglue.deliver.util.graphics;
25
26 // FontSaver.java
27
// conserves RAM and time by reusing Font and Font peer objects.
28
// Instead of saying:
29
// Font f = new Font("Dialog", 12, Font.PLAIN);
30
// say:
31
// Font f = FontSaver.create("Dialog", 12, Font.PLAIN);
32
// Version 1.2 1998 November 10 - new address and phone.
33
// Version 1.1 1998 January 15
34

35 /** copyright (c) 1998-2002 Roedy Green, Canadian Mind Products
36   * #327 - 964 Heywood Avenue
37   * Victoria, BC Canada V8V 2Y5
38   * mailto:roedy@mindprod.com
39   * http://mindprod.com
40   */

41
42 // May be distributed freely and used for any purpose but military.
43

44
45 import java.awt.Font JavaDoc;
46 import java.util.Hashtable JavaDoc;
47
48 public class FontSaver
49 {
50     
51     private static Hashtable JavaDoc h;
52    
53     /**
54      * Works just like the Font Constructor:
55      * Creates a new font with the specified name, style and point size.
56      * @param name the font name
57      * @param style the constant style used
58      * @param size the point size of the font
59      */

60    
61     public static Font JavaDoc create (String JavaDoc name, int style, int size)
62     {
63         if (h == null)
64             h = new Hashtable JavaDoc(101,.75f);
65       
66         FontKey fontKey = new FontKey(name, style, size);
67         Font JavaDoc prevFont = (Font JavaDoc)h.get(fontKey);
68         
69         if (prevFont != null)
70             return prevFont;
71       
72         Font JavaDoc newFont = new Font JavaDoc(name, style, size);
73       
74         h.put(fontKey, newFont);
75         
76         return newFont;
77     }
78
79 }
80
81
82 /*
83  * FontKey is just the name, style and size fields of a font,
84  * enough to identify it, a key to find it in a Hashtable.
85  */

86
87 class FontKey
88 {
89
90    /**
91      * constructor
92      * Creates a new FontKey id object with the specified name, style and point size.
93      * @param name the font name
94      * @param style the constant style used
95      * @param size the point size of the font
96      */

97
98     public FontKey (String JavaDoc name, int style, int size)
99     {
100         this.name = name;
101         this.style = style;
102         this.size = size;
103     }
104
105     public boolean equals(Object JavaDoc obj)
106     {
107         if ( obj instanceof FontKey )
108         {
109             FontKey fontKey = (FontKey)obj;
110             return(size == fontKey.size) && (style == fontKey.style) && name.equals(fontKey.name);
111         }
112         return false;
113     }
114
115    /**
116     * Returns a hashcode for this font.
117     */

118     
119     public int hashCode()
120     {
121         return name.hashCode() ^ style ^ size;
122     }
123
124    /**
125     * The logical name of this font.
126     */

127     
128     protected String JavaDoc name;
129
130    /**
131     * The style of the font. This is the sum of the
132     * constants PLAIN, BOLD, or ITALIC.
133     */

134     
135     protected int style;
136
137    /**
138     * The point size of this font.
139     */

140    
141     protected int size;
142
143 }
Popular Tags