Java Tips Weblog

  • Blog Stats

    • 1,249,665 hits
  • Categories

  • Archives

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

  • Font family
  • Font size
  • Bold
  • Italic
  • Underline
  • Strikethrough
  • Kerning
  • Foreground / Background
  • Swap colors
  • Width
  • Tracking
  • Superscript

VisualFontDesigner

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,
ActionListener cancelListener)
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.

Try The Demo

Launch – Using Java™ Web Start (JRE 6 required)

Get The Code

VisualFontDesigner.java
FontDesigner.java
FontModel.java
TextAttributeConstants.java

Related Reading

Java API: java.awt.Font
Java API: java.awt.font.TextAttribute
Java API: javax.swing.JColorChooser

About these ads

4 Responses to “Visual Font Designer”

  1. aliefte said

    it’s very nice blog.. thanks so much for share knowledge about java.. this blog is helpfull to me whose beginner java programming.. :)

  2. Darryl Burke said

    Thank you, Aliefte. We’re glad you like it.

    Cheers, Darryl

  3. André Uhres said

    Hi Darryl,

    thanks for this nice font designer. I suggest the following minor modifications:

    FontModel: use chain of .append methods instead of string concatenation


    sb.append("attributes.put(TextAttribute.").append(keyName).append(", ");
    ...
    sb.append("new Color(").append(color.getRed()).append(", ")
    .append(color.getGreen()).append(", ")
    .append(color.getBlue()).append(")");
    ...
    sb.append("TextAttribute.").append(valueName);

    TextAttributeConstants: replace StringBuffer with StringBuilder


    StringBuilder buffer = new StringBuilder(name.length());

    Cheers,
    André

    • Darryl Burke said

      Thank you, André for your useful comments. I’ve started work on a more general implementation of FontModel for use with a menu or toolbar, with additional interfaces/classes being factored out. I’ll include your suggestions in the next iteration. No, there’s no target date for publishing ;) I work on code when the mood takes me.

      Thanks again, Darryl

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

Join 95 other followers

%d bloggers like this: