// PictureViewingMIDlet.java Copyright (c) Kari Laitinen // http://www.naturalprogramming.com/ // 2007-09-12 This file was created. // 2007-09-12 Last modification. // This program shows how pictures (e.g. .png files) can be "drawn" // on a canvas. Pictures are represented by Image objects in this // program. // By pressing the arrow keys the user can change the picture that is // currently being shown. // The used .png files can be found in the same folder where this .java // file is located. If you work with the Sun Wireless Toolkit, the .png // files should be copied to the res folder of the project. import java.io.* ; import javax.microedition.midlet.*; import javax.microedition.lcdui.*; class PictureViewingCanvas extends Canvas { int index_of_current_picture = 0 ; String[] picture_file_names = { "/marilyn_by_warhol.png", "/nicole_kidman.png", "/terminator2.png", "/kate_winslet.png", "/scanned_leave.png" } ; Image[] pictures_to_be_shown = new Image[ picture_file_names.length ] ; public PictureViewingCanvas() { for ( int picture_index = 0 ; picture_index < picture_file_names.length ; picture_index ++ ) { try { pictures_to_be_shown[ picture_index ] = Image.createImage( picture_file_names[ picture_index ] ) ; } catch ( IOException caught_io_exception ) { System.out.print( "\n Image object not created .... " + picture_file_names[ picture_index ] ) ; } } } protected void paint( Graphics graphics ) { graphics.setColor( 255, 255, 255 ) ; // white graphics.fillRect( 0, 0, getWidth(), getHeight() ) ; graphics.drawImage( pictures_to_be_shown[ index_of_current_picture ], 2, 0, Graphics.TOP | Graphics.LEFT ) ; } public void keyPressed( int key_code ) { int game_action_code = getGameAction( key_code ) ; switch ( game_action_code ) { case UP: case LEFT: if ( index_of_current_picture > 0 ) { index_of_current_picture -- ; } else { index_of_current_picture = pictures_to_be_shown.length - 1 ; } break ; case DOWN: case RIGHT: if ( index_of_current_picture < ( pictures_to_be_shown.length - 1 )) { index_of_current_picture ++ ; } else { index_of_current_picture = 0 ; } break ; } repaint() ; } } public class PictureViewingMIDlet extends MIDlet { Display display_of_this_midlet = Display.getDisplay( this ) ; PictureViewingCanvas picture_viewing_canvas = new PictureViewingCanvas() ; protected void startApp() throws MIDletStateChangeException { display_of_this_midlet.setCurrent( picture_viewing_canvas ) ; } protected void pauseApp() { } protected void destroyApp( boolean unconditional_destruction_required ) { } }