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

📄 popup.java

📁 蓝牙骰子游戏: 用J2ME开发的手机蓝牙骰子游戏
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
      else
        c = text[breakOffset];
      if (c == NEWLINE)
      {
        // got a nice break here, new line
        niceB = breakOffset;
        break charLoop;
      }

      // Try finding break charachters
      breakCharLoop:
      for (int i = TEXTBREAKS.length - 1; i >= 0; i--)
      {
        if (c == TEXTBREAKS[i])
        {
          niceB = breakOffset;
          break breakCharLoop;
        }
      }
      if (breakOffset == offset + len - 1)
      {
        // Special case, skip the last character
        niceB = breakOffset + 1;
      }
      breakOffset++;
      textW += f.charWidth(c);
    }
    if (niceB > offset && niceB < offset + len - 2 && (text[niceB + 1] == ' '))
      return niceB + 2;        // case: special case to get rid of extra spaces
    else if (niceB > offset && niceB < offset + len)
      return niceB + 1;        // case: found a nice break, use this
    else if (breakOffset > offset + 1)
      return breakOffset - 1;  // case: broke due to text width too big
    else if (breakOffset == offset)
      return breakOffset + 1;  // case: broken on first char, step one more
    else
      return breakOffset;      // case: default
  }

  /**
   * Paints the popup. Call this from your <code>Displayable</code>'s
   * paint method.
   * 
   * @param g  Graphics context to paint on.
   */
  public void paint(Graphics g)
  {
    if (m_active)
    {
      // draw transparent background
      for (int y = OFFSET_POPUP + m_yoffset; y < OFFSET_POPUP + m_yoffset + m_h; y += 8)
      {
        g.drawRGB(m_rgbData, 0, m_w, OFFSET_POPUP, y, m_w, Math.min(8,
            OFFSET_POPUP + m_yoffset + m_h - y), true);
      }

      // border
      g.setColor(m_borderColor);
      g.drawRect(OFFSET_POPUP, OFFSET_POPUP + m_yoffset, m_w, m_h);

      // text
      g.setColor(m_textColor);
      g.setFont(m_font);
      int y = OFFSET_POPUP + OFFSET_TEXT + m_yoffset;
      int maxLine =
        Math.min(m_curLine + m_visibleLines, m_breakTextData.length);
      for (int i = m_curLine; i < maxLine; i++)
      {
        int offset = m_breakTextData[i][0];
        int len = m_breakTextData[i][1];
        if (len == 1 && m_text[offset] == NEWLINE)
        {
          y += m_fontHeight;
        }
        else
        {
          if (m_text[offset + len - 1] == NEWLINE)
          {
            len--;
          }
          g.drawChars(m_text, offset, len, OFFSET_POPUP + OFFSET_TEXT
              + (m_w >> 1), y, Graphics.TOP | Graphics.HCENTER);
          y += m_fontHeight;
        }
      }

      // scrollbar
      if (m_visibleLines < m_breakTextData.length)
      {
        int sbh = m_visibleLines * m_fontHeight;   // Scrollbar max height
        int sbstep = ((sbh - 4) << 8) / m_maxLine; // Scrollbar height * 256
        int sbX =
          OFFSET_POPUP + m_w - SB_WIDTH - (SB_WIDTH >> 1); // Scrollbar x-coordinate
        g.setColor(m_textColor);
        g.fillRect(sbX, OFFSET_POPUP + OFFSET_TEXT + ((m_curLine * sbstep) >> 8),
          SB_WIDTH, 4 + (sbstep >> 8));
      }

      // alternatives
      if (m_alternatives > 0)
      {
        y =
          OFFSET_POPUP + OFFSET_TEXT + m_h + m_yoffset - OFFSET_TEXT - m_fontHeight;
        int dx = (m_w / (m_alternatives + 1));
        int x = OFFSET_POPUP + OFFSET_TEXT;
        for (int i = 0; i < m_alternatives; i++)
        {
          char[] t = m_altTexts[i];
          x += dx;
          int xx = x - (m_font.charsWidth(t, 0, t.length) >> 1);
          if (m_curAlt != i)
          {
            // Unselected alternative
            g.setColor(m_alternativeColor);
            g.drawChars(t, 0, t.length, xx, y, Graphics.TOP | Graphics.LEFT);
          } 
          else
          {
            // Selected alternative
            g.setColor(m_alternativeColor);
            g.drawChars(t, 0, t.length, xx + 1, y + 1,
                Graphics.TOP | Graphics.LEFT);
            g.setColor(m_selectedAlternativeColor);
            g.drawChars(t, 0, t.length, xx, y, Graphics.TOP | Graphics.LEFT);
          }
        }
      }
    }
  }

  /**
   * Handles user interaction when pressing a key.
   * Call this from your <code>Displayable</code>'s
   * keyPressed keyRepeated method.
   * 
   * @param keyCode  The keycode.
   * @param gameCode The gamecode.
   */
  public void keyPressed(int keyCode, int gameCode)
  {
    if (m_active)
    {
      if (m_alternatives < 1)
      {
        // If no choice, any key will do
        m_active = false;
        if (m_listener != null)
          m_listener.selectedChoice(m_curAlt, false);
      }
      else
      {
        switch (gameCode)
        {
        // Scroll text
        case Canvas.DOWN:
        {
          m_curLine++;
          if (m_curLine >= m_maxLine)
            m_curLine = 0;
          break;
        }
        case Canvas.UP:
        {
          if (m_maxLine > 0)
            m_curLine--;
          if (m_curLine < 0)
            m_curLine = m_maxLine - 1;
          break;
        }
        // Select among choices
        case Canvas.RIGHT:
        {
          m_curAlt++;
          if (m_curAlt >= m_alternatives)
            m_curAlt = 0;
          break;
        }
        case Canvas.LEFT:
        {
          m_curAlt--;
          if (m_curAlt < 0)
            m_curAlt = (byte) (m_alternatives - 1);
          break;
        }
        case Canvas.FIRE:
        {
          // Select
          if (m_curAlt >= 0)
          {
            m_active = false;
            if (m_listener != null)
              m_listener.selectedChoice(m_curAlt, false);
          }
          break;
        }
        case Device.KEYCODE_BACK:
        {
          m_active = false;
          if (m_listener != null)
            m_listener.selectedChoice(m_timeOutAlt, false);
          break;
        }
        }
      }
    }
  }

  /**
   * Disposes all resources held by this popup and closes it.
   */
  public void dispose()
  {
    m_active = false;
    m_text = null;
    m_altTexts = null;
    m_listener = null;
    m_breakTextData = null;
    System.gc();
  }

  /**
   * Returns whether this popup is active or not.
   * 
   * @return true if active, false otherwise.
   */
  public boolean isActive()
  {
    return m_active;
  }

  /**
   * Returns alternative index on timeout
   * 
   * @return timeout alternative
   */
  public byte getTimeOutChoice()
  {
    return m_timeOutAlt;
  }

  /**
   * Called by framework to check if popup reached its' timeout.
   * 
   * @return true if timeout, false otherwise.
   */
  protected boolean pollTimeout()
  {
    if (m_active)
    {
      if (m_endTime > 0 && System.currentTimeMillis() > m_endTime)
      {
        m_active = false;
        if (m_listener != null)
        {
          m_listener.selectedChoice(m_timeOutAlt, true);
          return true;
        }
      }
    }
    return false;
  }

  // Runnable impl to poll this popup
  public void run()
  {
    while (isActive())
    {
      // Poll popup timeout
      try
      {
        Thread.sleep(1000);
        pollTimeout();
      } catch (InterruptedException e) {}
    }
  }
}

⌨️ 快捷键说明

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