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

📄 menucanvas.java

📁 J2me应用开发经典例子
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
      repaint();
    }
    else if (getTextField() != null && getTextField().isActive())
    {
		/*
		 * 由于我们的输入文本对话框和这个原生的Popup对话框是两个体系,
		 * 所以必须在此处特地判断是否要将用户的按键传送给激活状态的TextFieldPopup
		 * zhengyun 20051226 added
		 */
		getTextField().keyPressed(keyCode, getGameAction(keyCode));
	    repaint();
    }
    else
    {
      m_menu.keyPressed(keyCode);
      m_softButtons.keyPressed(keyCode);
    }
  }
  
  
  // Commandlistener implementation

  // See interface javadoc
  public void commandAction(Command c, Displayable d)
  {
    if (c == CMD_BACK)
    {
      m_menu.goBack();
    }
    else if (c == CMD_HELP)
    {
      PageItem item = m_menu.getSelectedItem();
      if (item != null)
      {
        char[] helpTxt = (char[])item.getProperty(ITEM_HELP);
        if (helpTxt != null)
        {			
          Bloglines.showPopup(m_display, helpTxt, Popup.ALT_OK, 0, 0, 0, null);
        }
      }
    }
  }
  
  /**
   * ItemAction implementation, called from items in
   * this menu. Instead of creating one class per action,
   * we centralize the action behaviour here. Each action
   * is identified by its id.
   * 
   * @param page	The page from which the action was called
   * @param item	The item to which the action belongs 
   */
  public void itemAction(MenuPage page, PageItem item)
  {
    int id = item.getId();
	System.out.println("itemAction Enter>>id=" + id);
	
	try
	{
	    switch(id)
	    {
	    case ACTION_LOCAL_READBLOGITEMS:
			{
				System.out.println("/* Start READBLOGITEMS!");
				// 选中的当前blog源的id
				int nSelectedItemId = page.getIndex(item);
				System.out.println("/* 1>>nSelectedItemId=" + nSelectedItemId);
				// 根据选中的item的序号,去找到对应的blog订阅号
				SerializeSub sub = (SerializeSub)m_subs.m_subs.elementAt(nSelectedItemId);
				System.out.println("/* 2");
				/*
				 * 在这里我们根据选中的订阅号,去请求bloglines该订阅号对应的blog
				 * 还有多少个项目没有被阅读,并返回这些文章的摘要
				 */
				if(m_getitems == null)
				{
					m_getitems = new ThreadGetItems(sub.subscriptionId, m_display, this);
					Thread thread;
				    (thread = new Thread(m_getitems)).start();
				}
				else
				{
					/*
					 * 通知线程去检索某个特定订阅的博客登录条目
					 */
					m_getitems.setSubId(sub.subscriptionId);
					m_getitems.notifyGetItems();
				}
			}
	      break;
	    case ACTION_QUIT:
	        Bloglines.shutdown();
	        break;
	    case ACTION_ABOUT:
	      Bloglines.showPopup(m_display, 
				  Resources.getChars(Resources.TXT_ABOUT), Popup.ALT_OK, 0, 0, 0, null);
	      break;
	    case ACTION_INPUTUSERMAIL:
			{
				/**
				   * 如果在RMS中找到了用户的登录电子邮件地址名,就不要求用户输入了
				   * zhengyun added 20051227
				   */
				String usermail = Bloglines.hasSavedUserMail();
				System.out.println("itemAction中得到的usermail=" + usermail);
				if( usermail.length() < 1)
				{
				    // 提示用户输入登录bloglines所需要的电子邮件地址名 
					Bloglines.showTextField(m_display, 
							Resources.getChars(Resources.TXT_H_PL_INPUT_USERMAIL), 
							TextFieldPopup.ALT_LOGIN_USERMAIL, 0, 0, 0,
							ACTION_INPUTUSERMAIL, null);
				}
				// 立刻改写原PageItem的id为ACTION_INPUTPASSWORD来要求输入密码
				item.setId(ACTION_INPUTPASSWORD);
			}
			break;
		case ACTION_SETTINGS_INPUTUSERMAIL:
			{
			    // 提示用户输入登录bloglines所需要的电子邮件地址名 
				Bloglines.showTextField(m_display, 
						Resources.getChars(Resources.TXT_H_PL_INPUT_USERMAIL), 
						TextFieldPopup.ALT_LOGIN_USERMAIL, 0, 0, 0,
						ACTION_SETTINGS_INPUTUSERMAIL, null);
			}
			break;
		case ACTION_INPUTPASSWORD:
			{
				/**
				   * 如果在RMS中找到了用户的登录密码,就不要求用户输入了
				   * zhengyun added 20051227
				   */
				String password = Bloglines.hasSavedUserPassword();
				System.out.println("itemAction中得到的password=" + password);
				
				if( password.length() < 1)	
				{
				    // 提示用户输入登录bloglines所需要的密码 
					Bloglines.showTextField(m_display, 
							Resources.getChars(Resources.TXT_H_PL_INPUT_USERPASS), 
							TextFieldPopup.ALT_LOGIN_USERMAIL, 0, 0, 0,
							ACTION_INPUTPASSWORD, null);
				}
				// 立刻改写原PageItem的id为ACTION_LOGIN来正式准备检索bloglines
				item.setId(ACTION_LOGIN);
			}
			break;
		case ACTION_SETTINGS_INPUTPASSWORD:
			{
			    // 提示用户输入登录bloglines所需要的密码 
				Bloglines.showTextField(m_display, 
						Resources.getChars(Resources.TXT_H_PL_INPUT_USERPASS), 
						TextFieldPopup.ALT_LOGIN_USERMAIL, 0, 0, 0,
						ACTION_SETTINGS_INPUTPASSWORD, null);
			}
			break;
		case ACTION_LOGIN:
			{
				/*
				 * 由于之前可能已经给该Page增加了blog订阅源的PageItem项,
				 * 所以必须在这里事先删除他们,要不然在界面上会重复多页PageItem
				 * zhengyun added 20051226
				 */
				if(m_loginPage.size() > 0)
				{
					m_loginPage.removeAllItems();
				}
				if(m_listsubs == null)
				{
					m_listsubs = new ThreadListSubs(m_loginPage, this);
					Thread thread;
				    (thread = new Thread(m_listsubs)).start();
				}
				else
				{
					/*
					 * 通知线程去查询用户订阅源信息
					 */
					m_listsubs.notifyListSubs();
				}				
			    
			}
	      break;
	    }
    }
	catch(Exception exc){}
  } 
  
  // Menulistener implementation
  
  // See interface javadoc
  public void newPage(MenuPage fromPage, MenuPage toPage, boolean back)
  {
    m_softButtons.enable(CMD_BACK, toPage != m_menu.getStartPage());
    if (fromPage instanceof FocusablePage)
    {
      ((FocusablePage)fromPage).onLeave();
    }
    if (toPage instanceof FocusablePage)
    {
      ((FocusablePage)toPage).onEnter();
    }
  }

  // See interface javadoc
  public void itemSelected(MenuPage page, PageItem oldItem, PageItem newItem)
  {
    if (newItem != null)
    {
      Object helpTxt = newItem.getProperty(ITEM_HELP);
      m_softButtons.enable(CMD_HELP, helpTxt != null);
    }
  }

  // See interface javadoc
  public void actionCalled(MenuPage page, PageItem item, ItemAction action) {}

  // See interface javadoc
  public void transitionStarted(MenuPage fromPage, MenuPage toPage, long delay,
      int frames, boolean back) {}

  // See interface javadoc
  public void transitionStopped(MenuPage fromPage, MenuPage toPage) {}

  /**
   * Binary page item turning boolean flags in rms on and off.
   * @author Peter Andersson
   */
  class PersistenceFlagItem extends BinaryPageItem
  {
    protected int m_key;

    public PersistenceFlagItem(int key, char[] label,
        Image imageTrue, Image imageFalse, int id)
    {
      super(label, imageTrue, imageFalse, null, MenuCanvas.this, id);
      m_key = key;
    }

    public boolean getBoolean()
    {
      return RmsFacade.getBoolean(m_key);
    }

    public void setBoolean(boolean value)
    {
      RmsFacade.setBoolean(m_key, value);
    }
  } // end of PersistenceFlagItem

  /**
   * Special painter that also paints <code>GameRecordPageItem</code>s and
   * adds special effects for selected items.
   * @author Peter Andersson
   */
  static class BackgammonMenuPainter extends DefaultMenuPainter
  {
    protected static final char[] SCORE_TXT = "9999".toCharArray();
    protected int m_scoreWidth;
    protected int[] m_rgbData;
    protected int m_canvasWidth;
	private Display m_display;
	
	  /** 
	   *  界面上的菜单被选中时的横条填充颜色,0x880000实际显示的就是暗红色
	   * */
	  protected int m_selectBackRectColor = 0x008a39;
    
    public BackgammonMenuPainter(int canvasWidth, Display display) throws Exception
    {
		try
		{			
			  m_display = display;
		      // Create selected item rgb buffer
			  m_canvasWidth = canvasWidth;
			  int nRgbData = m_canvasWidth * 4;
				
		      m_rgbData = new int[nRgbData];
		      //int bgcol = 0x880000;
			  Float fCanvasWidth = new Float(m_canvasWidth);
			  Float fMax = new Float(255);
			  for (int i = 0; i < m_canvasWidth; i++)
		      {
		        /*
		         * 你的代码中有对double的使用,比如这种代码:
		         double alpha = (double)Math.abs(i - m_canvasWidth / 2) /(double)m_canvasWidth;
		         但是,注意一点,CLDC1.1才开始支持Math,“Math functions in java.lang package is 
		         available only since CLDC-1.1”。所以,如果你一旦在CLDC1.0的nokia手机上使用,那么,
		         即使你用了try/catch捕获异常,程序也会自动退出。
		         这时候你也不要惊诧。
		         */
			    /*
			     * 
			    double alpha = (double)Math.abs(i - m_canvasWidth / 2) /
		        			   (double)m_canvasWidth;
		        int col = bgcol | (128 - (int)(255 * alpha) << 24);*/
				
			    Float alpha = new Float(i);
				alpha = fMax.Mul(alpha.Sub(fCanvasWidth.Div(2)).Div(fCanvasWidth));
				int col = m_selectBackRectColor | (128 - (int)alpha.toLong() << 24);
				
		        m_rgbData[i]                     = col;
		        m_rgbData[i + m_canvasWidth    ] = col;
		        m_rgbData[i + m_canvasWidth * 2] = col;
		        m_rgbData[i + m_canvasWidth * 3] = col;		        
		      }
			  
			    /*m_display.setCurrent(
						new Alert(
								"提示", 
								"BackgammonMenuPainter 3",
								null, AlertType.ERROR));
				Thread.sleep(2000);*/
		      
		      // Calculate maximum score text width
		      m_scoreWidth =
		        super.m_itemFont.charsWidth(SCORE_TXT, 0, SCORE_TXT.length);
		        
		}
		  catch(Exception exc)
		  {
			  display.setCurrent(
						new Alert(
								"错误", 
								"BackgammonMenuPainter:"
										+ exc.getMessage() + "/" + exc.getClass(),
										null, AlertType.ERROR));
		  }
    }
    
    // Extend PageItem.paintItem to handle GameRecordPageItems
    protected void paintItem(Graphics g, PageItem item, boolean selected, int x,
        int y, int w, int iMaxW, boolean to, boolean from) throws Exception
    {
		try
		{
		      if (selected)
		      {
		        int itemH = getItemHeight(item);
			  	// draw transparent background on selected item
			  	for (int by = y;
			  			by < y + itemH;
			  			by += 4)
			  	{
					/*
					 * Math.min(4, y + itemH - (by - y))在CLDC1.0上不能使用!
					 * 所以必须自己用if/else判断
					 * zhengyun 20051216
					 */
					int nTemp = y + itemH - (by - y);
					nTemp = nTemp>4 ? 4:nTemp;
				  	g.drawRGB(m_rgbData, 0, m_canvasWidth, x - m_canvasWidth/2, by, m_canvasWidth,
				  	    nTemp, true);
			  	}
		 
			  }
      
			  super.paintItem(g, item, selected, x, y, w, iMaxW, to, from);
		 }
		  catch(Exception exc)
		  {
			  m_display.setCurrent(
						new Alert(
								"错误", 
								"BackgammonMenuPainter.paintItem:"
										+ exc.getMessage() + "/" + exc.getClass(),
										null, AlertType.ERROR));
		  }
    }
 
    // Extend PageItem.getItemWidth to handle GameRecordPageItems
    protected int getItemWidth(PageItem item) throws Exception
    {
      return super.getItemWidth(item);
      
    }
  } // end of BackgammonMenuPainter
}

⌨️ 快捷键说明

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