You may create your own Item that looks like a button by extending CustomItem
class
This is a whole working MIDlet with good MyButton
class.
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.CustomItem;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemStateListener;
import javax.microedition.midlet.MIDlet;
public class TestMidlet extends MIDlet implements ItemStateListener
{
class MyButton extends CustomItem
{
// Button's image
private Image i=null;
// Is the button currently down
private boolean down=false;
// How many times has the button been clicked
private int clicks=0;
// Constructor
public MyButton(Image _i)
{
super("");
i=_i;
}
// Allows to change image
public void setImage(Image _i)
{
i=_i;
repaint();
}
// Allows to get current image
public Image getImage()
{
return i;
}
// Tells if the button was clicked
public boolean isClicked()
{
if(clicks>0)
{
clicks--;
return true;
}
return false;
}
// Tells if the button is currently down
public boolean isDown()
{
return down;
}
// Functions that set the button to down or up states
public void setDown()
{
down=true;
repaint();
notifyStateChanged();
}
public void setUp()
{
down=false;
clicks++;
repaint();
notifyStateChanged();
}
// Minimal button size = image size
protected int getMinContentHeight()
{
return i.getHeight();
}
protected int getMinContentWidth()
{
return i.getWidth();
}
// Preferred button size = image size + borders
protected int getPrefContentHeight(int width)
{
return i.getHeight()+2;
}
protected int getPrefContentWidth(int height)
{
return i.getWidth()+2;
}
// Button painting procedure
protected void paint(Graphics g,int w,int h)
{
// Fill the button with grey color - background
g.setColor(192,192,192);
g.fillRect(0,0,w,h);
// Draw the image in the center of the button
g.drawImage(i,w/2,h/2,Graphics.HCENTER|Graphics.VCENTER);
// Draw the borders
g.setColor(down?0x000000:0xffffff);
g.drawLine(0,0,w,0);
g.drawLine(0,0,0,h);
g.setColor(down?0xffffff:0x000000);
g.drawLine(0,h-1,w,h-1);
g.drawLine(w-1,0,w-1,h);
}
// If FIRE key is pressed, the button becomes pressed (down state)
protected void keyPressed(int c)
{
if(getGameAction(c)==Canvas.FIRE);
setDown();
}
// When FIRE key is released, the button becomes released (up state)
protected void keyReleased(int c)
{
if(getGameAction(c)==Canvas.FIRE);
setUp();
}
// The same for sensor screens
protected void pointerPressed(int x,int y)
{
setDown();
}
protected void pointerReleased(int x,int y)
{
setUp();
}
}
MyButton b=null;
public void itemStateChanged(Item item)
{
if(item==b)
{
if(b.isClicked())
System.out.print("clicked, ");
System.out.println(b.isDown()?"currently down":"currently up");
}
}
public void startApp()
{
try
{
Form f=new Form("Example");
b=new MyButton(Image.createImage("/icon.png"));
f.append(b);
f.setItemStateListener(this);
Display.getDisplay(this).setCurrent(f);
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
notifyDestroyed();
}
}