// MovingBallMIDlet.java Copyright (c) Kari Laitinen // http://www.naturalprogramming.com/ // 2002-??-?? File created. // 2007-09-17 Last modification. /* This program shows how to use a List object. The program displays a ball that can be moved with the arrow keys of the mobile phone. */ import javax.microedition.midlet.*; import javax.microedition.lcdui.*; class MovingBallCanvas extends Canvas implements CommandListener { MIDlet master_midlet ; Display midlet_display ; int ball_position_x, ball_position_y ; // The color is specified with a hexadecimal value 0x00RRGGBB // so that each color component (red, green, and blue) can // have value 0 ... 0xFF. int current_color = 0x00FF0000 ; // red is the initial color // The following two initialized arrays must be organized so that // the RGB value of a color has the same index as the name // of the color in question. int[] rgb_color_specifications = { 0x00FF0000, 0x0000FF00, 0x000000FF, 0x007F0000, 0x00007F00, 0x0000007F, 0x0000FFFF, 0x00FF00FF, 0x00FFFF00, 0x00000000, 0x007F7F7F } ; String[] selectable_colors = { "Red", "Green", "Blue", "Dark red", "Dark green", "Dark blue", "Cyan", "Magenta", "Yellow", "Black", "Grey" } ; List color_selection_list = new List( "Select Ball Color", List.IMPLICIT, selectable_colors, null ) ; Command exit_command = new Command( "Exit", Command.EXIT, 1 ) ; Command change_color_command = new Command( "Change color", Command.SCREEN, 1 ) ; public MovingBallCanvas( MIDlet given_master_midlet, Display given_display ) { master_midlet = given_master_midlet ; midlet_display = given_display ; ball_position_x = getWidth() / 2 - 20 ; ball_position_y = getHeight() / 2 - 20 ; addCommand( change_color_command ) ; addCommand( exit_command ) ; setCommandListener( this ) ; color_selection_list.setCommandListener( this ) ; } public void commandAction( Command given_command, Displayable display_content ) { if ( given_command == change_color_command ) { midlet_display.setCurrent( color_selection_list ) ; } else if ( given_command == List.SELECT_COMMAND ) { int index_of_selected_color = color_selection_list.getSelectedIndex() ; // The following assignment statement selects the right color // when the array pointed by rgb_color_specifications is initialized // so that it corresponds to the array containing the selectable // colors. current_color = rgb_color_specifications[ index_of_selected_color ] ; midlet_display.setCurrent( this ) ; } else if ( given_command == exit_command ) { // With the following method call this midlet informs the // runtime system that this method is ready for destruction. // The runtime system does not call the destroyApp() method // before the destruction operation. master_midlet.notifyDestroyed() ; } } public void keyPressed( int key_code ) { int game_action_code = getGameAction( key_code ) ; switch ( game_action_code ) { case UP: ball_position_y -= 3 ; break; case DOWN: ball_position_y += 3 ; break; case RIGHT: ball_position_x += 3 ; break; case LEFT: ball_position_x -= 3 ; break; } repaint() ; } protected void paint( Graphics graphics ) { graphics.setColor( 255, 255, 255 ) ; // white graphics.fillRect( 0, 0, getWidth(), getHeight() ) ; graphics.setColor( current_color ) ; graphics.drawRect( 0, 0, getWidth() - 1, getHeight() - 1 ) ; graphics.fillArc( ball_position_x, ball_position_y, 40, 40, 0, 360 ) ; graphics.drawString( "(" + ball_position_x + ", " + ball_position_y + ")", 2, 0, Graphics.TOP | Graphics.LEFT ) ; } } public class MovingBallMIDlet extends MIDlet { Display midlet_display = Display.getDisplay( this ) ; MovingBallCanvas moving_ball_canvas = new MovingBallCanvas( this, midlet_display ) ; public MovingBallMIDlet() { } protected void startApp() throws MIDletStateChangeException { midlet_display.setCurrent( moving_ball_canvas ) ; } protected void pauseApp() { } protected void destroyApp( boolean unconditional_destruction_required ) { } }