Hello, I think the code given below will give your more details knowledge about the Mutable Image in java than any explanation about it. So, understand the code and use it.
Code:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class MutableImage extends MIDlet
{
private Display dis;
private ImageCanvas1 canvas;
public MutableImage()
{
dis = Display.getDisplay(this);
canvas = new ImageCanvas1(this);
}
protected void startApp()
{
dis.setCurrent(canvas);
}
protected void pauseApp()
{
}
protected void destroyApp(boolean unconditional)
{
notifyDestroyed();
}
public void exitMIDlet()
{
destroyApp(true);
}
}
class ImageCanvas1 extends Canvas implements CommandListener
{
private Command cmd;
private MutableImage MI;
private Image img;
private String msg = "Mutable Image Example";
public ImageCanvas1(MutableImage MI)
{
this.MI = MI;
cmd = new Command("Exit", Command.EXIT, 1);
addCommand(cmd);
setCommandListener(this);
try
{
img = Image.createImage(100, 20);
Graphics g = img.getGraphics();
Font font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_MEDIUM);
g.setFont(font);
g.setColor(255, 0, 0);
g.fillRoundRect(0,0, img.getWidth()-1, img.getHeight()-1, 20, 20);
g.setColor(0, 0, 255);
g.drawString(msg,(img.getWidth()/2) - (font.stringWidth(msg)/2), 0, Graphics.TOP | Graphics.LEFT);
}
catch (Exception e)
{
System.err.println(e);
}
}
protected void paint(Graphics g)
{
if(img != null)
{
g.drawImage(img, getWidth()/2, getHeight()/2, Graphics.VCENTER | Graphics.HCENTER);
}
}
public void commandAction(Command c, Displayable d)
{
String label = c.getLabel();
if(label.equals("Exit"))
{
MI.cmdMIDlet();
}
}
}
Bookmarks