コード例 #1
0
ファイル: Panel.java プロジェクト: haskins/scorefour
  // ---------makes the animation for the remote player's (computer) bead---------
  private void startRemoteMoving(String position) {

    System.out.println("procesing " + position);
    if (isWin(position)) {

      winner = parseWinner(position);
      if (winner == 1) startImageLoader(WIN_IMAGE_FILE);
      else {

        isWinLoseTimer = new Timer(IMAGE_LOADERS_TIMER, this);
        isWinLoseTimer.setActionCommand("is win_lose");
        isWinLoseTimer.setRepeats(false); // occurs only one time
        isWinLoseTimer.start();
      }
      repaint();
      return;
    }
    if (isDraw(position)) {
      winner = 0;
      startImageLoader(TIE_IMAGE_FILE);
      repaint();
      return;
    }

    insertedInPegNum = mapPosToPeg(position);

    availableBeads[currentBead].setX(
        PEG_INITIAL_X[insertedInPegNum - 1]
            - (int) (availableBeads[currentBead].getBounds().getWidth() / 2));
    availableBeads[currentBead].setY(PEG_INITIAL_Y[insertedInPegNum - 1]);

    pegs[insertedInPegNum - 1].addBead(availableBeads[currentBead]);

    remoteBeadAnimTimer = new Timer(ANIMATION_REFRESH_RATE, this);
    remoteBeadAnimTimer.setActionCommand("remote bead animator");

    // sets the peg
    remoteBeadAnimTimer.setRepeats(true);
    remoteBeadAnimTimer.start();
    // creates and inserts new bead
    currentBead++;

    // System.out.println( "Type of game: " + typeOfGame );
    if (typeOfGame.equals("Gertrudis vs Computer")) {

      if (currentBead % 2 != 0) {
        availableBeads[currentBead] = new Bead(BEAD_INITIAL_X, BEAD_INITIAL_Y, 'b');
        availableBeads[currentBead].setImage(board.getBlackBeadImage());
      } else {
        availableBeads[currentBead] = new Bead(BEAD_INITIAL_X, BEAD_INITIAL_Y, 'w');
        availableBeads[currentBead].setImage(board.getWhiteBeadImage());
      }

    } else {
      availableBeads[currentBead] = new Bead(BEAD_INITIAL_X, BEAD_INITIAL_Y, 'w');
      availableBeads[currentBead].setImage(board.getWhiteBeadImage());
    }
  }
コード例 #2
0
ファイル: Panel.java プロジェクト: haskins/scorefour
 public void setGertrudeVsComputer() {
   typeOfGame = "Gertrudis vs Computer";
   typeOfGameTimer = new Timer(IMAGE_LOADERS_TIMER, this);
   typeOfGameTimer.setActionCommand("Gertrude vs Computer");
   typeOfGameTimer.setRepeats(false); // occurs only one time
   typeOfGameTimer.start();
 }
コード例 #3
0
ファイル: Panel.java プロジェクト: haskins/scorefour
  @Override
  public void paint(Graphics g) {
    super.paint(g);
    Graphics2D g2d = (Graphics2D) g;

    displayNames(g);
    displayWinner(g);
    g2d.drawImage(board.getImage(), board.getX(), board.getY(), null, null);

    for (int i = 0; i < 16; i++) {
      g2d.drawImage(pegs[i].getImage(), pegs[i].getX(), pegs[i].getY(), null, null);
      for (int j = 0; j < pegs[i].amountOfBeads2(); j++) {
        g2d.drawImage(
            pegs[i].beadAt(j).getImage(),
            pegs[i].beadAt(j).getX(),
            pegs[i].beadAt(j).getY(),
            null,
            null);
      }
    }

    if (availableBeads[currentBead].getImage() == null) {
      availableBeads[currentBead].loadImage();
    }

    g2d.drawImage(
        availableBeads[currentBead].getImage(),
        availableBeads[currentBead].getX(),
        availableBeads[currentBead].getY(),
        null,
        null);

    if (beadMustAnimate) {
      animationTimer = new Timer(ANIMATION_REFRESH_RATE, this); // every
      // REFRESH_RATE
      // ms
      animationTimer.setActionCommand("animation");
      animationTimer.start();
      beadMustAnimate = false;
      currentBead++;
      // creates next new bead
      availableBeads[currentBead] = new Bead(BEAD_INITIAL_X, BEAD_INITIAL_Y, 'b');
      availableBeads[currentBead].setImage(board.getBlackBeadImage());
    }
  }
コード例 #4
0
ファイル: Panel.java プロジェクト: haskins/scorefour
  ////////////////////////////////////////////////////////////
  // 			CONSTRUCTOR
  ////////////////////////////////////////////////////////////
  public Panel(
      String p1,
      String p2,
      LinkedBlockingQueue<String> sendQueue,
      LinkedBlockingQueue<String> recvQueue,
      JMenuBar menuBar)
      throws InterruptedException, ExecutionException {
    winner = NO_WINNER;
    typeOfGame = "";
    this.menuBar = menuBar;
    this.sendQueue = sendQueue; // receives queues
    this.recvQueue = recvQueue;
    board = new Board(BOARD_INITIAL_X, BOARD_INITIAL_Y); // creates board
    board.setPlayerNames(p1, p2);
    pegs = new Peg[MAX_NUM_PEGS];
    beadMustAnimate = false;
    isFirstTransaction = true;
    insertedInPegNum = 0;
    currentBead = 0;
    // its referree's responsibility to stop the game when no more bead are avasilable
    availableBeads = new Bead[MAX_NUM_BEADS * 2];

    // sets first free bead
    availableBeads[0] = new Bead(BEAD_INITIAL_X, BEAD_INITIAL_Y, 'w');
    availableBeads[0].setImage(board.getWhiteBeadImage());
    for (int i = 0; i < 16; i++) {
      pegs[i] = new Peg(PEG_INITIAL_X[i], PEG_INITIAL_Y[i]);
    }

    setFocusable(true); // makes panel focusable

    // adds listeners
    mouseListener = new MouseListener();
    addMouseMotionListener(mouseListener);
    addMouseListener(mouseListener);

    // when this timer fires, images will be displayed
    updateWhenLoaded = new Timer(IMAGE_LOADERS_TIMER, this);
    updateWhenLoaded.setActionCommand("images are loaded");
    updateWhenLoaded.setRepeats(false); // occurs only one time
    updateWhenLoaded.start();
  }