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

📄 bluegammon.java

📁 --- --- --- 基于J2ME的游戏程序--------很有技巧性的程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
          {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            BoardMediator.saveGame(baos);
            baos.close();
            GameRecord.saveGame(opponent, baos.toByteArray());
          }
        }
        catch (IOException e)
        {
          e.printStackTrace();
        }
      }
      else
      {
        // Remove the saved game data for this opponent
        GameRecord.saveGame(opponent, null);
      }
      BoardMediator.forceGameFinished();
      if (m_remoteConnection != null)
      {
        try
        {
          m_remoteConnection.close();
          m_remoteConnection = null;
        } catch (IOException e) {}
      }
    }
    
    // Load preferred rules
    Rules.setRuleFlags(RmsFacade.getInt(Bluegammon.RULES_PREFERRED));
    // Show menu
    MenuCanvas.getInstance().initShow();
    setCanvas(MenuCanvas.getInstance());
  }

  /**
   * Shuts down the MIDlet.
   */
  public synchronized static void shutdown()
  {
    BoardMediator.shutdown();
    RmsFacade.shutdown();
    Audio.shutdown();

    if (m_popupCache != null)
    {
      m_popupCache.dispose();
      m_popupCache = null;
    }

    m_inst = null;

    if (m_remoteConnection != null)
    {
      try
      {
        m_remoteConnection.close();
        m_remoteConnection = null;
      } catch (IOException e) {}
    }
    
    System.gc();

    Device.getMidlet().notifyDestroyed();
  }
  
  /**
   * Displays a popup with current rule settings.
   */
  public static void showRules()
  {
    String str = Resources.getString(Resources.TXT_T_RULES) + "\n\n";
    if (Rules.isAnyRuleSet())
    {
      if (Rules.isSet(Rules.EVEN_OUT))
      {
        str += Resources.getString(Resources.TXT_I_R_EVENOUT) + "\n";
      }
      if (Rules.isSet(Rules.MAX_FIVE))
      {
        str += Resources.getString(Resources.TXT_I_R_MAXFIVE) + "\n";
      }
    }
    else
    {
      str += Resources.getString(Resources.TXT_NO_RULES);
    }
    str += "\n";
     
    Bluegammon.showPopup(str.toCharArray(), null, 10, 0, 0, null);
  }

  /**
   * Gets input from the user. Runs asynchronously, i.e. this method does not
   * lock. User input is reported to specified <code>InputHandler</code>.
   * 
   * @param title       The title of the input dialog.
   * @param defaultText The default text presented in the input dialog.
   * @param length      The maximum length of the input.
   * @param constraints The constraints of the text as defined in <code>TextField</code>.
   * @param ih          The input handler, which handles the input when the user
   *                    commits the text.
   */
  public static void getStringInput(String title, char[] defaultText,
      int length, int constraints, StringInputHandler ih)
  {
    m_textBox.setString("");
    m_textBox.setMaxSize(length);
    m_textBox.setTitle(title);
    if (defaultText != null)
    {
      try
      {
        m_textBox.insert(defaultText, 0, defaultText.length, 0);
        m_textBox.setConstraints(constraints);
      } catch (Throwable t)
      {
        // Failed setting default text, may happen
        // due to constraints. Set to null text.
        try
        {
          m_textBox.setChars(null, 0, 0);
          m_textBox.setConstraints(constraints);
        } catch (Throwable t2) {}
      }
    }

    Device.getDisplay().setCurrent(m_textBox);
    m_inputHandler = ih;
  }

  /**
   * Returns the locking object of the user input mechanism
   * 
   * @return The input monitor
   */
  protected static Object getInputLock()
  {
    return m_textBox;
  }

  /**
   * Sets specified canvas as current
   * 
   * @param c The canvas to set
   */
  public synchronized static void setCanvas(PopupCanvas c)
  {
    m_currentCanvas = c;
    Device.getDisplay().setCurrent(c);
    c.repaint();
  }

  /**
   * Returns current canvas
   * 
   * @return Current canvas
   */
  public synchronized static PopupCanvas getCanvas()
  {
    return m_currentCanvas;
  }

  /**
   * Returns true if a popup is currently displayed to the user
   * 
   * @return true if a popup is displayed
   */
  public synchronized static boolean isShowingPopup()
  {
    if (getCanvas() != null &&
        getCanvas().getPopup() != null &&
        getCanvas().getPopup().isActive())
    {
      return true;
    }
    else
    {
      return false;
    }
  }

  /**
   * Returns current displayed popup
   * 
   * @return Current popup or null if no popup is displayed
   */
  public synchronized static Popup getCurrentPopup()
  {
    if (!isShowingPopup())
      return null;
    else
      return getCanvas().getPopup();
  }

  /**
   * Shows a popup on the device display, enabling the user to make a choice
   * amongst specified alternatives. Selected alternative is reported to
   * specified listener. The popup has a timeout - if this timeout is reached
   * the listener receives the specified timeout choice. If the user already has
   * a popup open, a call to this method makes current popup go away and the
   * listener to current popup is reported with the timeout choice. After this,
   * specified popup is presented. Thus, it is the responsibility of the server
   * or proxy layer to buffer multiple incoming popups. To check if a popup is
   * currently displayed, use <code>Bluegammon.getCurrentPopup()</code> or
   * <code>Bluegammon..isShowingPopup()</code>
   * 
   * @param text      Character array with text presented in popup.
   * @param altTexts  Array of character arrays with alternatives presented in popup. If
   *                  this argument is null, no alternatives are presented.
   * @param timeOutInSeconds  The timeout in seconds. If this argument is set to zero,
   *    			  the popup is displayed until the a user makes a choice. If no 
   * 				  choices are given and the timeout is zero, this popup will not show.
   * @param defaultChoice  The default selection when the popup appears.
   * @param timeOutChoice  The choice reported if the popup times out or is overridden by
   *		          another popup.
   * @param listener  The listener to this popup.
   */
  public synchronized static Popup showPopup(char[] text, char[][] altTexts,
      int timeOutInSeconds, int defaultChoice, int timeOutChoice,
      PopupListener listener)
  {
    if (getCanvas() == null)
      return null;

    if (getCanvas().getPopup() != null && getCanvas().getPopup().isActive())
    {
      Popup p = getCanvas().getPopup();
      getPopupListener().selectedChoice(p.getTimeOutChoice(), true);
      p.dispose();
      m_popupCache = p;
    }

    Popup p = getPopupInstance();
    p.init(text, altTexts, (byte) timeOutInSeconds, (byte) defaultChoice,
        (byte) timeOutChoice, getPopupListener(), getCanvas().getWidth(),
        getCanvas().getHeight());
    m_popupListener = listener;
    getCanvas().setPopup(p);
    getCanvas().repaint();
    return p;
  }

  /**
   * Returns a popup instance from cache.
   * @return	A popup instance
   */
  protected synchronized static Popup getPopupInstance()
  {
    if (m_popupCache == null)
    {
      m_popupCache = new Popup();
    }
    return m_popupCache;
  }

  /**
   * Returns a popuplistener that can be used for dispatching
   * popup events, used internally only.
   * @return A popuplistener dispatching events.
   */
  protected synchronized static PopupListener getPopupListener()
  {
    return getInstance();
  }

  /**
   * Returns a commandlistener that can be used for dispatching
   * user string input, used internally only.
   * @return A command listener for reporting user string input.
   */
  protected synchronized static CommandListener getInputCommandListener()
  {
    return getInstance();
  }

  /**
   * Returns an instance of <code>Bluegammon</code>,
   * used internally only for listener implementations.
   * @return  A bluegammon instance
   */
  private static Bluegammon getInstance()
  {
    return m_inst;
  }
  
  /**
   * Initializes this class.
   * @param bg		The bluegammon midlet.
   * @param display	The display.
   */
  public synchronized static void init(BluegammonMIDlet bg, Display display)
  {
    if (m_inst == null)
    {
      // Creates singleton listeners
      m_inst = new Bluegammon();
    }
    // Initialize device facade
    Device.init(bg, display);
    // Initialize the RMS persistence facade
    RmsFacade.init(NBR_OF_KEYS);
    // Set preferred rules
    Rules.setRuleFlags(RmsFacade.getInt(Bluegammon.RULES_PREFERRED));
    // Initialize the menu control canvas
    MenuCanvas.getInstance().initShow();
    setCanvas(MenuCanvas.getInstance());
    // Startup the backgammon board logic
    BoardMediator.startup();

    // Initialize generic string input handling
    m_textBox = new TextBox(null, null, 1, TextField.ANY);
    m_textBox.addCommand(OK);
    m_textBox.addCommand(CANCEL);
    m_textBox.setCommandListener(getInputCommandListener());
  }

  // PopupListener implementation
  public void selectedChoice(byte choice, boolean timeOut)
  {
    // Just forward to registered listener and repaint
    if (m_popupListener != null)
      m_popupListener.selectedChoice(choice, timeOut);
    if (getCanvas() != null)
      getCanvas().repaint();
  }

  // CommandListener implementation for input textbox
  public void commandAction(Command c, Displayable d)
  {
    if (d == m_textBox)
    {
      synchronized (getInputLock())
      {
        if (c != CANCEL &&
            m_textBox.getString().length() > 0 &&
            m_inputHandler != null)
        {
          final String input = m_textBox.getString();
          new Thread(new Runnable()
          {
            public void run()
            {
              m_inputHandler.handleStringInput(input);
            }
          }, "InputHandler").start();
        }
        setCanvas(getCanvas());
      }
    }
  }

  // Prevent external construction
  private Bluegammon() {}
}

⌨️ 快捷键说明

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