⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 smileyitem.java

📁 《J2ME Game Programming》随书光盘源代码
💻 JAVA
字号:

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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -