/* * [FontShowerAwt.java] * * Summary: displays various fonts in AWT. * * Copyright: (c) 2005-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 1.8 2005-08-10 based on Swing version * 1.9 2005-08-11 Separate out FontSamples class to make it common * to FontShower and FontShowerAwt. * Remove pointless repaint. * 2.0 2005-09-01 add Japanese Hiragana and Katagana char. * 2.1 2006-01-08 default is now TextArea to avoid giving impression * all fonts work in AWT. * 2.2 2006-03-05 reformat with IntelliJ, add Javadoc * 2.3 2007-04-10 add extra Chinese chars to sample. * 2.4 2007-05-03 more reserved colour scheme. * 2.5 2007-09-14 new layout for controls * 2.6 2007-10-02 fix recently introduced bug. Display all fonts for canvas. * 2.7 2008-05-26 add arrows to sample chars * 2.8 2008-11-20 add Esperanto Accented letters * 2.9 2009-01-23 add Kanji digits to font samples */ package com.mindprod.fontshowerawt; import com.mindprod.common18.Build; import com.mindprod.common18.CMPAboutBox; import com.mindprod.common18.FontFactory; import com.mindprod.common18.Hybrid; import com.mindprod.common18.Misc; import com.mindprod.common18.VersionCheck; import java.applet.Applet; import java.awt.BorderLayout; import java.awt.Button; import java.awt.Checkbox; import java.awt.CheckboxGroup; import java.awt.Choice; import java.awt.Color; import java.awt.Font; import java.awt.GraphicsEnvironment; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import java.awt.Label; import java.awt.Panel; import java.awt.ScrollPane; import java.awt.TextArea; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; /** * displays various fonts in AWT. *

* An Applet that lets you choose the font family, size, style * text color, background color and displays a pangram in the selected * font. Uses only AWT. * Use either as Applet with * <applet code="com.mindprod.fontshowerawt.FontShowerAwt.class" * archive="fontshowerawt.jar" * width="520" * height="390" * vspace="10" * hspace="10" * alt="Sorry, you need Java to run FontShowerAwt"> * Sorry, you need Java to run FontShowerAwt * </applet> *

* or as an application with: * java -ea -jar fontshowerawt.jar * * @author Roedy Green, Canadian Mind Products * @version 2.9 2009-01-23 add Kanji digits to font samples * @since 2005 */ public final class FontShowerAwt extends Applet { /** * height of Applet box in pixels. Does not include surrounding frame. */ private static final int APPLET_HEIGHT = 504; /** * Width of Applet box in pixels, wide enough for long filename and wide font.. */ private static final int APPLET_WIDTH = 1008; private static final int FIRST_COPYRIGHT_YEAR = 2005; /** * undisplayed copyright notice * * @noinspection UnusedDeclaration */ private static final String EMBEDDED_COPYRIGHT = "Copyright: (c) 2005-2017 Roedy Green, Canadian Mind Products, http://mindprod.com"; private static final String RELEASE_DATE = "2009-01-23"; private static final String TITLE_STRING = "AWT Font Shower"; private static final String VERSION_STRING = "2.9"; /** * colour for about button */ private static final Color BACKGROUND_FOR_BUTTON = new Color( 0x008000 ); /** * for titles */ private static final Color FOREGROUND_FOR_TITLE = new Color( 0xdc143c ); /** * font for titles */ private static final Font FONT_FOR_TITLE = FontFactory.build( "Dialog", Font.BOLD, 14 ); /** * current color of the background, light green to match mindprod.com background. */ private static Color currentBackground = Build.BACKGROUND_FOR_BLENDING; /** * current font we are displaying */ private static String currentFontName = "Dialog"; /** * size a in points/pixels of font we are displaying */ private static int currentFontSize = 18; /** * current color of the forgeground, dark green to start */ private static Color currentForeground = new Color( 0x004000 ); /** * where we display the sample fonts. */ private AntiAliastedFontedTextArea displayCanvas; /** * about button */ private Button about; /** * invoke background colour chooser */ private Button chooseBackgroundColor; /** * invoke foreground colour chooser */ private Button chooseForegroundColor; /** * toggle anti-alias state */ private Checkbox toggleAntialias; /** * toggle bold state */ private Checkbox toggleBold; /** * toggle italic state */ private Checkbox toggleItalics; /** * render with Canvas */ private Checkbox withCanvas; /** * render with TextArea */ private Checkbox withTextArea; /** * list of possible fonts */ private Choice allFontChoices; /** * list of possible fonts */ private Choice logicalFontChoices; /** * possible sizes */ private Choice sizeChoices; /** * used to select a foreground or background Color. */ private ColorChooser colorChooser; /** * labels font point sizes */ private Label ptLabel; /** * title and version of the app */ private Label title; /** * toolbar across to top where all controls go */ private Panel toolBar; /** * scrolls displayArea */ private ScrollPane canvasScroller; /** * Can only show the basic Logical fonts, and even then stripped down. */ private TextArea displayTextArea; /** * true if using Canvas rather than TextArea. Start with TextArea */ private boolean usingCanvas = false; /** * displays the font sample in a new font */ private void displayFontSample() { boolean wasUsingCanvas = usingCanvas; usingCanvas = withCanvas.getState(); if ( usingCanvas != wasUsingCanvas ) { if ( usingCanvas ) { flipFromTextArea(); flipToCanvas(); } else { flipFromCanvas(); flipToTextArea(); } } int style = Font.PLAIN; if ( toggleBold.getState() ) { style |= Font.BOLD; } if ( toggleItalics.getState() ) { style |= Font.ITALIC; } currentFontSize = Integer.parseInt( sizeChoices.getSelectedItem() ); if ( usingCanvas ) { currentFontName = allFontChoices.getSelectedItem(); // don't use FontFactory. We want the official font. Font font = new Font( currentFontName, style, currentFontSize ); displayCanvas.setFont( font ); displayCanvas.setBackground( currentBackground ); displayCanvas.setForeground( currentForeground ); displayCanvas.setAntialias( toggleAntialias.getState() ); canvasScroller.getVAdjustable() .setUnitIncrement( currentFontSize + 5 ); } else { currentFontName = logicalFontChoices.getSelectedItem(); // don't use FontFactory. We want the official font. Font font = new Font( currentFontName, style, currentFontSize ); displayTextArea.setFont( font ); displayTextArea.setBackground( currentBackground ); displayTextArea.setForeground( currentForeground ); // whether anti-aliased determined by OS } // text set at allocation time. // changing font, let alone changing from TextArea to Canvas // can invalidate entire layout this.validate();// decides if need scrollbars } /** * Stop using a Canvas */ private void flipFromCanvas() { allFontChoices.setVisible( false ); this.remove( canvasScroller ); } /** * stop using a TextArea */ private void flipFromTextArea() { logicalFontChoices.setVisible( false ); this.remove( displayTextArea ); } /** * start using a Canvas */ private void flipToCanvas() { this.add( canvasScroller, BorderLayout.CENTER ); allFontChoices.setVisible( true ); toggleAntialias.setEnabled( true ); allFontChoices.select( logicalFontChoices.getSelectedItem() ); } /** * Start using a TextArea */ private void flipToTextArea() { // with TextArea we have no contror over whether anti-aliased this.add( displayTextArea, BorderLayout.CENTER ); logicalFontChoices.setVisible( true ); toggleAntialias.setEnabled( false ); // Match font if Canvas is currently using a Logical font, // otherwise, leaves as it was. logicalFontChoices.select( allFontChoices.getSelectedItem() ); } /** * layout he toolback controls */ private void layoutToolbar() { toolBar.setLayout( new GridBagLayout() ); // Layout // ----- 0 ------- -1----------- 2 -- 3 -----4----- 5-------6 // 0 fontchoice--- canvas--- anti-alias -- bold -background about // 1 ------------- -textarea -size--- -pt- -italic -foreground ----- // 2 // x y w h wtx wty anchor fill T L B R padx pady toolBar.add( title, new GridBagConstraints( 0, 0, 1, 1, 10.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets( 5, 10, 5, 5 ), 0, 0 ) ); toolBar.add( logicalFontChoices, new GridBagConstraints( 0, 1, 1, 1, 10.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets( 5, 10, 5, 5 ), 0, 0 ) ); toolBar.add( allFontChoices, new GridBagConstraints( 0, /* same slot */ 0, 1, 2, 10.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets( 5, 10, 5, 5 ), 0, 0 ) ); // x y w h wtx wty anchor fill T L B R padx pady toolBar.add( withCanvas, new GridBagConstraints( 1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 5, 5, 0, 5 ), 0, 0 ) ); // x y w h wtx wty anchor fill T L B R padx pady toolBar.add( withTextArea, new GridBagConstraints( 1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 5, 5, 5, 5 ), 0, 0 ) ); toolBar.add( toggleAntialias, new GridBagConstraints( 2, 0, 2, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 5, 10, 0, 5 ), 0, 0 ) ); // x y w h wtx wty anchor fill T L B R padx pady toolBar.add( sizeChoices, new GridBagConstraints( 2, 1, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 5, 10, 5, 5 ), 0, 0 ) ); // x y w h wtx wty anchor fill T L B R padx pady toolBar.add( ptLabel, new GridBagConstraints( 3, 1, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 5, 0, 5, 5 ), 0, 0 ) ); // x y w h wtx wty anchor fill T L B R padx pady toolBar.add( toggleBold, new GridBagConstraints( 4, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 5, 5, 0, 5 ), 0, 0 ) ); // x y w h wtx wty anchor fill T L B R padx pady toolBar.add( toggleItalics, new GridBagConstraints( 4, 1, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 5, 5, 5, 5 ), 0, 0 ) ); // x y w h wtx wty anchor fill T L B R padx pady toolBar.add( chooseBackgroundColor, new GridBagConstraints( 5, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 5, 5, 0, 5 ), 0, 0 ) ); // x y w h wtx wty anchor fill T L B R padx pady toolBar.add( chooseForegroundColor, new GridBagConstraints( 5, 1, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 5, 5, 5, 5 ), 0, 0 ) ); // x y w h wtx wty anchor fill T L B R padx pady toolBar.add( about, new GridBagConstraints( 6, 0, 1, 2, 0.0, 0.0, GridBagConstraints.NORTHEAST, GridBagConstraints.NONE, new Insets( 5, 5, 5, 10 ), 10, 2 ) ); this.add( toolBar, BorderLayout.NORTH ); } /** * main to convert FontShowerAwt Applet into an hybrid application. * * @param args ignored. */ public static void main( String args[] ) { Hybrid.fireup( new FontShowerAwt(), TITLE_STRING + " " + VERSION_STRING, APPLET_WIDTH, APPLET_HEIGHT ); } /** * Called by the browser or Applet viewer to inform * this Applet that it is being reclaimed and that it should destroy * any resources that it has allocated. */ public void destroy() { about = null; allFontChoices = null; canvasScroller = null; chooseBackgroundColor = null; chooseForegroundColor = null; colorChooser = null; displayCanvas = null; displayTextArea = null; logicalFontChoices = null; ptLabel = null; sizeChoices = null; title = null; toggleAntialias = null; toggleBold = null; toggleItalics = null; toolBar = null; withCanvas = null; withTextArea = null; } /** * Called by the browser or Applet viewer to inform * this Applet that it has been loaded into the system. */ public void init() { // need 1.2 in order to enumerate fontchoices without deprecation. if ( !VersionCheck.isJavaVersionOK( 1, 8, 0, this ) ) { // effectively abort return; } this.setLayout( new BorderLayout() ); toolBar = new Panel(); // use same ItemListener for many components ItemListener itemListener = new ItemListener() { public void itemStateChanged( ItemEvent e ) { displayFontSample(); } }; title = new Label( TITLE_STRING + " " + VERSION_STRING + " build " + Build .BUILD_NUMBER ); title.setFont( FONT_FOR_TITLE ); title.setForeground( FOREGROUND_FOR_TITLE ); // font names GraphicsEnvironment ge = GraphicsEnvironment .getLocalGraphicsEnvironment(); String[] possibleFontNames = ge.getAvailableFontFamilyNames(); allFontChoices = new Choice(); for ( final String possibleFontName : possibleFontNames ) { allFontChoices.add( possibleFontName ); } allFontChoices.select( currentFontName ); allFontChoices.addItemListener( itemListener ); logicalFontChoices = new Choice(); logicalFontChoices.add( "Dialog" ); logicalFontChoices.add( "DialogInput" ); logicalFontChoices.add( "Monospaced" ); logicalFontChoices.add( "SansSerif" ); logicalFontChoices.add( "Serif" ); logicalFontChoices.select( "Dialog" ); logicalFontChoices.addItemListener( itemListener ); sizeChoices = new Choice(); for ( int i = 8; i <= 30; i++ ) { sizeChoices.add( Integer.toString( i ) ); } for ( int i = 35; i <= 70; i += 5 ) { sizeChoices.add( Integer.toString( i ) ); } sizeChoices.select( Integer.toString( currentFontSize ) ); sizeChoices.addItemListener( itemListener ); ptLabel = new Label( "pts", Label.LEFT ); toggleBold = new Checkbox( "Bold", false ); toggleBold.addItemListener( itemListener ); // italic button toggleItalics = new Checkbox( "Italics", false ); toggleItalics.addItemListener( itemListener ); CheckboxGroup renderMode = new CheckboxGroup(); withCanvas = new Checkbox( "Canvas", renderMode, false ); withCanvas.addItemListener( itemListener ); withTextArea = new Checkbox( "TextArea", renderMode, true ); withTextArea.addItemListener( itemListener ); // antialias button toggleAntialias = new Checkbox( "Anti-alias", false ); toggleAntialias.addItemListener( itemListener ); chooseBackgroundColor = new Button( "Background Colour" ); chooseBackgroundColor.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) { currentBackground = colorChooser.query( false /* background */, currentBackground, currentForeground ); displayFontSample(); } } ); colorChooser = new ColorChooser( Misc.getParentFrame( this ) ); chooseForegroundColor = new Button( "Text Colour" ); chooseForegroundColor.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) { currentForeground = colorChooser.query( true /* foreground */, currentBackground, currentForeground ); displayFontSample(); } } ); about = new Button( "About" ); about.setForeground( Color.white ); about.setBackground( BACKGROUND_FOR_BUTTON ); about.setFont( FontFactory.build( "Dialog", Font.BOLD, 16 ) ); about.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) { // open aboutbox frame new CMPAboutBox( TITLE_STRING, VERSION_STRING, "Shows you what fonts are available in AWT and what", "they look like in various styles, sizes and colours.", "freeware", RELEASE_DATE, FIRST_COPYRIGHT_YEAR, "Roedy Green", "FONTSHOWERAWT", "1.6" ); } } ); displayTextArea = new TextArea( FontSamples.getFontSampleText() ); // don't add. displayCanvas = new AntiAliastedFontedTextArea(); displayCanvas.setTextLines( FontSamples.getFontSampleLines() ); canvasScroller = new ScrollPane( ScrollPane.SCROLLBARS_AS_NEEDED ); canvasScroller.add( displayCanvas ); layoutToolbar(); flipFromCanvas(); flipToTextArea(); displayFontSample(); } }