// Fig. 8.18: StaticCharMethods2.java // Demonstrates the static character conversion methods // of class Character from the java.lang package. import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class StaticCharMethods2 extends Applet implements ActionListener { char c; int digit, radix; boolean charToDigit; Label prompt1, prompt2; TextField input, radixField; Button toChar, toInt; public void init() { c = 'A'; radix = 16; charToDigit = true; prompt1 = new Label( "Enter a digit or character " ); input = new TextField( "A", 5 ); prompt2 = new Label( "Enter a radix " ); radixField = new TextField( "16", 5 ); toChar = new Button( "Convert digit to character" ); toChar.addActionListener( this ); toInt = new Button( "Convert character to digit" ); toInt.addActionListener( this ); add( prompt1 ); add( input ); add( prompt2 ); add( radixField ); add( toChar ); add( toInt ); } public void paint( Graphics g ) { if ( charToDigit ) g.drawString( "Convert character to digit: " + Character.digit( c, radix ), 25, 125 ); else g.drawString( "Convert digit to character: " + Character.forDigit( digit, radix ), 25, 125 ); } public void actionPerformed( ActionEvent e ) { if ( e.getSource() == toChar ) { charToDigit = false; digit = Integer.parseInt( input.getText() ); radix = Integer.parseInt( radixField.getText() ); repaint(); } else if ( e.getSource() == toInt ) { charToDigit = true; String s = input.getText(); c = s.charAt( 0 ); radix = Integer.parseInt( radixField.getText() ); repaint(); } } }