Пример #1
0
  // ---------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
 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
  @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
  Slideshow(String images[]) {
    // transfer array of images into local array
    imagePaths = images;

    // create the frame and set its size, layout, location, and close operation
    frame = new JFrame("Slideshow");
    frame.setSize(800, 600);
    frame.setLayout(new BorderLayout());
    frame.setLocationRelativeTo(frame);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // create center and play/stop buttons
    centerButton = new JButton("center");
    PlayStopButton = new JButton("Play");

    // set actionListener for timer
    timer = new Timer(seconds, this);
    timer.setActionCommand("timer");

    // set actionListener for the Play/Stop button
    PlayStopButton.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent ae) {
            if (PlayStopButton.getText() == "Play") {
              PlayStopButton.setText("Stop");
              timer.start();
            } else if (PlayStopButton.getText() == "Stop") {
              PlayStopButton.setText("Play");
              timer.stop();
            }
          }
        });

    // add the JLabel to the JScrollPane
    image = new JLabel("");
    scrollPane = new JScrollPane(image);

    // add everything to the frame and display it
    frame.add(scrollPane);
    frame.add(PlayStopButton, BorderLayout.SOUTH);
    frame.setVisible(true);
  }
Пример #5
0
  ////////////////////////////////////////////////////////////
  // 			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();
  }