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(); }
@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()); } }
//////////////////////////////////////////////////////////// // 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(); }
// ---------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()); } }
//////////////////////////////////////////////////////////// // OVERRIDEN //////////////////////////////////////////////////////////// @Override public void actionPerformed(ActionEvent e) { // System.out.println( "timer event (1) !"); if (e.getActionCommand().equals("remote bead animator")) { availableBeads[currentBead - 1].animation(5); int tolerance = 5; if (availableBeads[currentBead - 1].getY() > (int) (pegs[insertedInPegNum - 1].getBounds().getMaxY() - availableBeads[currentBead - 1].getBounds().getHeight() - (availableBeads[currentBead - 1].getBounds().getHeight() + tolerance) * (pegs[insertedInPegNum - 1].amountOfBeads2() - 1))) { // here the remote player finished moving remoteBeadAnimTimer.stop(); // keep reading from refereee!!!!!! try { receiveFromReferee(); } catch (InterruptedException e1) { e1.printStackTrace(); } } // end if repaint(); // happens every time the animationTimer triggers an event } else if (e.getActionCommand().equals("images are loaded")) { // images are loaded, then just paint them // this only happens one time after user // clicked in new game menu repaint(); } else if (e.getActionCommand().equals("is win_lose")) { // images are loaded, then just paint them // this only happens one time after user // clicked in new game menu repaint(); startImageLoader(LOSE_IMAGE_FILE); updateWhenLoaded.stop(); } else if (e.getActionCommand().equals("Gertrude vs Computer")) { // starts the worker that receives from refereee try { receiveFromReferee(); // this is a worker } catch (InterruptedException e1) { System.out.println("COuldnt receive from referee"); } } else if (e.getActionCommand().equals("animation")) { availableBeads[currentBead - 1].animation(5); int tolerance = 5; if (availableBeads[currentBead - 1].getY() > pegs[insertedInPegNum - 1].getBounds().getMaxY() - availableBeads[currentBead - 1].getBounds().getHeight() - (availableBeads[currentBead - 1].getBounds().getWidth() + tolerance) * (pegs[insertedInPegNum - 1].amountOfBeads2() - 1)) { animationTimer.stop(); // send to the referee the user's move System.out.println("GUI, I sent " + insertedInPegNum); sendToReferee(Integer.toString(insertedInPegNum)); // here the local player finished moving a piece, // now its turn of the remote player to go! // starts the worker that receives from refereee try { receiveFromReferee(); // this is a worker } catch (InterruptedException e1) { System.out.println("Couldnt receive from referee"); } } // end if repaint(); } // end if-else }