// This midlet creates an Image rom a file resource // make sure you have created a res directory in your project and save the // .png file in there import javax.microedition.midlet.*; import javax.microedition.lcdui.*; import java.io.IOException; public class ImmutImage extends MIDlet implements CommandListener{ private Display display; // a display private Form fmMain; // the main form private Command cmExit; // exit command private Image im; // an image instance // constructor public ImmutImage(){ display = Display.getDisplay(this); cmExit = new Command("Exit", Command.EXIT, 1); fmMain = new Form(" "); // for without title // add command and set command listener fmMain.addCommand(cmExit); fmMain.setCommandListener(this); // Read the approperiate image based on colour support from file // create an image object im // make sure the color the color is supoorted by the device try{ im = Image.createImage((display.isColor()) ? "/res/red.png" : "/res/green.png"); // First Code to be implemented // place a string on the form fmMain.append("A1"); //place the image on the form fmMain.append(new ImageItem(null, im, ImageItem.LAYOUT_NEWLINE_BEFORE| ImageItem.LAYOUT_CENTER|ImageItem.LAYOUT_NEWLINE_AFTER, null)); fmMain.append("A2"); // Some codes to be added // Add image to the Form with specified layout fmMain.append("B1"); fmMain.append(new ImageItem(null, im, ImageItem.LAYOUT_NEWLINE_BEFORE | ImageItem.LAYOUT_LEFT | ImageItem.LAYOUT_NEWLINE_AFTER, null)); // Add a string fmMain.append("B2"); // Add another string and image with associated layout fmMain.append("C1"); fmMain.append(new ImageItem(null, im, ImageItem.LAYOUT_NEWLINE_BEFORE | ImageItem.LAYOUT_RIGHT | ImageItem.LAYOUT_NEWLINE_AFTER, null)); // add a string fmMain.append("C2"); // Code with no directive just add string and image object fmMain.append("D1"); fmMain.append(im); fmMain.append("D2"); // show the layout directives in the output System.out.println("Layout Directives are: "+((ImageItem)fmMain.get(1)).getLayout()); display.setCurrent(fmMain); } catch(java.io.IOException e){ System.err.println("Unable to locate or read .png file"); } } public void startApp(){ display.setCurrent(fmMain); } public void pauseApp(){ } public void destroyApp(boolean unconditional){ } public void commandAction(Command c, Displayable s){ if(c == cmExit){ destroyApp(false); notifyDestroyed(); } } }