// TickerDemoMIDlet.java (c) Kari Laitinen // http://www.naturalprogramming.com // 2008-02-05 File created. // 2008-02-06 Last modification. // A Ticker in a midlet is a text that runs on the // top of the screen. A Ticker can be attached onto // any object that is related to class Displayable. // This program shows how a Ticker is attached to a // Canvas-based object. Canvas is a subclass of // Displayable. // This program also demonstrates how commands // are added and removed to/from a Canvas. import javax.microedition.midlet.*; import javax.microedition.lcdui.*; class TickerDemoCanvas extends Canvas implements CommandListener { int canvas_width = getWidth() ; int canvas_height = getHeight() ; Command command_to_show_ticker = new Command( "Show Ticker", Command.SCREEN, 1 ) ; Command command_to_remove_ticker = new Command( "Remove Ticker", Command.SCREEN, 1 ) ; Ticker ticker_to_be_shown = new Ticker( "This is a Ticker." ) ; public TickerDemoCanvas() { addCommand( command_to_show_ticker ) ; setCommandListener( this ) ; } public void commandAction( Command given_command, Displayable display_content ) { if ( given_command == command_to_show_ticker ) { setTicker( ticker_to_be_shown ) ; removeCommand( command_to_show_ticker ) ; addCommand( command_to_remove_ticker ) ; } else if ( given_command == command_to_remove_ticker ) { setTicker( null ) ; removeCommand( command_to_remove_ticker ) ; addCommand( command_to_show_ticker ) ; } } public void paint( Graphics graphics ) { graphics.setColor( 0, 0, 0 ) ; // black color is used for background graphics.fillRect( 0, 0, canvas_width, canvas_height ) ; graphics.setColor( 255, 255, 255 ) ; // white color for text graphics.drawString( "TickerDemoMIDlet.java", 50, 100, Graphics.TOP | Graphics.LEFT ) ; } } public class TickerDemoMIDlet extends MIDlet { Display display_of_this_midlet = Display.getDisplay( this ) ; TickerDemoCanvas canvas_of_this_midlet = new TickerDemoCanvas() ; protected void startApp() throws MIDletStateChangeException { display_of_this_midlet.setCurrent( canvas_of_this_midlet ) ; } protected void pauseApp() { } protected void destroyApp( boolean unconditional_destruction_required ) { } }