diamonditem.java

来自「J2ME手机游戏开发技术详解随书光盘」· Java 代码 · 共 54 行

JAVA
54
字号
import javax.microedition.lcdui.*;

public class DiamondItem
    extends CustomItem {
  private boolean mState;
  
  public DiamondItem(String title) {
    super(title);
    mState = false;
  }
  
  public void toggle() {
    mState = !mState;
    repaint();
  }
  
  // CustomItem abstract methods.
  
  public int getMinContentWidth() { return 80; }
  public int getMinContentHeight() { return 40; }
  
  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);
    int stepx = 8, stepy = 16;
    for (int y = 0; y < h; y += stepy) {
      for (int x = 0; x < w; x += stepx) {
        g.drawLine(x, y, x + stepx, y + stepy);
        g.drawLine(x, y + stepy, x + stepx, y);
        if (mState == true) {
          int midx = x + stepx / 2;
          int midy = y + stepy / 2;
          g.fillTriangle(x, y, x + stepx, y, midx, midy);
          g.fillTriangle(midx, midy, x, y + stepy,
              x + stepx, y + stepy);
        }
      }
    }
  }
  
  // CustomItem methods.
  
  protected void keyPressed(int keyCode) { toggle(); }
  
  protected void pointerPressed(int x, int y) { toggle(); }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?