Ejemplo n.º 1
0
  public boolean queensideClear() {
    boolean[] flags = ChessGameDemo.getFlags();
    int row = square.getRow();
    int column = square.getColumn();
    if ((row == 1) && (flags[0] || flags[4])) {
      return false;
    }
    if ((row == 8) && (flags[2] || flags[5])) {
      return false;
    }
    if (((row == 1) && (column == 5)) || ((row == 8) && (column == 5))) {
      for (int i = 1; i < 4; i++) {
        int r = row;
        int c = column - i;
        int cell = square.getCellFromCoord(r, c);
        Square sq = (Square) board.getComponent(cell);
        if ((sq.getComponentCount() == 1) || (r < 1)) {
          return false;
        } else if (color == 1) {
          if ((board.getBlackCheckList()).contains(sq.getPosition())) {
            return false;
          }
        } else {
          if ((board.getWhiteCheckList()).contains(sq.getPosition())) {
            return false;
          }
        }
      }
      return true;
    }

    return false;
  }
Ejemplo n.º 2
0
  public String castleQueenside(Square candidateSquare) {
    if (piece == 0) {

      int row;
      int column;
      if (color == 1) {
        row = 1;
        column = 1;
      } else {
        row = 8;
        column = 1;
      }
      int cell = square.getCellFromCoord(row, column);
      Square sq = (Square) board.getComponent(cell);
      Piece pc = (Piece) sq.getComponent(0);

      int newCell;
      if (pc.getColor() == 1) {
        newCell = square.getCellFromCoord(1, 4);
      } else {
        newCell = square.getCellFromCoord(8, 4);
      }
      Square newSquare = (Square) board.getComponent(newCell);
      // move rook
      movePiece(pc, newSquare, sq);
      return ("0-0-0");
    }
    return "";
  }
Ejemplo n.º 3
0
 public BoardPanel(
     final JFrame frame,
     Game game,
     UnitTypeConfiguration unitTypeConfiguration,
     TileTypeConfiguration tileTypeConfiguration,
     TileStateConfiguration tileStateConfiguration,
     ChessMovementStrategy movementStrategy,
     UnitSelectorMode unitSelectorMode,
     BattleStrategyConfiguration battleStrategyConfiguration) {
   this.frame = frame;
   this.battleStrategyConfiguration = battleStrategyConfiguration;
   thisPanel = this;
   this.game = game;
   game.addGameObserver(this);
   this.board = game.getBoard();
   this.movementStrategy = movementStrategy;
   int rows = board.getDimension().getHeight();
   int columns = board.getDimension().getWidth();
   setLayout(new GridLayout(rows, columns));
   for (Tile tile : board.getTiles()) {
     final TilePanel tilePanel =
         new TilePanel(tileTypeConfiguration, unitTypeConfiguration, tileStateConfiguration, tile);
     if (UnitSelectorMode.MULTIPLE.equals(unitSelectorMode)) {
       tilePanel.addMouseListener(
           new MultipleUnitSelectorMouseListener(tilePanel, frame, thisPanel));
     }
     if (UnitSelectorMode.SINGLE.equals(unitSelectorMode)) {
       tilePanel.addMouseListener(
           new SingleUnitSelectorMouseListener(tilePanel, frame, thisPanel));
     }
     map.put(tile, tilePanel);
     add(tilePanel);
   }
   resetPositions();
 }
Ejemplo n.º 4
0
  /** Pastes Items from the Clipboard on the Board */
  public void paste() {

    ComponentSelection clipboardContent = (ComponentSelection) tbe.getClipboard().getContents(this);

    if ((clipboardContent != null)
        && (clipboardContent.isDataFlavorSupported(ComponentSelection.itemFlavor))) {

      Object[] tempItems = null;
      try {
        tempItems =
            board.cloneItems(clipboardContent.getTransferData(ComponentSelection.itemFlavor));
      } catch (UnsupportedFlavorException e1) {

        e1.printStackTrace();
      }
      ItemComponent[] items = new ItemComponent[tempItems.length];
      for (int i = 0; i < tempItems.length; i++) {
        items[i] = (ItemComponent) tempItems[i];
      }
      PasteCommand del = new PasteCommand(items);
      ArrayList<Command> actCommands = new ArrayList<Command>();
      actCommands.add(del);
      tbe.addCommands(actCommands);
      board.addItem(items);
    }
  }
Ejemplo n.º 5
0
  /**
   * Sets the current Tool, the right listeners and the Cursor
   *
   * @param tool, Tool to set as current
   * @param button, JButton to set as current
   */
  public void setTool(Tool tool, JButton button) {
    // IF NO CURSORTOOL
    if (this.currentTool instanceof CursorTool && !(tool instanceof CursorTool)) {
      board.removeMouseListener(listeners[0]);

      // IF CURSORTOOL
    } else if (tool instanceof CursorTool && !(this.currentTool instanceof CursorTool)) {

      board.addMouseListener(listeners[0]);
    }
    if (tool == null) throw new IllegalArgumentException("Tool must not be null.");

    if (this.currentTool != tool) {
      if (this.currentButton != null) {
        this.currentButton.setEnabled(true);
      }
      this.currentButton = button;
      this.currentTool = tool;
    }
    if (tool instanceof CursorTool || tool instanceof ArrowTool || tool instanceof TextBoxTool) {
      board.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
    } else {
      board.setCursor(tool.getItemType().getCursor());
    }
  }
Ejemplo n.º 6
0
  public void actionPerformed(ActionEvent ae) {

    if (ae.getSource() == this.buttonStart && !animate) {
      animate = true;
      animatorThread = new Thread(animator);
      animatorThread.start();
    }

    if (ae.getSource() == this.buttonStop && animate) {
      animate = false;
      animatorThread = null;
    }

    if (ae.getSource() == this.buttonNextRound && !animate) {
      board.nextRound();
      numOfGenerations++;
      grid.setBoard(board.getCurrentRound());
      grid.repaint();
      generationsLabel.setText("Generation " + numOfGenerations);
    }

    if (ae.getSource() == this.buttonClearBoard && !animate) {
      board.clear();
      this.numOfGenerations = 0;
      grid.repaint();
      generationsLabel.setText("Generation " + numOfGenerations);
    }

    if (ae.getSource() == this.drawingCheckBox) {
      draw = drawingCheckBox.isSelected();
    }
  }
Ejemplo n.º 7
0
 private void setListForPawn() {
   moveList.clear();
   if (color == 1) {
     int row = square.getRow();
     int column = square.getColumn();
     int cell1 = square.getCellFromCoord(row + 1, column);
     int cell2 = square.getCellFromCoord(row + 2, column);
     int cell3 = square.getCellFromCoord(row + 1, column + 1);
     int cell4 = square.getCellFromCoord(row + 1, column - 1);
     Square s1 = (Square) board.getComponent(cell1);
     Square s2 = (Square) board.getComponent(cell2);
     Square s3 = (Square) board.getComponent(cell3);
     Square s4 = (Square) board.getComponent(cell4);
     if (s1.getComponentCount() == 0) {
       moveList.add(s1.getPosition());
       squareList.add(s1);
     }
     if (s2.getComponentCount() == 0) {
       moveList.add(s2.getPosition());
       squareList.add(s2);
     }
     if ((s3.getComponentCount() == 1) && ((Piece) s3.getComponent(0)).getColor() == 0) {
       moveList.add(s3.getPosition());
       squareList.add(s3);
     }
     if ((s4.getComponentCount() == 1) && ((Piece) s4.getComponent(0)).getColor() == 0) {
       moveList.add(s4.getPosition());
       squareList.add(s4);
     }
   }
   if (color == 0) {
     int row = square.getRow();
     int column = square.getColumn();
     int cell1 = square.getCellFromCoord(row - 1, column);
     int cell2 = square.getCellFromCoord(row - 2, column);
     int cell3 = square.getCellFromCoord(row - 1, column + 1);
     int cell4 = square.getCellFromCoord(row - 1, column - 1);
     Square s1 = (Square) board.getComponent(cell1);
     Square s2 = (Square) board.getComponent(cell2);
     Square s3 = (Square) board.getComponent(cell3);
     Square s4 = (Square) board.getComponent(cell4);
     if (s1.getComponentCount() == 0) {
       moveList.add(s1.getPosition());
       squareList.add(s1);
     }
     if (s2.getComponentCount() == 0) {
       moveList.add(s2.getPosition());
       squareList.add(s2);
     }
     if ((s3.getComponentCount() == 1) && ((Piece) s3.getComponent(0)).getColor() == 1) {
       moveList.add(s3.getPosition());
       squareList.add(s3);
     }
     if ((s4.getComponentCount() == 1) && ((Piece) s4.getComponent(0)).getColor() == 1) {
       moveList.add(s4.getPosition());
       squareList.add(s4);
     }
   }
 }
Ejemplo n.º 8
0
 public Main() {
   dimension = new Dimension(WINDOW_WIDTH * SPRITE_SIZE, WINDOW_HEIGHT * SPRITE_SIZE);
   board = new Board();
   board.setPreferredSize(dimension);
   board.setMinimumSize(dimension);
   initUI();
   pack();
 }
Ejemplo n.º 9
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());
    }
  }
Ejemplo n.º 10
0
  /** Deletes the selected Item of the Board and creates a DeleteCommand */
  public void delete() {

    ItemComponent[] items = board.getSelectedItems();
    DeleteCommand del = new DeleteCommand(items);
    ArrayList<Command> actCommands = new ArrayList<Command>();
    actCommands.add(del);
    tbe.addCommands(actCommands);
    board.removeItem(items);
  }
Ejemplo n.º 11
0
  /**
   * Constructor for open a Board
   *
   * @param board
   */
  public WorkingView(Board board) {
    this.sport = board.getSport();
    this.board = board;
    tbe.setSaved(true);

    int value = board.getPath().lastIndexOf("\\");
    tbe.getFrame().setTitle("TBE - Tactic Board Editor - " + board.getPath().substring(value + 1));
    createWorkingView();
  }
Ejemplo n.º 12
0
 /**
  * Helper method for actionPerformed. Iterates through the spaces and determines which space was
  * clicked on.
  *
  * @param event the click of a space
  * @return a (file, rank) pair corresponding to the space
  */
 public Pair<Character, Integer> findSource(ActionEvent event) {
   for (int y = board.getYDimension(); y >= 1; y--) {
     for (char x = 'a'; x < ('a' + board.getXDimension()); x++) {
       if (event.getSource() == gui.getSpaceAt(x, y)) {
         return new Pair<Character, Integer>(x, y);
       }
     }
   }
   return null;
 }
Ejemplo n.º 13
0
  // -----------draws names on screen-----------
  private void displayNames(Graphics g) {

    // draw players names
    drawText(g, "Player 1", TEXT_P1_INITIAL_X, TEXT_P1_INITIAL_Y + 4, Color.green, 22);
    drawText(g, board.player1(), TEXT_P1_INITIAL_X, TEXT_P1_INITIAL_Y, Color.blue, 22);
    drawText(g, "Player 2", TEXT_P2_INITIAL_X, TEXT_P2_INITIAL_Y + 4, Color.green, 22);
    drawText(g, board.player2(), TEXT_P2_INITIAL_X, TEXT_P2_INITIAL_Y, Color.blue, 22);
    drawText(g, "___________", BEAD_TEXT_INITIAL_X, BEAD_TEXT_INITIAL_Y - 4, Color.BLACK, 17);
    drawText(g, " Free Bead", BEAD_TEXT_INITIAL_X, BEAD_TEXT_INITIAL_Y, Color.BLACK, 17);
  }
Ejemplo n.º 14
0
  /** Creates the WorkingView */
  private void createWorkingView() {
    workingViewLabels = getResourceBundle(tbe.getLang());
    this.setLayout(new BorderLayout());
    this.setBackground(Color.WHITE);
    Invoker.getInstance().clear();

    // Toolbar
    this.add(toolbar, BorderLayout.NORTH);

    // Attributebar
    sideBar = new SideBar(board);
    this.add(sideBar, BorderLayout.WEST);

    // gemeinsames Panel für Board und Legend
    rightPanel.setLayout(new BorderLayout());

    rightPanel.add(new JScrollPane(board), BorderLayout.CENTER);
    class ViewMouseListener extends MouseAdapter {
      public void mousePressed(MouseEvent e) {
        if (e.getButton() == 3 && !(currentTool instanceof CursorTool)) {
          setTool(cursorTool, cursorButton);
        } else {
          Point p = new Point(e.getX(), e.getY());
          WorkingView.this.getTool().mouseDown(p.x, p.y, e);
        }
        if (currentTool instanceof ArrowTool || currentTool instanceof TextBoxTool) {
          setTool(cursorTool, cursorButton);
        }
        board.requestFocus();
      }

      public void mouseReleased(MouseEvent e) {

        checkDefaultButtonVisibility();
      }
    }
    initDefaultTools();

    initSportTools();

    listeners[0] = board.getMouseListeners()[0];
    listeners[1] = new ViewMouseListener();
    board.addMouseListener(listeners[1]);

    // Legend
    legendBar = new LegendBar(board);
    rightPanel.add(legendBar, BorderLayout.SOUTH);

    this.add(rightPanel, BorderLayout.CENTER);
    this.activatePoints(false);

    tbe.getMenu().setVisibleToolbar(!this.toolbar.isVisible());
    tbe.getMenu().setVisibleLegend(!this.legendBar.isVisible());
    tbe.getMenu().setVisibleSidebar(!this.sideBar.isVisible());
  }
Ejemplo n.º 15
0
  /** Cuts selected Iems of the Board and put it into the ClipBoard */
  public void cut() {

    ItemComponent[] items = board.getSelectedItems();
    CutCommand cut = new CutCommand(items);
    ArrayList<Command> actCommands = new ArrayList<Command>();
    actCommands.add(cut);
    tbe.addCommands(actCommands);
    ComponentSelection contents = new ComponentSelection(this.getBoard().cloneItems(items));
    tbe.getClipboard().setContents(contents, cut);
    board.removeItem(items);
  }
Ejemplo n.º 16
0
  public void prepareGUI() {
    mainFrame = new JFrame("Game of life");
    mainFrame.setSize(750, 670);
    mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    grid = new GridPanel(board.getNumOfRows(), board.getNumOfColumns());
    grid.setPreferredSize(new Dimension(600, 600));
    grid.setBoard(this.board.getCurrentRound());
    grid.addMouseListener(this);

    scrollPane = new JScrollPane(grid);
    scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
    scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

    buttonStart = new JButton("Start");
    buttonStop = new JButton("Stop");
    buttonNextRound = new JButton("Next round");
    buttonClearBoard = new JButton("Clear board");

    buttonStart.setMaximumSize(
        new Dimension(Integer.MAX_VALUE, buttonStart.getMinimumSize().height));
    buttonStop.setMaximumSize(new Dimension(Integer.MAX_VALUE, buttonStop.getMinimumSize().height));
    buttonNextRound.setMaximumSize(
        new Dimension(Integer.MAX_VALUE, buttonNextRound.getMinimumSize().height));
    buttonClearBoard.setMaximumSize(
        new Dimension(Integer.MAX_VALUE, buttonClearBoard.getMinimumSize().height));

    buttonStart.addActionListener(this);
    buttonStop.addActionListener(this);
    buttonNextRound.addActionListener(this);
    buttonClearBoard.addActionListener(this);

    generationsLabel = new JLabel("Generation " + numOfGenerations);

    drawingCheckBox = new JCheckBox("Drawing");

    buttonsPanel = new JPanel();
    buttonsPanel.setLayout(new BoxLayout(buttonsPanel, BoxLayout.PAGE_AXIS));
    buttonsPanel.add(buttonStart);
    buttonsPanel.add(Box.createVerticalStrut(2));
    buttonsPanel.add(buttonStop);
    buttonsPanel.add(Box.createVerticalStrut(2));
    buttonsPanel.add(buttonNextRound);
    buttonsPanel.add(Box.createVerticalStrut(2));
    buttonsPanel.add(buttonClearBoard);
    buttonsPanel.add(Box.createVerticalStrut(2));
    buttonsPanel.add(generationsLabel);
    buttonsPanel.add(Box.createVerticalStrut(2));
    buttonsPanel.add(drawingCheckBox);

    mainFrame.add(scrollPane, BorderLayout.CENTER);
    mainFrame.add(buttonsPanel, BorderLayout.EAST);
  }
Ejemplo n.º 17
0
 /**
  * Checks if the Rotate, Add and Remove Buttons should be activated or not. Add and Remove only if
  * Arrow, Rotate only if Shape
  */
 public void checkDefaultButtonVisibility() {
   if (board.getSelectionCount() == 1 && board.getSelectionCell() instanceof ArrowItem) {
     this.activatePoints(true);
   } else {
     this.activatePoints(false);
   }
   if (board.getSelectionCount() == 1 && board.getSelectionCell() instanceof ShapeItem) {
     this.activateRotation(true);
   } else {
     this.activateRotation(false);
   }
 }
Ejemplo n.º 18
0
 // ---------draws winner on screen--------------
 private void displayWinner(Graphics g) {
   if (winner == 1) {
     // System.out.println( "Printing winner "  + 1 );
     drawText(
         g, board.player1() + " wins!", LOSE_WIN_TIE_TEXT_X, LOSE_WIN_TIE_TEXT_Y, Color.red, 38);
   } else if (winner == 2) {
     // System.out.println( "Printing winner "  + 2 );
     drawText(
         g, board.player2() + " wins!", LOSE_WIN_TIE_TEXT_X, LOSE_WIN_TIE_TEXT_Y, Color.red, 38);
   } else if (winner == 0) {
     // System.out.println( "Printing tie" );
     drawText(g, "DRAW", LOSE_WIN_TIE_TEXT_X, LOSE_WIN_TIE_TEXT_Y, Color.red, 38);
   }
 }
Ejemplo n.º 19
0
  public Frame(boolean isStar) {
    super("2048");
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new BorderLayout());
    this.isStar = isStar;

    background = new JLabel(new ImageIcon("Images\\background.png"));
    this.setResizable(false);
    add(background);
    background.setLayout(new FlowLayout());
    newGameButton = new JButton(new ImageIcon("Images\\newGame.png"));
    recordTableButton = new JButton(new ImageIcon("Images\\recordTableButton.png"));
    designButton = new JButton(new ImageIcon("Images\\changeDesign.png"));
    newGameButton.setBorder(BorderFactory.createEmptyBorder());
    newGameButton.setContentAreaFilled(false);
    recordTableButton.setBorder(BorderFactory.createEmptyBorder());
    recordTableButton.setContentAreaFilled(false);
    designButton.setBorder(BorderFactory.createEmptyBorder());
    designButton.setContentAreaFilled(false);
    background.add(newGameButton);
    background.add(recordTableButton);
    background.add(designButton);
    newGameButton.addActionListener(this);
    recordTableButton.addActionListener(this);
    designButton.addActionListener(this);
    this.addKeyListener(this);
    board = new Board(highscores, this);
    this.pack();
    nextImage = new JLabel(new ImageIcon("images\\next2.png"));
    background.add(board.getBoard());
    background.add(nextImage);
    background.add(board.getScoreLabel());
    if (isStar != this.board.getChangedDesign()) {
      this.board.ChaneDesign();

      background.setIcon(new ImageIcon("Images\\Background2.png"));
      newGameButton.setIcon(new ImageIcon("Images\\newGameStar.png"));
      recordTableButton.setIcon(new ImageIcon("Images\\recordTableStarButton.png"));
      designButton.setIcon(new ImageIcon("Images\\changeDesignStar.png"));
      nextImage.setIcon(new ImageIcon("Images\\next2Star.png"));
      this.board.getScoreLabel().setText("Stars collected:" + this.board.getScore().getScore());
    }

    // board.ChaneDesign();

    this.setSize(476, 570);
    this.pack();
    this.setVisible(true);
    this.requestFocusInWindow();
  }
Ejemplo n.º 20
0
 /**
  * Makes it so all buttons are not clickable except for the spaces with pieces of a particular
  * color
  *
  * @param color Pieces of this color can be clicked on
  */
 public void setCurrentSide(int color) {
   for (char file = 'a'; file < ('a' + board.getXDimension()); file++) {
     for (int rank = 1; rank <= board.getYDimension(); rank++) {
       Piece piece = board.at(file, rank);
       JButton space = gui.getSpaceAt(file, rank);
       // check if the space is empty, or if it matches the color parameter
       if (piece != null && piece.getColor() == color) {
         space.setEnabled(true);
       } else {
         space.setEnabled(false);
       }
     }
   }
 }
Ejemplo n.º 21
0
  /** Updates the matrix with current board and current pentomino */
  public void updateMatrix() {
    int[][] tmpBoard = new int[gameBoard.getBoard().length][gameBoard.getBoard()[0].length];
    // Copy the old board to the tmp
    for (int i = 0; i < gameBoard.getBoard().length; i++) {
      System.arraycopy(gameBoard.getBoard()[i], 0, tmpBoard[i], 0, gameBoard.getBoard()[i].length);
    }
    // Add the pentomino to the tmpBoard
    for (Point p : activePentomino.getLocation()) {
      int newX = (int) (pentominoLocation.getX() + p.getX());
      int newY = (int) (pentominoLocation.getY() + p.getY());
      if (newY >= 0) tmpBoard[newX][newY] = activePentomino.getID();
    }

    matrix = tmpBoard;
  }
Ejemplo n.º 22
0
  /** Sets the location for predicted pentomino */
  public void predictDrop() {

    predictedLocation = (Point) pentominoLocation.clone();
    while (nextDropLegal(activePentomino, gameBoard.getBoard(), predictedLocation)) {
      predictedLocation.setLocation(predictedLocation.getX(), predictedLocation.getY() + 1);
    }
  }
Ejemplo n.º 23
0
        @Override
        public void run() {

          while (animate) {
            board.nextRound();
            grid.setBoard(board.getCurrentRound());
            numOfGenerations++;
            grid.repaint();
            generationsLabel.setText("Generation " + numOfGenerations);

            try {
              Thread.sleep(250);
            } catch (InterruptedException e) {
              e.printStackTrace();
            }
          }
        }
Ejemplo n.º 24
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());
    }
  }
Ejemplo n.º 25
0
 /**
  * Constructor for a new Board
  *
  * @param sport
  */
 public WorkingView(Sport sport) {
   this.sport = sport;
   GraphModel model = new DefaultGraphModel();
   GraphLayoutCache view = new GraphLayoutCache(model, new TBECellViewFactory());
   this.board = new Board(model, view, sport);
   board.getDescription().setDescription(sport.getName());
   tbe.setSaved(false);
   createWorkingView();
 }
Ejemplo n.º 26
0
  @Override
  public void mouseReleased(MouseEvent e) {
    if (animate || e.getButton() != MouseEvent.BUTTON1) {
      return;
    }

    int row = e.getY() / 10;
    int column = e.getX() / 10;

    if (row >= board.getNumOfRows() || column >= board.getNumOfColumns()) {
      return;
    }

    toggleCell(new Position(column, row));
    numOfGenerations = 0;

    grid.repaint();
    generationsLabel.setText("Generation " + numOfGenerations);
  }
Ejemplo n.º 27
0
 /**
  * the paintComponent(Graphics g) method paints the view component. It begins by painting the
  * black and white squares, and then iterates throught the board to paint all the pieces on the
  * board.
  */
 @Override
 public void paintComponent(Graphics g) {
   Graphics2D g2 = (Graphics2D) g;
   boolean isBlack = false; // keeps track of whether a square should be painted black or white
   /*
    * This for loop cycles through an 8x8 grid, alternating black and white squares
    */
   for (int i = 0; i < 8; i++) {
     isBlack = !(isBlack);
     for (int j = 0; j < 8; j++) {
       isBlack = !(isBlack);
       Rectangle rect = new Rectangle(i * 62, j * 62, 62, 62);
       if (isBlack) {
         g2.setColor(Color.darkGray);
       } else {
         g2.setColor(Color.white);
       }
       g2.fill(rect);
     }
   }
   /*
    * This for loop cycles through the board and for any board square with a piece,
    * it paints draws the piece.
    */
   for (int i = 0; i < 8; i++) {
     for (int j = 0; j < 8; j++) {
       if (board.hasPiece(i, j)) { // perform draw action if piece exists on board
         board.getSquare(i, j).draw(g2);
       }
     }
   }
   // draws selected pieces on top to ensure they are on the top layer
   for (int i = 0; i < 8; i++) {
     for (int j = 0; j < 8; j++) {
       if (board.hasPiece(i, j)) {
         if (board.getSquare(i, j).isSelected()) {
           board.getSquare(i, j).draw(g2);
         }
       }
     }
   }
 }
Ejemplo n.º 28
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();
  }
Ejemplo n.º 29
0
  /**
   * Activate/Deactivate Rotate-Button
   *
   * @param b
   */
  public void activateRotation(boolean b) {

    if (showRotate && b) {
      rotatePanel.setVisible(b);
      rotateSlider.setValue(((ShapeItem) board.getSelectedItems()[0]).getRotation());

    } else {
      rotatePanel.setVisible(false);
    }
    rotate.setEnabled(b);
  }
Ejemplo n.º 30
0
 private List<TilePanel> getValidMovementPanels(Tile from, Unit unit) {
   List<TilePanel> result = new ArrayList<TilePanel>();
   List<Position> validMoves = movementStrategy.getValidMoves(unit, from.getPosition(), unitMap);
   for (Position validMove : validMoves) {
     TilePanel e = map.get(board.getTileAtPosition(validMove));
     if (e != null) {
       result.add(e);
     }
   }
   return result;
 }