// RandomNumbersMIDlet.java Copyright (c) Kari Laitinen // http://www.naturalprogramming.com/ // 2007-09-24 This file was created. // 2008-06-05 Last modification. // This program demonstrates the following things: // - ChoiceGroup class // - class Random and its methods nextInt() and nextDouble() // This program requires a CLDC 1.1 phone. // When the user presses a numerical key, this program generates a // random number and shows it on the screen. // Via the Settings menu the user can select what kinds of random // numbers will be generated. import javax.microedition.midlet.*; import javax.microedition.lcdui.*; import java.util.Random ; class RandomNumbersCanvas extends Canvas implements CommandListener { Display midlet_display ; int generated_random_integer = 0 ; double generated_random_double = 0 ; Form settings_form = new Form( "SETTINGS" ) ; String[] integer_ranges = { "0 ... 9", "0 ... 99", "0 ... 999" } ; ChoiceGroup integer_range_selection = new ChoiceGroup( "Range for random integers:", Choice.EXCLUSIVE, integer_ranges, null ) ; String[] double_selection_text = { "Show only a double value:" } ; ChoiceGroup double_selection = new ChoiceGroup( "Generate random double:", Choice.MULTIPLE, double_selection_text, null ) ; Command command_to_make_settings = new Command( "Settings", Command.SCREEN, 1 ) ; Command command_to_exit_settings = new Command( "Exit settings", Command.SCREEN, 1 ) ; public RandomNumbersCanvas( Display given_display ) { midlet_display = given_display ; settings_form.append( integer_range_selection ) ; settings_form.append( double_selection ) ; settings_form.addCommand( command_to_exit_settings ) ; settings_form.setCommandListener( this ) ; addCommand( command_to_make_settings ) ; setCommandListener( this ) ; } public void commandAction( Command given_command, Displayable current_display_content ) { if ( given_command == command_to_make_settings ) { midlet_display.setCurrent( settings_form ) ; } else if ( given_command == command_to_exit_settings ) { midlet_display.setCurrent( this ) ; } } public void keyPressed( int key_code ) { if ( key_code >= '0' && key_code <= '9' ) { Random random_number_generator = new Random() ; if ( double_selection.isSelected( 0 ) ) { generated_random_double = random_number_generator.nextDouble() ; } else if ( integer_range_selection.getSelectedIndex() == 0 ) { generated_random_integer = random_number_generator.nextInt( 10 ) ; } else if ( integer_range_selection.getSelectedIndex() == 1 ) { generated_random_integer = random_number_generator.nextInt( 100 ) ; } else if ( integer_range_selection.getSelectedIndex() == 2 ) { generated_random_integer = random_number_generator.nextInt( 1000 ) ; } } repaint() ; } protected void paint( Graphics graphics ) { graphics.setColor( 255, 255, 255 ) ; // White color graphics.fillRect( 0, 0, getWidth(), getHeight() ) ; graphics.setColor( 0, 0, 0 ) ; // Black color graphics.drawString( "LAST GENERATED RANDOM NUMBER:", 10, 20, Graphics.TOP | Graphics.LEFT ) ; if ( double_selection.isSelected( 0 ) ) { graphics.drawString( "" + generated_random_double, 10, 40, Graphics.TOP | Graphics.LEFT ) ; } else { graphics.drawString( "" + generated_random_integer, 10, 40, Graphics.TOP | Graphics.LEFT ) ; } } } public class RandomNumbersMIDlet extends MIDlet { Display midlet_display = Display.getDisplay( this ) ; RandomNumbersCanvas random_numbers_canvas = new RandomNumbersCanvas( midlet_display ) ; protected void startApp() throws MIDletStateChangeException { midlet_display.setCurrent( random_numbers_canvas ) ; } protected void pauseApp() { } protected void destroyApp( boolean unconditional_destruction_required ) { } }