smileyitem.java

来自「《J2ME Game Programming》随书光盘源代码」· Java 代码 · 共 57 行

JAVA
57
字号

import javax.microedition.lcdui.*;

public class SmileyItem extends CustomItem
{
   // the state of the item; true for happy, false for sad
   private boolean isHappy;

   public SmileyItem(String title)
   {
      super(title);
      isHappy = true;
   }

   public void change()
   {
      isHappy = !isHappy;
      repaint();
   }

   // overide abstract methods to fill in size details for the item
   public int getMinContentWidth()             { return 50; }
   public int getMinContentHeight()            { return 20; }
   public int getPrefContentWidth(int width)   { return getMinContentWidth(); }
   public int getPrefContentHeight(int height) { return getMinContentHeight(); }

   // where responsible for drawing the item onto a supplied graphics canvas
   // for our custom smiley we just draw a red bax and some text.
   public void paint(Graphics g, int w, int h)
   {
      // set color to red
      g.setColor(0xff0000);

      // fill the item area
      g.fillRect(0, 0, w - 1, h - 1);

      // change to white
      g.setColor(0xffffff);

      // set the font
      g.setFont(Font.getDefaultFont());

      // draw the smiley using text
      g.drawString(":" + (isHappy ? ")" : "("), getMinContentWidth()/2, 1,
                   Graphics.TOP | Graphics.HCENTER);

   }

   // with a custom item we have to handle all the input ourselves, in this
   // case we just change the state of the smiley so matter what they hit
   protected void keyPressed(int keyCode)
   {
      change();
   }

}

⌨️ 快捷键说明

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