📄 hexviewer.java
字号:
label.setBorder( new EmptyBorder( 0, 2, 0, 2 ) ); AWTUtilities.constrain( ctlPanel, label, GridBagConstraints.NONE, GridBagConstraints.CENTER, ctlCol++, ctlRow, 1, 1, 0.0, 0.0 ); this.blkHexField = new JTextField(); AWTUtilities.constrain( ctlPanel, this.blkHexField, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, ctlCol++, ctlRow, 1, 1, 0.2, 0.0 ); this.blkDecField = new JTextField(); AWTUtilities.constrain( ctlPanel, this.blkDecField, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, ctlCol++, ctlRow, 1, 1, 0.25, 0.0 ); label = new JLabel( "Offset:" ); label.setHorizontalAlignment( JLabel.RIGHT ); label.setBorder( new EmptyBorder( 0, 8, 0, 2 ) ); AWTUtilities.constrain( ctlPanel, label, GridBagConstraints.NONE, GridBagConstraints.CENTER, ctlCol++, ctlRow, 1, 1, 0.0, 0.0 ); this.offHexField = new JTextField(); AWTUtilities.constrain( ctlPanel, this.offHexField, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, ctlCol++, ctlRow, 1, 1, 0.2, 0.0 ); this.offDecField = new JTextField(); AWTUtilities.constrain( ctlPanel, this.offDecField, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, ctlCol++, ctlRow++, 1, 1, 0.35, 0.0 ); JPanel dataPanel = new JPanel(); dataPanel.setLayout( new GridBagLayout() ); dataPanel.setBorder( new EmptyBorder( 2, 2, 2, 2 ) ); AWTUtilities.constrain( this, dataPanel, GridBagConstraints.BOTH, GridBagConstraints.CENTER, 0, row++, 1, 1, 1.0, 1.0 ); this.scrollBar = new JScrollBar( JScrollBar.HORIZONTAL, 0, 1, 0, 1 ); this.scrollBar.setBlockIncrement( HexViewer.PAGEINCR ); this.scrollBar.getModel().addChangeListener( new IChangeListener() ); AWTUtilities.constrain( dataPanel, this.scrollBar, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 0, 0, 1, 1, 1.0, 0.0 ); this.hexCanvas = new HexCanvas(); this.hexCanvas.setOpaque( true ); this.hexCanvas.setBackground( Color.white ); this.hexCanvas.setBorder( new EmptyBorder( 0, 8, 0, 2 ) ); this.hexCanvas.setFont( new Font( "Monospaced", Font.PLAIN, 12 ) ); AWTUtilities.constrain( dataPanel, this.hexCanvas, GridBagConstraints.BOTH, GridBagConstraints.CENTER, 0, 1, 1, 1, 1.0, 1.0 ); } private void resetCursor() { Frame f = (Frame)getTopLevelAncestor(); if ( f != null && this.saveCursor != null ) f.setCursor( this.saveCursor ); this.saveCursor = null; } private void setWaitCursor() { Frame f = (Frame)getTopLevelAncestor(); if ( f != null ) { this.saveCursor = f.getCursor(); f.setCursor( Cursor.getPredefinedCursor( Cursor.WAIT_CURSOR ) ); } }//............................................................// INNER CLASSES private class HexCanvas extends JComponent { private int hexHeight; private int hexByteW; private int hexQuadW; private int hexDataW; private int hexSepW; private int hexCharW; private int hexLineW; private int spaceW; private int offsetW; private Dimension mDim; private Dimension pDim; private boolean displayEOF; private byte[] data; private HexNumberFormat format; public HexCanvas() { super(); this.data = null; this.displayEOF = false; this.mDim = new Dimension( 20, 20 ); this.pDim = new Dimension( 400, 400 ); this.format = new HexNumberFormat( "XX" ); } public boolean isFocusTraversable() { return false; } public void displayEOF() { this.data = null; this.displayEOF = true; repaint( 500 ); } public void displayData( byte[] data ) { this.data = data; this.displayEOF = false; repaint( 500 ); } public void update( Graphics updateG ) { paint( updateG ); } public synchronized void paint( Graphics g ) { int i, j; int top, left; int width, height; int x, y; Dimension sz = getSize(); if ( this.isOpaque() ) { g.setColor( getBackground() ); g.fillRect( 0, 0, sz.width, sz.height ); } g.setColor( getForeground() ); Font fHex = getFont(); Font fTitle = getFont(); x = 1; y = this.hexHeight + 1; if ( this.displayEOF ) { // UNDONE This is HORRIBLY inefficient! g.setFont( new Font( "Serif", Font.BOLD, 18 ) ); FontMetrics fm = g.getFontMetrics( g.getFont() ); String msg = "End Of Data"; int sW = fm.stringWidth( msg ); x = (sz.width - sW) / 2; y = ((sz.height - fm.getHeight()) / 2) + fm.getHeight(); g.drawString( msg, x, y ); return; } else if ( this.data == null ) { return; } int cnt = 0; g.setFont( fHex ); for ( i = 0 ; i < HexViewer.HEXLINES && cnt < this.data.length ; ++i ) { StringBuffer buf = new StringBuffer(); StringBuffer chBuf = new StringBuffer(); FieldPosition pos = new FieldPosition(0); this.format.format( new Integer(i * HexViewer.HEXBYTES), buf, pos ); buf.append( ": " ); for ( j = 0 ; j < HexViewer.HEXBYTES && cnt < this.data.length ; ++j, ++cnt ) { int index = (i * HexViewer.HEXBYTES) + j; if ( index >= this.data.length ) break; char ch = (char)this.data[ index ]; chBuf.append( (ch >= 32 && ch < 127) ? ch : '.' ); this.format.format( new Integer( this.data[index] ), buf, pos ); buf.append( " " ); } for ( ; j < HexViewer.HEXBYTES ; ++j ) { buf.append( " " ); } buf.append( " " ); buf.append( chBuf ); g.drawString( buf.toString(), x, y ); y += this.hexHeight; } } public void addNotify() { super.addNotify(); computeDimensions(); } public void setFont( Font f ) { super.setFont( f ); Graphics g = getGraphics(); if ( g != null ) { establishFontMetrics( g, f ); } } private void establishFontMetrics( Graphics g, Font f ) { FontMetrics fm = g.getFontMetrics( getFont() ); this.hexHeight = fm.getLeading() + fm.getAscent(); this.spaceW = fm.stringWidth( " " ); this.offsetW = fm.stringWidth( "88: " ); this.hexByteW = fm.stringWidth( "88" ); this.hexQuadW = (4 * hexByteW) + (3 * this.spaceW); this.hexDataW = (4 * hexQuadW); this.hexSepW = fm.stringWidth( " " ); this.hexCharW = fm.stringWidth( "0123456789ABCDEF" ); this.hexLineW = this.offsetW + this.hexDataW + this.hexSepW + this.hexCharW; } public void computeDimensions() { int width, height; Dimension sz = getSize(); Graphics g = getGraphics(); establishFontMetrics( g, getFont() ); width = this.hexLineW + 2; height = (HexViewer.HEXLINES * this.hexHeight) + 2; this.mDim.width = width; this.mDim.height = height; this.pDim.width = mDim.width; this.pDim.height = mDim.height; } public Dimension getPreferredSize() { return pDim; } public Dimension getMinimumSize() { return pDim; } }//............................................................ private class IChangeListener implements ChangeListener { public void stateChanged( ChangeEvent event ) { int value = scrollBar.getValue(); setCurrentBlock( value * BLOCKSIZE ); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -