📄 font2dtest.java
字号:
/* * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * -Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * -Redistribution in binary form must reproduct the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the distribution. * * Neither the name of Sun Microsystems, Inc. or the names of contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * This software is provided "AS IS," without a warranty of any kind. ALL * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT * BE LIABLE FOR ANY DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT * OF OR RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THE SOFTWARE OR ITS * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN * IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. * * You acknowledge that Software is not designed, licensed or intended for * use in the design, construction, operation or maintenance of any nuclear * facility. *//* * @(#)Font2DTest.java 1.20 03/01/23 */import java.awt.BorderLayout;import java.awt.CheckboxGroup;import java.awt.Container;import java.awt.Dimension;import java.awt.Font;import java.awt.Graphics;import java.awt.GraphicsEnvironment;import java.awt.GridBagConstraints;import java.awt.GridBagLayout;import java.awt.GridLayout;import java.awt.Insets;import java.awt.Toolkit;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.ItemEvent;import java.awt.event.ItemListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.awt.image.BufferedImage;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.util.StringTokenizer;import javax.swing.*;import com.sun.image.codec.jpeg.JPEGCodec;import com.sun.image.codec.jpeg.JPEGImageDecoder;/** * Font2DTest.java * * @version @(#)Font2DTest.java 1.2 00/08/22 * @author Shinsuke Fukuda * @author Ankit Patel [Conversion to Swing - 01/07/30] *//// Main Font2DTest Classpublic final class Font2DTest extends JPanel implements ActionListener, ItemListener { /// JFrame that will contain Font2DTest private final JFrame parent; /// FontPanel class that will contain all graphical output private final FontPanel fp; /// RangeMenu class that contains info about the unicode ranges private final RangeMenu rm; /// Other menus to set parameters for text drawing private final ChoiceV2 fontMenu; private final JTextField sizeField; private final ChoiceV2 styleMenu; private final ChoiceV2 textMenu; private int currentTextChoice = 0; private final ChoiceV2 transformMenu; private final ChoiceV2 transformMenuG2; private final ChoiceV2 methodsMenu; private final JCheckBox useAntialiasCB; private final JCheckBox useFractionalCB; /// CheckboxMenuItems private CheckboxMenuItemV2 displayGridCBMI; private CheckboxMenuItemV2 force16ColsCBMI; private CheckboxMenuItemV2 showFontInfoCBMI; /// JDialog boxes private JDialog userTextDialog; private JTextArea userTextArea; private JDialog printDialog; private JDialog fontInfoDialog; private LabelV2 fontInfos[] = new LabelV2[2]; private JFileChooser filePromptDialog = null; private ButtonGroup printCBGroup; private JRadioButton printModeCBs[] = new JRadioButton[3]; /// Status bar private final LabelV2 statusBar; /// Universal font used for labels private final Font labelFont = new Font( "dialog", Font.BOLD, 12 ); /// Text filename private String tFileName; /// Initialize GUI variables and its layouts public Font2DTest( JFrame f, boolean isApplet ) { parent = f; rm = new RangeMenu( this, parent ); fp = new FontPanel( this, parent ); statusBar = new LabelV2(""); fontMenu = new ChoiceV2( this ); sizeField = new JTextField( "12", 3 ); styleMenu = new ChoiceV2( this ); textMenu = new ChoiceV2( ); // listener added later transformMenu = new ChoiceV2( this ); transformMenuG2 = new ChoiceV2( this ); methodsMenu = new ChoiceV2( this ); useAntialiasCB = new JCheckBox( "Antialiasing", false ); useFractionalCB = new JCheckBox(" Fractional Metrics", false ); useAntialiasCB.setFont( labelFont ); useFractionalCB.setFont( labelFont ); sizeField.addActionListener( this ); useAntialiasCB.addItemListener( this ); useFractionalCB.addItemListener( this ); setupPanel(); setupMenu( isApplet ); setupDialog( isApplet ); } /// Set up the main interface panel private void setupPanel() { GridBagLayout gbl = new GridBagLayout(); GridBagConstraints gbc = new GridBagConstraints(); gbc.fill = GridBagConstraints.BOTH; gbc.weightx = 1; gbc.insets = new Insets( 2, 0, 2, 2 ); this.setLayout( gbl ); addLabeledComponentToGBL( "Font:", fontMenu, gbl, gbc, this ); addLabeledComponentToGBL( "Size:", sizeField, gbl, gbc, this ); gbc.gridwidth = GridBagConstraints.REMAINDER; addLabeledComponentToGBL( "Transform:", transformMenu, gbl, gbc, this ); gbc.gridwidth = 1; addLabeledComponentToGBL( "Range:", rm, gbl, gbc, this ); addLabeledComponentToGBL( "Style:", styleMenu, gbl, gbc, this ); gbc.gridwidth = GridBagConstraints.REMAINDER; addLabeledComponentToGBL( "Text to use:", textMenu, gbl, gbc, this ); gbc.gridwidth = 1; /// ABP addLabeledComponentToGBL( "Method:", methodsMenu, gbl, gbc, this ); gbc.anchor = GridBagConstraints.EAST; gbc.gridwidth = GridBagConstraints.REMAINDER; addLabeledComponentToGBL( "Graphics2D Transform:", transformMenuG2, gbl, gbc, this ); gbc.gridwidth = 1; gbc.gridwidth = 2; gbc.fill = GridBagConstraints.NONE; gbc.anchor = GridBagConstraints.CENTER; gbl.setConstraints( useAntialiasCB, gbc ); this.add( useAntialiasCB ); gbc.gridwidth = GridBagConstraints.REMAINDER; gbl.setConstraints( useFractionalCB, gbc ); this.add( useFractionalCB ); gbc.weightx = 1; gbc.weighty = 1; gbc.insets = new Insets( 2, 0, 0, 2 ); gbc.fill = GridBagConstraints.BOTH; gbl.setConstraints( fp, gbc ); this.add( fp ); gbc.weighty = 0; gbc.insets = new Insets( 0, 2, 0, 0 ); gbl.setConstraints( statusBar, gbc ); this.add( statusBar ); } /// Adds a component to a container with a label to its left in GridBagLayout private void addLabeledComponentToGBL( String name, JComponent c, GridBagLayout gbl, GridBagConstraints gbc, Container target ) { LabelV2 l = new LabelV2( name ); GridBagConstraints gbcLabel = (GridBagConstraints) gbc.clone(); gbcLabel.insets = new Insets( 2, 2, 2, 0 ); gbcLabel.gridwidth = 1; gbcLabel.weightx = 0; if ( c == null ) c = new JLabel( "" ); gbl.setConstraints( l, gbcLabel ); target.add( l ); gbl.setConstraints( c, gbc ); target.add( c ); } /// Sets up menu entries private void setupMenu( boolean isApplet ) { JMenu fileMenu = new JMenu( "File" ); JMenu optionMenu = new JMenu( "Option" ); fileMenu.add( new MenuItemV2( "Save Selected Options...", this )); fileMenu.add( new MenuItemV2( "Load Options...", this )); fileMenu.addSeparator(); fileMenu.add( new MenuItemV2( "Save as JPEG...", this )); fileMenu.add( new MenuItemV2( "Load JPEG File to Compare...", this )); fileMenu.add( new MenuItemV2( "Page Setup...", this )); fileMenu.add( new MenuItemV2( "Print...", this )); fileMenu.addSeparator(); if ( !isApplet ) fileMenu.add( new MenuItemV2( "Exit", this )); else fileMenu.add( new MenuItemV2( "Close", this )); displayGridCBMI = new CheckboxMenuItemV2( "Display Grid", true, this ); force16ColsCBMI = new CheckboxMenuItemV2( "Force 16 Columns", false, this ); showFontInfoCBMI = new CheckboxMenuItemV2( "Display Font Info", false, this ); optionMenu.add( displayGridCBMI ); optionMenu.add( force16ColsCBMI ); optionMenu.add( showFontInfoCBMI ); JMenuBar mb = parent.getJMenuBar(); if ( mb == null ) mb = new JMenuBar(); mb.add( fileMenu ); mb.add( optionMenu ); parent.setJMenuBar( mb ); String fontList[] = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames(); for ( int i = 0; i < fontList.length; i++ ) fontMenu.addItem( fontList[i] ); fontMenu.setSelectedItem( "Dialog" ); styleMenu.addItem( "Plain" ); styleMenu.addItem( "Bold" ); styleMenu.addItem( "Italic" ); styleMenu.addItem( "Bold Italic" ); transformMenu.addItem( "None" ); transformMenu.addItem( "Scale" ); transformMenu.addItem( "Shear" ); transformMenu.addItem( "Rotate" ); transformMenuG2.addItem( "None" ); transformMenuG2.addItem( "Scale" ); transformMenuG2.addItem( "Shear" ); transformMenuG2.addItem( "Rotate" ); methodsMenu.addItem( "drawString" ); methodsMenu.addItem( "drawChars" ); methodsMenu.addItem( "drawBytes" ); methodsMenu.addItem( "drawGlyphVector" ); methodsMenu.addItem( "TextLayout.draw" ); methodsMenu.addItem( "GlyphVector.getOutline + draw" ); methodsMenu.addItem( "TextLayout.getOutline + draw" ); textMenu.addItem( "Unicode Range" ); textMenu.addItem( "All Glyphs" ); textMenu.addItem( "User Text" ); textMenu.addItem( "Text File" ); textMenu.addActionListener ( this ); // listener added later so unneeded events not thrown } /// Sets up the all dialogs used in Font2DTest... private void setupDialog( boolean isApplet ) { if (!isApplet) filePromptDialog = new JFileChooser( ); else filePromptDialog = null; /// Prepare user text dialog... userTextDialog = new JDialog( parent, "User Text", false ); JPanel dialogTopPanel = new JPanel(); JPanel dialogBottomPanel = new JPanel(); LabelV2 message1 = new LabelV2( "Enter text below and then press update" ); LabelV2 message2 = new LabelV2( "(Unicode char can be denoted by \\uXXXX)" ); userTextArea = new JTextArea( "Java2D!" ); ButtonV2 bUpdate = new ButtonV2( "Update", this ); userTextArea.setFont( new Font( "dialog", Font.PLAIN, 12 )); dialogTopPanel.setLayout( new GridLayout( 2, 1 )); dialogTopPanel.add( "North", message1 ); dialogTopPanel.add( "South", message2 ); dialogBottomPanel.add( bUpdate ); //ABP JScrollPane userTextAreaSP = new JScrollPane(userTextArea); userTextAreaSP.setPreferredSize(new Dimension(300, 100));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -