Visual Font Designer
Posted by Darryl Burke on November 30, 2008
New in version 2: VisualFontDesigner can now be used as a Font chooser, much on the lines of JColorChooser
When trying to create an attractive Font, the first challenge is usually to research the difference that applicable attributes make to its appearance. Applying and reviewing numerous combinations of attributes can be a tedious exercise.
VisualFontDesigner makes it a breeze to quickly visualize the effects of most of the available attributes, with the added bonus of generating a code block that can be directly copied to recreate the font elsewhere.
The nature of GUI programming demands some familiarity with setting custom fonts. The Font class makes the setting of only two attributes – Bold and Italic – easy, while largely obscuring the wide range of font variations possible by application of other attributes.
While the primary purpose behind VisualFontDesigner is to provide a quick, easy, and fun way to customize a font, it is also hoped that perusing the generated code will familiarize the casual GUI programmer with the APIs involved in creating a range of custom fonts, and also encourage experimenting to produce still more variations.
All controls of VisualFontDesigner are furnished with tool tips to facilitate immediate productive use of the tool. Fonts that lack the glyphs for common text are shown in red in the Font family combo box.
The attributes that can be manipulated are
With the update to version 2, VisualFontDesigner has been reinvented as a Font chooser, while still allowing it to be run as a standalone application. The class now extends JComponent, which means it can be added to any GUI. New methods have been added to get and set the custom Font, add PropertyChangeListeners and create and show a JDialog or JFrame with the VisualFontDesigner. If you are experienced with JColorChooser, programming VisualFontDesigner to allow users to choose a Font will be child’s play, as the methods follow the same familiar pattern.
Additionally, a VisualFontDesigner can now be constructed with an initial Font, making it easier to review progress and continue. When constructed with a Component, the startup Font will be that returned by the Component’s getFont(). If the Component is a subclass of JTextComponent, the text component’s text will be displayed in the preview text area. When used in this way, the code area and Copy button will not be shown, as the code is not relevant to an end user.
This is a summary of the added public constructors and methods, with a brief description. All methods of the JComponent hierarchy are of course inherited, but the notably useful one is void addPropertyChangeListener(PropertyChangeListener listener). Each time the Font is modified, VisualFontDesigner will fire a “font” property change.
|VisualFontDesigner()||Constructs a VisualFontDesigner with default font and sample text, and no code generator.|
|VisualFontDesigner(Component component)||Constructs a VisualFontDesigner with an initial font obtained from the component and no code generator.
If the component is a JTextComponent and contains text, the sample text will be set to this.
|VisualFontDesigner(Font font)||Constructs a VisualFontDesigner with the specified initial font, default sample text and no code generator.|
|Font getDesignedFont()||Returns the Font designed in this designer.|
|void setDesignedFont(Font font)||Sets the designer’s designed Font to the passed in font and updates its GUI components accordingly.|
|void showFrame(int closeOperation)||Launches an instance of VisualFontDesigner in a JFrame with the specified close operation.|
|Font showDialog(Component parent)||Shows a modal font designer dialog and blocks until the dialog is hidden. If the user presses the “OK” button, this method disposes the dialog and returns the designed Font. If the user presses the “Cancel” button or closes the dialog without pressing “OK”, then this method resets the font to its initial value, disposes the dialog and returns null.|
|Font showDialog(Component parent, String title, Font initialFont)||Shows a modal font designer dialog with the specified title and initial font and blocks until the dialog is hidden. If the user presses the “OK” button, this method disposes the dialog and returns the designed Font. If the user presses the “Cancel” button or closes the dialog without pressing “OK”, then this method resets the designed font to the font of the parent, or to the initial font if parent is null, disposes the dialog and returns null.|
|void createDialog(Component parent, String title, boolean modal,
VisualFontDesigner designer, ActionListener okListener,
|Creates and returns a new dialog containing the specified VisualFontDesigner along with “OK” and “Cancel” buttons. If the “OK” or “Cancel” buttons are pressed, the dialog is automatically disposed.|
|String getCode()||Generates and returns code that can programatically recreate the designed Font.|
The Foreground and Background of a Component may be set to a Color. The corresponding attributes of Font, however, may be instances of any class implementing Paint. This makes it possible to design some interesting fonts using MultipleGradientPaint and TexturePaint.
For ease of use, VisualFontDesigner limits the selection of the Foreground and Background attributes to Color.
The supporting classes created in the course of revamping VisualFontDesigner may feature in future blog posts. For the time being, the code download links are provided in this page.
The Web Start demo has been designed to let you play with the new features of VisualFontDesigner and get a feel for the simplicity of the code required to use it.