/*============================================================== * BASE_CONVERTER.java * © 1999 Midnight Oil Music-All Rights Reserved * * Last Modified: * * This program converts numbers from one base to another, * e.g. decimal to binary, hex to decimal etc. * *=============================================================*/ import java.lang.*; import java.awt.*; import java.applet.Applet; //import java.awt.event.*; public class BaseConverterApplet extends Applet { int num; Font theFont; String outStr; String inStr; TextField dataEntry; TextField outputField; FontMetrics fMetrics; boolean fontsSet = false; public void init() { this.setLayout( null ); // Don't use a layout manager Label l = new Label( "The Results" ); l.reshape( 201, 90, 100, 20 ); this.add( l ); // Add a text field for the output outputField = new TextField( "" ); outputField.setEditable( false ); // You can't type in here outputField.reshape( 126, 110, 250, 20 ); this.add( outputField ); l = new Label( "Enter Number:" ); l.reshape( 10, 160, 110, 20 ); this.add( l ); // Add a text field for data entry dataEntry = new TextField( 35 ); dataEntry.reshape( 126, 160, 300, 20 ); this.add( dataEntry ); dataEntry.requestFocus(); // Set cursor to blinking l = new Label( "Select Operation" ); l.reshape( 201, 200, 110, 20 ); this.add( l ); // Add the function buttons Button b = new Button( "Decimal to Bin" ); b.reshape( 12, 230, 110, 20 ); this.add( b ); b = new Button( "Bin to Decimal" ); b.reshape( 134, 230, 110, 20 ); this.add( b ); b = new Button( "Decimal to Hex" ); b.reshape( 256, 230, 110, 20 ); this.add( b ); b = new Button( "Hex to Decimal" ); b.reshape( 378, 230, 110, 20 ); this.add( b ); //repaint(); } // end init() /*------------------------------------------------------------------------ * setFonts * *-----------------------------------------------------------------------*/ private void setFonts( Graphics g ) { if ( fontsSet ) return; theFont = new Font( "Times", Font.BOLD, 36 ); fMetrics = g.getFontMetrics( theFont ); fontsSet = true; } // end setFonts() /*------------------------------------------------------------------------ * paint * *-----------------------------------------------------------------------*/ public void paint( Graphics g ) { setFonts( g ); String title = "Bob's Base Converter"; int cx = 50; int cy = 50; g.setFont( theFont ); g.drawString( title, cx, cy ); } /*------------------------------------------------------------------------ * action * Respond to button clicks *-----------------------------------------------------------------------*/ public boolean action( Event evt, Object arg ) { if ( arg.equals( "Decimal to Bin" ) ) { inStr = dataEntry.getText(); // Read the text field // Convert the text to a decimal integer // and then to a binary string. parseInt throws a // NumberFormat Exception so this is in a try catch block try { num = Integer.parseInt( inStr, 10 ); if ( num >= Integer.MIN_VALUE && num <= Integer.MAX_VALUE ) outStr = Integer.toBinaryString( num ); else outStr = "Value out of range!"; } catch( NumberFormatException e ) // If the number is not in the specified format { // catch the exception outStr = "Invalid Entry!"; } outputField.setText( "" ); // Clear the output text field outputField.setText( outStr ); // Show the results dataEntry.setText( "" ); // Clear the entry text field dataEntry.requestFocus(); // Set the cursor to flashing } else if ( arg.equals( "Bin to Decimal" ) ) { inStr = dataEntry.getText(); try { num = Integer.parseInt( inStr, 2 ); if ( num >= Integer.MIN_VALUE && num <= Integer.MAX_VALUE ) outStr = Integer.toString( num ); else outStr = "Value out of range!"; } catch( NumberFormatException e ) { outStr = "Invalid Entry!"; } outputField.setText( "" ); outputField.setText( outStr ); dataEntry.setText( "" ); dataEntry.requestFocus(); } else if ( arg.equals( "Decimal to Hex" ) ) { inStr = dataEntry.getText(); try { num = Integer.parseInt( inStr, 10 ); if ( num >= Integer.MIN_VALUE && num <= Integer.MAX_VALUE ) outStr = Integer.toHexString( num ); else outStr = "Value out of range!"; } catch( NumberFormatException e ) { outStr = "Invalid Entry!"; } outputField.setText( "" ); outputField.setText( outStr ); dataEntry.setText( "" ); dataEntry.requestFocus(); } else { inStr = dataEntry.getText(); try { num = Integer.parseInt( inStr, 16 ); if ( num >= Integer.MIN_VALUE && num <= Integer.MAX_VALUE ) outStr = Integer.toString( num ); else outStr = "Value out of range!"; } catch( NumberFormatException e ) { outStr = "Invalid Entry!"; } outputField.setText( "" ); outputField.setText( outStr ); dataEntry.setText( "" ); dataEntry.requestFocus(); } return true; } // end action() } // end BaseConverterApplet class