FileDocCategorySizeDatePackage
FontPanel.javaAPI DocSun JDK 1.4.2 Example49695Thu May 12 00:35:27 BST 2005None

FontPanel

public final class FontPanel extends JPanel implements AdjustmentListener
FontPanel.java
version
@(#)FontPanel.java 1.1 00/08/22
author
Shinsuke Fukuda
author
Ankit Patel [Conversion to Swing - 01/07/30]

Fields Summary
private final String[]
STYLES
private final int
NONE
private final int
SCALE
private final int
SHEAR
private final int
ROTATE
private final String[]
TRANSFORMS
private final int
DRAW_STRING
private final int
DRAW_CHARS
private final int
DRAW_BYTES
private final int
DRAW_GLYPHV
private final int
TL_DRAW
private final int
GV_OUTLINE
private final int
TL_OUTLINE
private final String[]
METHODS
public final int
RANGE_TEXT
public final int
ALL_GLYPHS
public final int
USER_TEXT
public final int
FILE_TEXT
private final String[]
MS_OPENING
private final String[]
MS_CLOSING
private final JScrollBar
verticalBar
private final FontCanvas
fc
private boolean
updateBackBuffer
private boolean
updateFontMetrics
private boolean
updateFont
private boolean
force16Cols
public boolean
showingError
private int
g2Transform
public final int
ONE_PAGE
public final int
CUR_RANGE
public final int
ALL_TEXT
private int
printMode
private PageFormat
page
private PrinterJob
printer
private String
fontName
private int
fontSize
private int
fontStyle
private int
fontTransform
private Font
testFont
private boolean
useAntialias
private boolean
useFractional
private int
drawMethod
private int
textToUse
private String[]
userText
private String[]
fileText
private int[]
drawRange
private String[]
fontInfos
private boolean
showGrid
private final Font2DTest
f2dt
private final JFrame
parent
Constructors Summary
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, fontSize );
        updateFontInfo();
    
Methods Summary
public voidadjustmentValueChanged(java.awt.event.AdjustmentEvent e)

        updateBackBuffer = true;
        fc.repaint();
    
public voiddoPageSetup()

        if ( printer == null ) {
            printer = PrinterJob.getPrinterJob();
            page = printer.defaultPage();
        }
        page = printer.pageDialog( page );
    
public voiddoPrint(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 );
            }
        }
    
public voiddoSaveJPEG(java.lang.String fileName)

        fc.writeJPEG( fileName );
    
private java.awt.geom.AffineTransformgetAffineTransform(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 java.lang.StringgetCurrentOptions()

        /// Create a new String to store the options
        /// The array will contain all 8 setting (font name, size...) and
        /// character range or user text data used (no file text data)
        int userTextSize = 0;
        String options;

        options = ( fontName + "\n" + fontSize  + "\n" + fontStyle + "\n" +
                    fontTransform + "\n"  + g2Transform + "\n"+ textToUse + "\n" + drawMethod + "\n" +
                    useAntialias + "\n" + useFractional + "\n");
        if ( textToUse == USER_TEXT )
          for ( int i = 0; i < userText.length; i++ )
            options += ( userText[i] + "\n" );

        return options;
    
public java.lang.String[]getFontInfo()

        return fontInfos;
    
public java.awt.DimensiongetPreferredSize()

        return new Dimension(600, 200);
    
public voidloadOptions(boolean grid, boolean force16, int start, int end, java.lang.String name, int size, int style, int transform, int g2transform, int text, int method, boolean aa, boolean fm, java.lang.String[] user)

        int range[] = { start, end };

        /// Since repaint call has a low priority, these functions will finish
        /// before the actual repainting is done
        setGridDisplay( grid );
        setForce16Columns( force16 );
	// previous call to readTextFile has already set the text to draw
	if (textToUse != FILE_TEXT) {
	  setTextToDraw( text, range, user, null );
	}
        setFontParams( name, size, style, transform );
        setTransformG2( g2transform ); // ABP
        setDrawMethod( method );
        setRenderingHints( aa, fm );
    
public voidpaintComponent(java.awt.Graphics g)

        // Windows does not repaint correctly, after
        // a zoom. Thus, we need to force the canvas
        // to repaint, but only once. After the first repaint,
        // everything stabilizes. [ABP]
        fc.repaint();
    
public voidsetDrawMethod(int i)

        drawMethod = i;
        updateBackBuffer = true;
        fc.repaint();
    
public voidsetFontParams(java.lang.Object obj, int size, int style, int transform)

        setFontParams( (String)obj, size, style, transform );
    
public voidsetFontParams(java.lang.String name, int 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, 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 voidsetForce16Columns(boolean b)

        force16Cols = b;
        updateBackBuffer = true;
        updateFontMetrics = true;
        fc.repaint();
    
public voidsetGridDisplay(boolean b)

        showGrid = b;
        updateBackBuffer = true;
        fc.repaint();
    
public voidsetRenderingHints(boolean aa, boolean fm)

        useAntialias = aa;
        useFractional = fm;
        updateBackBuffer = true;
        updateFontMetrics = true;
        fc.repaint();
    
public voidsetTextToDraw(int i, int[] range, java.lang.String[] textSet, java.lang.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 voidsetTransformG2(int transform)

        g2Transform = transform;
        updateBackBuffer = true;
        updateFontMetrics = true;
        fc.repaint();
    
private voidupdateFontInfo()

        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 ) {
            for ( int i = drawRange[0]; i < drawRange[1]; i++ )
              if ( testFont.canDisplay( (char) i ))
                numGlyphs++;
            fontInfos[1] = fontInfos[1] + numGlyphs + " / " + numCharsInRange;
        }
        else
          fontInfos[1] = null;