?? font2dtest.java
字號(hào):
printDialog.pack(); /// Prepare font information dialog... fontInfoDialog = new JDialog( parent, "Font info", false ); fontInfoDialog.setResizable( false ); fontInfoDialog.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e ) { fontInfoDialog.hide(); showFontInfoCBMI.setState( false ); } }); JPanel fontInfoPanel = new JPanel(); fontInfoPanel.setLayout( new GridLayout( fontInfos.length, 1 )); for ( int i = 0; i < fontInfos.length; i++ ) { fontInfos[i] = new LabelV2(""); fontInfoPanel.add( fontInfos[i] ); } fontInfoDialog.getContentPane().add( fontInfoPanel ); /// Move the location of the dialog... userTextDialog.setLocation( 200, 300 ); fontInfoDialog.setLocation( 0, 400 ); } /// RangeMenu object signals using this function /// when Unicode range has been changed and text needs to be redrawn public void fireRangeChanged() { int range[] = rm.getSelectedRange(); fp.setTextToDraw( fp.RANGE_TEXT, range, null, null ); if(canDisplayCheck) { setupFontList(range[0], range[1]); } if ( showFontInfoCBMI.getState() ) fireUpdateFontInfo(); } /// Changes the message on the status bar public void fireChangeStatus( String message, boolean error ) { /// If this is not ran as an applet, use own status bar, /// Otherwise, use the appletviewer/browser's status bar statusBar.setText( message ); if ( error ) fp.showingError = true; else fp.showingError = false; } /// Updates the information about the selected font public void fireUpdateFontInfo() { if ( showFontInfoCBMI.getState() ) { String infos[] = fp.getFontInfo(); for ( int i = 0; i < fontInfos.length; i++ ) fontInfos[i].setText( infos[i] ); fontInfoDialog.pack(); } } private void setupFontList(int rangeStart, int rangeEnd) { int listCount = fontMenu.getItemCount(); int size = 16; try { size = Float.valueOf(sizeField.getText()).intValue(); } catch ( Exception e ) { System.out.println("Invalid font size in the size textField. Using default value of 16"); } int style = fontStyles[styleMenu.getSelectedIndex()]; Font f; for (int i = 0; i < listCount; i++) { String fontName = (String)fontMenu.getItemAt(i); f = new Font(fontName, style, size); if ((rm.getSelectedIndex() != RangeMenu.SURROGATES_AREA_INDEX) && canDisplayRange(f, rangeStart, rangeEnd)) { fontMenu.setBit(i, true); } else { fontMenu.setBit(i, false); } } fontMenu.repaint(); } protected boolean canDisplayRange(Font font, int rangeStart, int rangeEnd) { for (int i = rangeStart; i < rangeEnd; i++) { if (font.canDisplay(i)) { return true; } } return false; } /// Displays a file load/save dialog and returns the specified file private String promptFile( boolean isSave, String initFileName ) { int retVal; String str; /// ABP if ( filePromptDialog == null) return null; if ( isSave ) { filePromptDialog.setDialogType( JFileChooser.SAVE_DIALOG ); filePromptDialog.setDialogTitle( "Save..." ); str = "Save"; } else { filePromptDialog.setDialogType( JFileChooser.OPEN_DIALOG ); filePromptDialog.setDialogTitle( "Load..." ); str = "Load"; } if (initFileName != null) filePromptDialog.setSelectedFile( new File( initFileName ) ); retVal = filePromptDialog.showDialog( this, str ); if ( retVal == JFileChooser.APPROVE_OPTION ) { File file = filePromptDialog.getSelectedFile(); String fileName = file.getAbsolutePath(); if ( fileName != null ) { return fileName; } } return null; } /// Converts user text into arrays of String, delimited at newline character /// Also replaces any valid escape sequence with appropriate unicode character /// Support \\UXXXXXX notation for surrogates private String[] parseUserText( String orig ) { int length = orig.length(); StringTokenizer perLine = new StringTokenizer( orig, "\n" ); String textLines[] = new String[ perLine.countTokens() ]; int lineNumber = 0; while ( perLine.hasMoreElements() ) { StringBuffer converted = new StringBuffer(); String oneLine = perLine.nextToken(); int lineLength = oneLine.length(); int prevEscapeEnd = 0; int nextEscape = -1; do { int nextBMPEscape = oneLine.indexOf( "\\u", prevEscapeEnd ); int nextSupEscape = oneLine.indexOf( "\\U", prevEscapeEnd ); nextEscape = (nextBMPEscape < 0) ? ((nextSupEscape < 0) ? -1 : nextSupEscape) : ((nextSupEscape < 0) ? nextBMPEscape : Math.min(nextBMPEscape, nextSupEscape)); if ( nextEscape != -1 ) { if ( prevEscapeEnd < nextEscape ) converted.append( oneLine.substring( prevEscapeEnd, nextEscape )); prevEscapeEnd = nextEscape + (nextEscape == nextBMPEscape ? 6 : 8); try { String hex = oneLine.substring( nextEscape + 2, prevEscapeEnd ); if (nextEscape == nextBMPEscape) { converted.append( (char) Integer.parseInt( hex, 16 )); } else { converted.append( new String( Character.toChars( Integer.parseInt( hex, 16 )))); } } catch ( Exception e ) { int copyLimit = Math.min(lineLength, prevEscapeEnd); converted.append( oneLine.substring( nextEscape, copyLimit )); } } } while (nextEscape != -1); if ( prevEscapeEnd < lineLength ) converted.append( oneLine.substring( prevEscapeEnd, lineLength )); textLines[ lineNumber++ ] = converted.toString(); } return textLines; } /// Reads the text from specified file, detecting UTF-16 encoding /// Then breaks the text into String array, delimited at every line break private void readTextFile( String fileName ) { try { String fileText, textLines[]; BufferedInputStream bis = new BufferedInputStream( new FileInputStream( fileName )); int numBytes = bis.available(); if (numBytes == 0) { throw new Exception("Text file " + fileName + " is empty"); } byte byteData[] = new byte[ numBytes ]; bis.read( byteData, 0, numBytes ); bis.close(); /// If byte mark is found, then use UTF-16 encoding to convert bytes... if (numBytes >= 2 && (( byteData[0] == (byte) 0xFF && byteData[1] == (byte) 0xFE ) || ( byteData[0] == (byte) 0xFE && byteData[1] == (byte) 0xFF ))) fileText = new String( byteData, "UTF-16" ); /// Otherwise, use system default encoding else fileText = new String( byteData ); int length = fileText.length(); StringTokenizer perLine = new StringTokenizer( fileText, "\n" ); /// Determine "Return Char" used in this file /// This simply finds first occurrence of CR, CR+LF or LF... for ( int i = 0; i < length; i++ ) { char iTh = fileText.charAt( i ); if ( iTh == '\r' ) { if ( i < length - 1 && fileText.charAt( i + 1 ) == '\n' ) perLine = new StringTokenizer( fileText, "\r\n" ); else perLine = new StringTokenizer( fileText, "\r" ); break; } else if ( iTh == '\n' ) /// Use the one already created break; } int lineNumber = 0, numLines = perLine.countTokens(); textLines = new String[ numLines ]; while ( perLine.hasMoreElements() ) { String oneLine = perLine.nextToken(); if ( oneLine == null ) /// To make LineBreakMeasurer to return a valid TextLayout /// on an empty line, simply feed it a space char... oneLine = " "; textLines[ lineNumber++ ] = oneLine; } fp.setTextToDraw( fp.FILE_TEXT, null, null, textLines ); rm.setEnabled( false ); methodsMenu.setEnabled( false ); } catch ( Exception ex ) { fireChangeStatus( "ERROR: Failed to Read Text File; See Stack Trace", true ); ex.printStackTrace(); } } /// Returns a String storing current configuration private void writeCurrentOptions( String fileName ) { try { String curOptions = fp.getCurrentOptions(); BufferedOutputStream bos = new BufferedOutputStream( new FileOutputStream( fileName )); /// Prepend title and the option that is only obtainable here int range[] = rm.getSelectedRange(); String completeOptions = ( "Font2DTest Option File\n" + displayGridCBMI.getState() + "\n" + force16ColsCBMI.getState() + "\n" + showFontInfoCBMI.getState() + "\n" + rm.getSelectedItem() + "\n" + range[0] + "\n" + range[1] + "\n" + curOptions + tFileName); byte toBeWritten[] = completeOptions.getBytes( "UTF-16" ); bos.write( toBeWritten, 0, toBeWritten.length ); bos.close(); } catch ( Exception ex ) { fireChangeStatus( "ERROR: Failed to Save Options File; See Stack Trace", true ); ex.printStackTrace(); } } /// Updates GUI visibility/status after some parameters have changed private void updateGUI() { int selectedText = textMenu.getSelectedIndex(); /// Set the visibility of User Text dialog if ( selectedText == fp.USER_TEXT ) userTextDialog.show(); else userTextDialog.hide(); /// Change the visibility/status/availability of Print JDialog buttons printModeCBs[ fp.ONE_PAGE ].setSelected( true ); if ( selectedText == fp.FILE_TEXT || selectedText == fp.USER_TEXT ) { /// ABP /// update methodsMenu to show that TextLayout.draw is being used /// when we are in FILE_TEXT mode if ( selectedText == fp.FILE_TEXT ) methodsMenu.setSelectedItem("TextLayout.draw"); methodsMenu.setEnabled( selectedText == fp.USER_TEXT ); printModeCBs[ fp.CUR_RANGE ].setEnabled( false ); printModeCBs[ fp.ALL_TEXT ].setEnabled( true ); } else { /// ABP /// update methodsMenu to show that drawGlyph is being used /// when we are in ALL_GLYPHS mode if ( selectedText == fp.ALL_GLYPHS ) methodsMenu.setSelectedItem("drawGlyphVector"); methodsMenu.setEnabled( selectedText == fp.RANGE_TEXT ); printModeCBs[ fp.CUR_RANGE ].setEnabled( true ); printModeCBs[ fp.ALL_TEXT ].setEnabled( false ); } /// Modify RangeMenu and fontInfo label availabilty if ( selectedText == fp.RANGE_TEXT ) { fontInfos[1].setVisible( true ); rm.setEnabled( true ); } else { fontInfos[1].setVisible( false ); rm.setEnabled( false ); } } /// Loads saved options and applies them private void loadOptions( String fileName ) { try { BufferedInputStream bis = new BufferedInputStream( new FileInputStream( fileName )); int numBytes = bis.available(); byte byteData[] = new byte[ numBytes ]; bis.read( byteData, 0, numBytes ); bis.close(); if ( numBytes < 2 || (byteData[0] != (byte) 0xFE || byteData[1] != (byte) 0xFF) ) throw new Exception( "Not a Font2DTest options file" ); String options = new String( byteData, "UTF-16" ); StringTokenizer perLine = new StringTokenizer( options, "\n" ); String title = perLine.nextToken(); if ( !title.equals( "Font2DTest Option File" )) throw new Exception( "Not a Font2DTest options file" ); /// Parse all options boolean displayGridOpt = Boolean.parseBoolean( perLine.nextToken() ); boolean force16ColsOpt = Boolean.parseBoolean( perLine.nextToken() ); boolean showFontInfoOpt = Boolean.parseBoolean( perLine.nextToken() ); String rangeNameOpt = perLine.nextToken(); int rangeStartOpt = Integer.parseInt( perLine.nextToken() ); int rangeEndOpt = Integer.parseInt( perLine.nextToken() ); String fontNameOpt = perLine.nextToken(); float fontSizeOpt = Float.parseFloat( perLine.nextToken() ); int fontStyleOpt = Integer.parseInt( perLine.nextToken() ); int fontTransformOpt = Integer.parseInt( perLine.nextToken() ); int g2TransformOpt = Integer.parseInt( perLine.nextToken() ); int textToUseOpt = Integer.parseInt( perLine.nextToken() ); int drawMethodOpt = Integer.parseInt( perLine.nextToken() ); int antialiasOpt = Integer.parseInt(perLine.nextToken()); int fractionalOpt = Integer.parseInt(perLine.nextToken()); int lcdContrast = Integer.parseInt(perLine.nextToken()); String userTextOpt[] = { "Java2D!" }, dialogEntry = "Java2D!"; if (textToUseOpt == fp.USER_TEXT ) { int numLines = perLine.countTokens(), lineNumber = 0; if ( numLines != 0 ) { userTextOpt = new String[ numLines ]; dialogEntry = ""; for ( ; perLine.hasMoreElements(); lineNumber++ ) { userTextOpt[ lineNumber ] = perLine.nextToken(); dialogEntry += userTextOpt[ lineNumber ] + "\n"; } } } /// Reset GUIs displayGridCBMI.setState( displayGridOpt ); force16ColsCBMI.setState( force16ColsOpt ); showFontInfoCBMI.setState( showFontInfoOpt ); rm.setSelectedRange( rangeNameOpt, rangeStartOpt, rangeEndOpt ); fontMenu.setSelectedItem( fontNameOpt ); sizeField.setText( String.valueOf( fontSizeOpt )); styleMenu.setSelectedIndex( fontStyleOpt ); transformMenu.setSelectedIndex( fontTransformOpt ); transformMenuG2.setSelectedIndex( g2TransformOpt ); textMenu.setSelectedIndex( textToUseOpt ); methodsMenu.setSelectedIndex( drawMethodOpt ); antiAliasMenu.setSelectedIndex( antialiasOpt ); fracMetricsMenu.setSelectedIndex( fractionalOpt ); contrastSlider.setValue(lcdContrast); userTextArea.setText( dialogEntry ); updateGUI(); if ( textToUseOpt == fp.FILE_TEXT ) { tFileName = perLine.nextToken(); readTextFile(tFileName ); } /// Reset option variables and repaint fp.loadOptions( displayGridOpt, force16ColsOpt, rangeStartOpt, rangeEndOpt, fontNameOpt, fontSizeOpt, fontStyleOpt, fontTransformOpt, g2TransformOpt, textToUseOpt, drawMethodOpt, antialiasOpt, fractionalOpt, lcdContrast, userTextOpt );
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -