// CustomItemDemoMIDlet.java Copyright (c) Kari Laitinen // http://www.naturalprogramming.com/ // 2008-09-22 This file was created. // 2009-10-04 Last modification. // This program demonstrates the following things: // - CustomItem class: A CustomItem-based object is an Item which can // be attached to a Form, and which has a paint() method // with which it is possible use the Graphics method to produce // the content for the Item. // import javax.microedition.midlet.* ; import javax.microedition.lcdui.* ; class PaintableArea extends CustomItem { static final int DRAWING_AREA_WIDTH = 180 ; static final int DRAWING_AREA_HEIGHT = 140 ; CustomItemDemoMIDlet master_midlet ; public PaintableArea( CustomItemDemoMIDlet given_midlet ) { super( "PAINTABLE AREA" ) ; master_midlet = given_midlet ; } protected int getMinContentWidth() { return DRAWING_AREA_WIDTH ; } protected int getMinContentHeight() { return DRAWING_AREA_HEIGHT ; } protected int getPrefContentWidth( int tentative_content_height ) { return DRAWING_AREA_WIDTH ; } protected int getPrefContentHeight( int tentative_content_width ) { return DRAWING_AREA_HEIGHT ; } public void request_repaint() { repaint() ; } protected void paint( Graphics graphics, int current_item_width, int current_item_height ) { graphics.setColor( 255, 255, 0 ) ; // Yellow color graphics.fillRect( 0, 0, current_item_width, current_item_height ) ; graphics.setColor( 255, 0, 0 ) ; // Red color if ( master_midlet.ball_drawing_is_selected() == true ) { int ball_diameter = current_item_height / 2 ; graphics.fillArc( current_item_width / 2 - ball_diameter / 2, current_item_height / 2 - ball_diameter / 2, ball_diameter, ball_diameter, 0, 360 ) ; } else if ( master_midlet.triangle_drawing_is_selected() == true ) { int triangle_height = current_item_height / 2 ; int triangle_width = triangle_height * 3 / 2 ; int triangle_top_point_x = current_item_width / 2 ; int triangle_top_point_y = current_item_height / 4 ; graphics.fillTriangle( triangle_top_point_x, triangle_top_point_y, triangle_top_point_x - triangle_width / 2, triangle_top_point_y + triangle_height, triangle_top_point_x + triangle_width / 2, triangle_top_point_y + triangle_height ) ; } else if ( master_midlet.square_drawing_is_selected() == true ) { int square_height = current_item_height / 2 ; graphics.fillRect( current_item_width / 2 - square_height / 2, current_item_height / 2 - square_height / 2, square_height, square_height ) ; } } } public class CustomItemDemoMIDlet extends MIDlet implements ItemStateListener { Display midlet_display = Display.getDisplay( this ) ; PaintableArea paintable_area = new PaintableArea( this ) ; Form form_for_items = new Form( "CustomItem DEMO" ) ; String[] selectable_shapes = { "Ball", "Triangle", "Square" } ; ChoiceGroup shape_choices = new ChoiceGroup( "Select shape to draw:", Choice.EXCLUSIVE, selectable_shapes, null ) ; public CustomItemDemoMIDlet() { form_for_items.append( paintable_area ) ; form_for_items.append( shape_choices ) ; form_for_items.setItemStateListener( this ) ; } protected void startApp() throws MIDletStateChangeException { midlet_display.setCurrent( form_for_items ) ; } protected void pauseApp() { } protected void destroyApp( boolean unconditional_destruction_requested ) { } public boolean ball_drawing_is_selected() { return ( shape_choices.getSelectedIndex() == 0 ) ; } public boolean triangle_drawing_is_selected() { return ( shape_choices.getSelectedIndex() == 1 ) ; } public boolean square_drawing_is_selected() { return ( shape_choices.getSelectedIndex() == 2 ) ; } public void itemStateChanged( Item item_which_changed_state ) { if ( item_which_changed_state == shape_choices ) { paintable_area.request_repaint() ; } } }