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

📄 gamewindow.java

📁 ErGo是一个很早的Java通用围棋服务器(IGS/NNGS)客户端程序。有全部源码和文档
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
      saveAs(filename);
      return false;
    }
  }

  public boolean saveAs () {
    // Ask the user for a pathname.
    FileDialog saveDialog
      = new FileDialog(this, "Save As...", FileDialog.SAVE);
    filename = (game.whiteName + "-" + game.blackName + "-"
		+ Util.getSortableDate() + ".sgf");
    if (window.getDefaultDirectory() != null)
      saveDialog.setDirectory(window.getDefaultDirectory());
    saveDialog.setFile(filename);
    saveDialog.show();
    filename = saveDialog.getFile();
    if (filename == null)
      return true;
    else {
      filename = saveDialog.getDirectory() + filename;
      saveAs(filename);
      window.setDefaultDirectory(saveDialog.getDirectory());
      return false;
    }
  }

  private void saveAs (String filename) {
    try {
      PrintWriter outstream = null;
      if (filename != null)
	outstream = new PrintWriter(new FileWriter(filename));
      game.writeSGF(outstream);	// outstream = null means write to console
      if (outstream != null)
	outstream.close();
      needsSaving = false;
      saveItem.setEnabled(false);
    }
    catch (IOException e) {
      window.displayString("Save error: " + e);
    }
  }


  private void copyAsSGF () {
    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    StringWriter swriter = new StringWriter();
    PrintWriter pwriter = new PrintWriter(swriter);
    game.writeSGF(pwriter);
    pwriter.flush();
    StringSelection data = new StringSelection(swriter.toString());
    clipboard.setContents(data, data);
  }

  public void close () {
    if (needsSaving && opser.getBooleanOption(savePromptString)) {
      new YNCDialog(this, "Save game?", true, "Save " + toString() + "?",
		    new ActionListener() {
		      public void actionPerformed (ActionEvent e) {
			closeInternal(e.getActionCommand());
		      }
		    }, true).open();
    }
    else
      closeInternal("no");
  }

  // +++ This uses package scope rather than private to workaround
  //     Java bug 4034664.  Fixed in Java 1.2.
  void closeInternal (String cmd) {
    if (!"cancel".equalsIgnoreCase(cmd)) {
      boolean remove = true;
      if (gameNumber != 0	// 0 means invalid game num
	  && gameStatus == PLAYING
	  && isNetGame()) {
	if (!isParticipating()) {
	  conn.send("unobserve " + gameNumber);
	  remove = false;
	}
	else if (ourColor == Move.BOTH)	// Teaching game.
	  conn.send("adjourn");
      }
      if ("yes".equalsIgnoreCase(cmd))
	if (save() == true)	// save dialog cancelled.
	  return;
      setVisible(false);
      // Observed games are no longer removed from the list here.
      // Instead it's done when the controller receives
      // the message "9 Removing game n from observation list."  This
      // avoids a timing problem in which if a new move arrives before
      // the server receives the unob message the window would get
      // recreated.  This way the moves keep getting added to the window
      // (even though it's hidden) until the unob arrives.

      // Note: The dispose() call also occurs in removeGameWindow(). AMB
      // at 0.8

      // although moves arrive as per first comment, window will never be
      // shown again. Therefore we can remove it from list of
      // Observers. AMB 0.8  
      window.registrar.releaseMenu(windowsMenu); 

      if (remove)
	window.removeGameWindow(this);
    }
  }

  public void commitSuicide () {
    statusPanel.commitSuicide();
    dispose();
  }


  /*********************************************************
     Manage attachment and display of window subcomponents:
   *********************************************************/


  public void refresh () {
    boardCanvas.refreshLocal();
    updateKibitzArea();
    statusPanel.refresh();
    northCanvas.refresh();
    southCanvas.refresh();
    eastCanvas.refresh();
    westCanvas.refresh();
  }

  // Forwarding...
  public int getBevelWidth () {
    return boardCanvas.getBevelWidth();
  }

  public void toggleCoordinates () {
    toggleCoordinates(null);
  }

  // cc == null means show/hide all
  // cc != null means hide cc only
  public void toggleCoordinates (CoordinateCanvas cc) {
    if (cc == null) {
      showCoordinates = !showCoordinates;
      Boolean B = showCoordinates?T:F;
      opser.updateOption(NcoordString, B);
      opser.updateOption(WcoordString, B);
      opser.updateOption(EcoordString, B);
      opser.updateOption(ScoordString, B);

      showCoordinatesItem.setState(showCoordinates);
      configureCoordinates();
    }
    else {
      // Just hiding one coordinate panel.  Make sure that next time
      // Show Coordinates menu item is chosen it turns them all on.
      showCoordinatesItem.setState(false);
      if (cc == northCanvas)
	opser.updateOption(NcoordString, F);
      if (cc == eastCanvas)
	opser.updateOption(EcoordString, F);
      if (cc == southCanvas)
	opser.updateOption(ScoordString, F);
      if (cc == westCanvas)
	opser.updateOption(WcoordString, F);

      boardPanel.remove(cc);
      validate();
    }
  }

  private void configureCoordinates () {
    if (opser.getBooleanOption(NcoordString))
      boardPanel.add("North", northCanvas);
    else
      boardPanel.remove(northCanvas);
    if (opser.getBooleanOption(ScoordString))
      boardPanel.add("South", southCanvas);
    else
      boardPanel.remove(southCanvas);
    if (opser.getBooleanOption(EcoordString))
      boardPanel.add("East", eastCanvas);
    else
      boardPanel.remove(eastCanvas);
    if (opser.getBooleanOption(WcoordString))
      boardPanel.add("West", westCanvas); 
    else
      boardPanel.remove(westCanvas); 
    validate();
    refresh();
  }

  private GWPanel configureBrowser (boolean tall) {
    GWPanel browserPanel = new GWPanel();
    Panel bpanel1 = new GWPanel();
    Panel bpanel2 = new GWPanel();
    if (tall) {
      bpanel1.setLayout(new ColumnLayout());
      bpanel1.add(stepBackButton);
      bpanel1.add(backBranchButton);
      bpanel1.add(backKibButton);
      bpanel1.add(toBeginningButton);
      bpanel2.setLayout(new ColumnLayout());
      bpanel2.add(stepForwardButton);
      bpanel2.add(fwdBranchButton);
      bpanel2.add(fwdKibButton);
      bpanel2.add(toEndButton);
      browserPanel.setLayout(new RowLayout(RowLayout.CENTER, 0, 0, false));
    }
    else {
      bpanel1.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
      bpanel1.add(toBeginningButton);
      bpanel1.add(stepBackButton);
      bpanel1.add(stepForwardButton);
      bpanel1.add(toEndButton);
      bpanel2.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
      bpanel2.add(backBranchButton);
      bpanel2.add(backKibButton);
      bpanel2.add(fwdKibButton);
      bpanel2.add(fwdBranchButton);
      browserPanel.setLayout(new ColumnLayout(ColumnLayout.CENTER, 0, 0, false));
    }
    browserPanel.add(bpanel1);
    browserPanel.add(bpanel2);
    return browserPanel;
  }

  // Build a new StatusPanel and fix the GameWindow to display it in the right place.
  // 
  public StatusPanel configureStatus (String position) {
    String old = opser.getStringOption(statusString);
    if (!position.equalsIgnoreCase(old)) {
      opser.updateOption(statusString, position);
    }
    if (statusPanel != null)
      topPanel.remove(statusPanel); // always remove old one
    if (position.equalsIgnoreCase("None"))
      showStatus = false;
    else if (position.equalsIgnoreCase("Bottom")) {
      tallConfiguration = true;
      statusPanel = new StatusPanel(this, configureBrowser(tallConfiguration),
				    popupControl, !tallConfiguration, conn);
      topPanel.add("South", statusPanel);
    }
    else {
      tallConfiguration = false;
      statusPanel = new StatusPanel(this, configureBrowser(tallConfiguration),
				    popupControl, !tallConfiguration, conn);
      topPanel.add("East", statusPanel);
    }
    //this.pack();
    //invalidate();
    validate();
    return statusPanel;
  }

  public void updateTimers (int wcaptured, int wtime, int wstones,
			    int bcaptured, int btime, int bstones) {
    int color = game.currentMove().color();
    statusPanel.startTicking(color);
    statusPanel.stopTicking(Move.nextColor(color));
    statusPanel.setTime(Move.BLACK, btime, bstones);
    statusPanel.setTime(Move.WHITE, wtime, wstones);
    
  }

  public void setKibitzVisible (boolean showKibitzes1) {
    showKibitzes = showKibitzes1;
    if (showKibitzes) {
      add("South", bottomPanel);
    }
    else
      remove(bottomPanel);
    validate();
  }



  /********************
   * Debugging code   *
   ********************/

  // for debugging only
  void displayBoard () {
    System.out.println();
    System.out.println();
    // Display current state of board in ASCII
    for (int r = game.size() - 1; r >= 0; r--) {
      for (int c = 0; c < game.size(); c++) {
	System.out.print(game.ss.readAnyStone(r, c) + " ");
      }
      System.out.print("    ");
      for (int c = 0; c < game.size(); c++) {
	if (game.groupAt(r, c) == null)
	  System.out.print(". ");
	else
	  System.out.print("G ");
      }
      System.out.println();
    }
  }

  void displayGroups () {
    System.out.println();
    SimpleGroupVector seen = new SimpleGroupVector();
    for (int r = game.size() - 1; r >= 0; r--) {
      for (int c = 0; c < game.size(); c++) {
	SimpleGroup group = game.groupAt(r, c);
	if (group != null && seen.indexOf(group) == -1) {
	  System.out.println(group.toString());
	  seen.addElement(group);
	}}}
    // Display game tree.
    /*
    System.out.println();
    Move fm = game.firstMove();
    if(fm != null)
      fm.print();
    System.out.flush();
    */
  }

  // CheckboxMenuItemGroup
  class CBMIGroup implements ItemListener {
    private Vector Items = new Vector();
    private ItemListener external;
    private CheckboxMenuItem current = null;
    private Menu commonParent = null;
    CBMIGroup (ItemListener external1) {
      external = external1;
    }
    CBMIGroup (ItemListener external1, Menu commonParent) {
      this.commonParent = commonParent;
      external = external1;
    }
    private CheckboxMenuItem itemAt (int i) {
      return (CheckboxMenuItem)Items.elementAt(i);
    }
    public void register (CheckboxMenuItem cbmi) {
      Items.addElement(cbmi);
      cbmi.addItemListener(this);
    }
    public CheckboxMenuItem generate (Menu parent, String label) {
      CheckboxMenuItem togo = new CheckboxMenuItem(label);
      if (parent != null)
	parent.add(togo);
      register(togo);
      return togo;
    }
    public CheckboxMenuItem generate (String label) {
      return generate(commonParent, label);
    }
    public void deregister (CheckboxMenuItem cbmi) {
      Items.removeElement(cbmi);
    }
    public void init (CheckboxMenuItem current1) { // quietly initialise.
      selectItem(current1);
    }
    private void selectItem (Object source) {
      for (int i=0; i<Items.size(); ++i) {
	CheckboxMenuItem cbmi = itemAt(i);
	if (source == cbmi)
	  cbmi.setState(true);
	else cbmi.setState(false);
      }
    }
    public void itemStateChanged (ItemEvent ie) {
      Object source = ie.getSource();
      if (! (source instanceof CheckboxMenuItem))
	return;
      boolean deselect = false;
      selectItem(source);
      if (ie.getStateChange() == ItemEvent.SELECTED) 
	external.itemStateChanged(ie); // propagate it out.
    }
  }  // end inner class CBMIGroup


}  // end class GameWindow

⌨️ 快捷键说明

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