📄 fontpanel.java
字号:
/* * @(#)FontPanel.java 1.20 05/11/17 * * Copyright (c) 2006 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: * * -Redistribution of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * * -Redistribution in binary form must reproduce 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 MIDROSYSTEMS, INC. ("SUN") * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS 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 THIS SOFTWARE, * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. * * You acknowledge that this software is not designed, licensed or intended * for use in the design, construction, operation or maintenance of any * nuclear facility. *//* * @(#)FontPanel.java 1.20 05/11/17 */import java.awt.BorderLayout;import java.awt.Color;import java.awt.Cursor;import java.awt.Dimension;import java.awt.Font;import java.awt.FontMetrics;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.GraphicsConfiguration;import java.awt.GraphicsEnvironment;import java.awt.Point;import java.awt.Rectangle;import java.awt.RenderingHints;import java.awt.Toolkit;import java.awt.event.AdjustmentEvent;import java.awt.event.AdjustmentListener;import java.awt.event.ComponentAdapter;import java.awt.event.ComponentEvent;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.awt.event.MouseMotionListener;import java.awt.font.FontRenderContext;import java.awt.font.GlyphVector;import java.awt.font.LineBreakMeasurer;import java.awt.font.TextLayout;import java.awt.geom.AffineTransform;import java.awt.geom.NoninvertibleTransformException;import java.awt.geom.Rectangle2D;import java.awt.image.BufferedImage;import java.awt.print.PageFormat;import java.awt.print.Printable;import java.awt.print.PrinterJob;import java.io.BufferedOutputStream;import java.io.FileOutputStream;import java.text.AttributedString;import java.util.EnumSet;import java.util.Vector;import javax.imageio.*;import javax.swing.*;import static java.awt.RenderingHints.*;/** * FontPanel.java * * @version @(#)FontPanel.java 1.1 00/08/22 * @author Shinsuke Fukuda * @author Ankit Patel [Conversion to Swing - 01/07/30] *//// This panel is combination of the text drawing area of Font2DTest/// and the custom controlled scroll barpublic final class FontPanel extends JPanel implements AdjustmentListener { /// Drawing Option Constants private final String STYLES[] = { "plain", "bold", "italic", "bold italic" }; private final int NONE = 0; private final int SCALE = 1; private final int SHEAR = 2; private final int ROTATE = 3; private final String TRANSFORMS[] = { "with no transforms", "with scaling", "with Shearing", "with rotation" }; private final int DRAW_STRING = 0; private final int DRAW_CHARS = 1; private final int DRAW_BYTES = 2; private final int DRAW_GLYPHV = 3; private final int TL_DRAW = 4; private final int GV_OUTLINE = 5; private final int TL_OUTLINE = 6; private final String METHODS[] = { "drawString", "drawChars", "drawBytes", "drawGlyphVector", "TextLayout.draw", "GlyphVector.getOutline", "TextLayout.getOutline" }; public final int RANGE_TEXT = 0; public final int ALL_GLYPHS = 1; public final int USER_TEXT = 2; public final int FILE_TEXT = 3; private final String MS_OPENING[] = { " Unicode ", " Glyph Code ", " lines ", " lines " }; private final String MS_CLOSING[] = { "", "", " of User Text ", " of LineBreakMeasurer-reformatted Text " }; /// General Graphics Variable private final JScrollBar verticalBar; private final FontCanvas fc; private boolean updateBackBuffer = true; private boolean updateFontMetrics = true; private boolean updateFont = true; private boolean force16Cols = false; public boolean showingError = false; private int g2Transform = NONE; /// ABP /// Printing constants and variables public final int ONE_PAGE = 0; public final int CUR_RANGE = 1; public final int ALL_TEXT = 2; private int printMode = ONE_PAGE; private PageFormat page = null; private PrinterJob printer = null; /// Text drawing variables private String fontName = "Dialog"; private float fontSize = 12; private int fontStyle = Font.PLAIN; private int fontTransform = NONE; private Font testFont = null; private Object antiAliasType = VALUE_TEXT_ANTIALIAS_DEFAULT; private Object fractionalMetricsType = VALUE_FRACTIONALMETRICS_DEFAULT; private Object lcdContrast = getDefaultLCDContrast(); private int drawMethod = DRAW_STRING; private int textToUse = RANGE_TEXT; private String userText[] = null; private String fileText[] = null; private int drawRange[] = { 0x0000, 0x007f }; private String fontInfos[] = new String[2]; private boolean showGrid = true; /// Parent Font2DTest panel private final Font2DTest f2dt; private final JFrame parent; public FontPanel( Font2DTest demo, JFrame f ) { f2dt = demo; parent = f; verticalBar = new JScrollBar ( JScrollBar.VERTICAL ); fc = new FontCanvas(); this.setLayout( new BorderLayout() ); this.add( "Center", fc ); this.add( "East", verticalBar ); verticalBar.addAdjustmentListener( this ); this.addComponentListener( new ComponentAdapter() { public void componentResized( ComponentEvent e ) { updateBackBuffer = true; updateFontMetrics = true; } }); /// Initialize font and its infos testFont = new Font(fontName, fontStyle, (int)fontSize); if ((float)((int)fontSize) != fontSize) { testFont = testFont.deriveFont(fontSize); } updateFontInfo(); } public Dimension getPreferredSize() { return new Dimension(600, 200); } /// Functions called by the main programs to set the various parameters public void setTransformG2( int transform ) { g2Transform = transform; updateBackBuffer = true; updateFontMetrics = true; fc.repaint(); } /// convenience fcn to create AffineTransform of appropriate type private AffineTransform getAffineTransform( int transform ) { /// ABP AffineTransform at = new AffineTransform(); switch ( transform ) { case SCALE: at.setToScale( 1.5f, 1.5f ); break; case ROTATE: at.setToRotation( Math.PI / 6 ); break; case SHEAR: at.setToShear( 0.4f, 0 ); break; case NONE: break; default: //System.err.println( "Illegal G2 Transform Arg: " + transform); break; } return at; } public void setFontParams(Object obj, float size, int style, int transform) { setFontParams( (String)obj, size, style, transform ); } public void setFontParams(String name, float size, int style, int transform) { boolean fontModified = false; if ( !name.equals( fontName ) || style != fontStyle ) fontModified = true; fontName = name; fontSize = size; fontStyle = style; fontTransform = transform; /// Recreate the font as specified testFont = new Font(fontName, fontStyle, (int)fontSize); if ((float)((int)fontSize) != fontSize) { testFont = testFont.deriveFont(fontSize); } if ( fontTransform != NONE ) { AffineTransform at = getAffineTransform( fontTransform ); testFont = testFont.deriveFont( at ); } updateBackBuffer = true; updateFontMetrics = true; fc.repaint(); if ( fontModified ) { /// Tell main panel to update the font info updateFontInfo(); f2dt.fireUpdateFontInfo(); } } public void setRenderingHints( Object aa, Object fm, Object contrast) { antiAliasType = ((AAValues)aa).getHint(); fractionalMetricsType = ((FMValues)fm).getHint(); lcdContrast = contrast; updateBackBuffer = true; updateFontMetrics = true; fc.repaint(); } public void setDrawMethod( int i ) { drawMethod = i; updateBackBuffer = true; fc.repaint(); } public void setTextToDraw( int i, int range[], String textSet[], String fileData[] ) { textToUse = i; if ( textToUse == RANGE_TEXT ) drawRange = range; else if ( textToUse == ALL_GLYPHS ) drawMethod = DRAW_GLYPHV; else if ( textToUse == USER_TEXT ) userText = textSet; else if ( textToUse == FILE_TEXT ) { fileText = fileData; drawMethod = TL_DRAW; } updateBackBuffer = true; updateFontMetrics = true; fc.repaint(); updateFontInfo(); } public void setGridDisplay( boolean b ) { showGrid = b; updateBackBuffer = true; fc.repaint(); } public void setForce16Columns( boolean b ) { force16Cols = b; updateBackBuffer = true; updateFontMetrics = true; fc.repaint(); } /// Prints out the text display area public void doPrint( int i ) { if ( printer == null ) { printer = PrinterJob.getPrinterJob(); page = printer.defaultPage(); } printMode = i; printer.setPrintable( fc, page ); if ( printer.printDialog() ) { try { printer.print(); } catch ( Exception e ) { f2dt.fireChangeStatus( "ERROR: Printing Failed; See Stack Trace", true ); } } } /// Displays the page setup dialog and updates PageFormat info public void doPageSetup() { if ( printer == null ) { printer = PrinterJob.getPrinterJob(); page = printer.defaultPage(); } page = printer.pageDialog( page ); } /// Obtains the information about selected font private void updateFontInfo() { int numGlyphs = 0, numCharsInRange = drawRange[1] - drawRange[0] + 1; fontInfos[0] = "Font Face Name: " + testFont.getFontName(); fontInfos[1] = "Glyphs in This Range: "; if ( textToUse == RANGE_TEXT ) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -