I am relatively new to J2ME Programming. I have been asked to built a program which displays 5 fruit names, and when "show" is clicked,it displays the respective images. Although I have coded it right, but image is not displayed, all I get is a black screen on the next form.
CODE :
import javax.microedition.lcdui.* ;
import javax.microedition.midlet.*;
/**
* @author Ashutosh
*/
public class Midlet extends MIDlet implements CommandListener
{
private Display display ;
private Form f,f1 ;
ChoiceGroup cg ;
private Image image ;
Command cmd = new Command("SHOW" , Command.OK , 1) ;
Command cmd1 = new Command("BACK" , Command.BACK , 1) ;
public Midlet()
{
try
{
image = Image.createImage("Apple.png");
}
catch (Exception e)
{
}
}
public void startApp()
{
f = new Form("Home") ;
f1 = new Form("Show Screen") ;
cg=new ChoiceGroup("Select Apple:",Choice.EXCLUSIVE);
display = Display.getDisplay(this) ;
cg.append("Apple",null) ;
cg.append("Banana",null) ;
cg.append("Cherry",null) ;
cg.append("Kiwi",null) ;
cg.append("Mango",null) ;
f.append(cg) ;
f.addCommand(cmd);
f1.addCommand(cmd1);
display.setCurrent(f);
f.setCommandListener(this);
f1.setCommandListener(this);
}
public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
}
public void commandAction(Command c, Displayable d)
{
int a = cg.getSelectedIndex() ;
if(c == cmd)
{
display.setCurrent(f1);
switch(a)
{
case 0 : f1.append(image) ;
default : System.exit(0) ;
}
}
if(c == cmd1)
{
display.setCurrent(f);
}
}
}
Thanks in advance.
I found the solution. You have to place your images in the "Resources" directory, created only in your IDE.Adding the image to the source folder on the Disk is inconsequential.
The following answer by msell effectively addresses the issue. https://stackoverflow.com/a/1332470/4786292