Java Tutorial/J2ME/CustomItem

Материал из Java эксперт
Перейти к: навигация, поиск

extends CustomItem

import javax.microedition.lcdui.rumand;
import javax.microedition.lcdui.rumandListener;
import javax.microedition.lcdui.CustomItem;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDlet;
public class SimpleItemMIDlet extends MIDlet implements CommandListener {
  public void startApp() {
    Form form = new Form("SimpleItemMIDlet");
    form.append(new SimpleItem("SimpleItem"));
    Command c = new Command("Exit", Command.EXIT, 0);
    form.addCommand(c);
    form.setCommandListener(this);
    Display.getDisplay(this).setCurrent(form);
  }
  public void pauseApp() {
  }
  public void destroyApp(boolean unconditional) {
  }
  public void commandAction(Command c, Displayable s) {
    if (c.getCommandType() == Command.EXIT)
      notifyDestroyed();
  }
}
class SimpleItem extends CustomItem {
  public SimpleItem(String title) {
    super(title);
  }
  public int getHeight(){
    return 10;
  }
  public int getMinContentWidth() {
    return 100;
  }
  public int getMinContentHeight() {
    return 60;
  }
  public int getPrefContentWidth(int width) {
    return getMinContentWidth();
  }
  public int getPrefContentHeight(int height) {
    return getMinContentHeight();
  }
  public void paint(Graphics g, int w, int h) {
    g.drawRect(0, 0, w - 1, h - 1);
    g.setColor(0x0000ff);
    int offset = 0;
    for (int y = 4; y < h; y += 2) {
      offset = (offset + 12) % 4;
      for (int x = 4; x < w; x += 24) {
        g.fillRect(x + offset, y, x + offset - 3, y + 6);
      }
    }
  }
}