/** 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); } }
/** * Creates a new "game" from the current engine.Globals.game variable. While the ANN stays the * same, the speed, actor positions, score, et cetera, are all reset. */ public void newGame() { stopped = true; player.setLives(STARTING_LIVES); player.setScore(0); Graphics g = strategy.getDrawGraphics(); waiting = true; Globals.state = ENEMY_HUNTER_STATE; player.setCenter(board.getPlayerStartPoint()); player.setDirection(Player.DEFAULT_STARTING_DIRECTION); board.reset(); Globals.blipsLeft = Globals.game.getBoard().getBlipCount(); for (Enemy enemy : enemies) { enemy.reset(); } GamePainters.drawBoard(board, g, this); GamePainters.drawEnemies(enemies, g, this); GamePainters.drawPlayer(player, g, this); GamePainters.paintBottomBar(player, board, g, this); strategy.show(); }
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 ""; }
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; }
@Override public void mouseClicked(MouseEvent mouseEvent) { Board b = bgui.getGridView(); if (b.getAction() == Board.Action.ADD && bgui.getSelectedComponent().equals("Gizmo")) { String gizmoShape = bgui.getGizmoShape(); Point mouseP = MouseInfo.getPointerInfo().getLocation(); Point gridP = b.getLocationOnScreen(); int x = mouseP.x - gridP.x; int y = mouseP.y - gridP.y; boolean added = false; switch (gizmoShape) { case "Circle": added = m.addCircularBumper(x, y, 0, "circle"); break; case "Triangle": added = m.addTriangularBumper(x, y, 0, "triangle"); break; case "Square": added = m.addSquareBumper(x, y, 0, "square"); break; case "Teleporter": added = m.addTeleporterBumper(x, y, 0, "teleporter"); break; default: } if (added) { bgui.setMessageColor(Color.GREEN); bgui.setMessage(gizmoShape + " added!"); } else { bgui.setMessageColor(Color.RED); bgui.setMessage("That space is occupied, " + gizmoShape + " cannot be added"); } } }
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(); }
/** * 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()); } }
/** * Paints the graphic component * * @param g Graphic component */ public void paint(Graphics g) { if (environment != null) { Sudoku env = (Sudoku) environment; Board board = env.getBoard(); int n = SudokuLanguage.DIGITS; int sqrt_n = (int) (Math.sqrt(n) + 0.1); g.setColor(Color.lightGray); Font font = g.getFont(); g.setFont(new Font(font.getName(), font.getStyle(), 20)); for (int i = 0; i < n; i++) { int ci = getCanvasValue(i); if (i % sqrt_n == 0) { g.drawLine(ci, DRAW_AREA_SIZE + MARGIN, ci, MARGIN); g.drawLine(DRAW_AREA_SIZE + MARGIN, ci, MARGIN, ci); } for (int j = 0; j < n; j++) { int cj = getCanvasValue(j); int value = board.get(i, j); if (value > 0) { g.setColor(Color.black); g.drawString("" + value, cj + CELL_SIZE / 5, ci + CELL_SIZE); g.setColor(Color.lightGray); } } } g.drawLine(DRAW_AREA_SIZE + MARGIN, DRAW_AREA_SIZE + MARGIN, DRAW_AREA_SIZE + MARGIN, MARGIN); g.drawLine(DRAW_AREA_SIZE + MARGIN, DRAW_AREA_SIZE + MARGIN, MARGIN, DRAW_AREA_SIZE + MARGIN); } }
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); } } }
public void loadGame() { this.stopped = true; this.board = Globals.game.getBoard(); this.enemies = Globals.game.getEnemies(); this.setBounds(0, 0, board.getColumns() * TILE_WIDTH, board.getRows() * TILE_HEIGHT + 20); newGame(); }
public Main() { dimension = new Dimension(WINDOW_WIDTH * SPRITE_SIZE, WINDOW_HEIGHT * SPRITE_SIZE); board = new Board(); board.setPreferredSize(dimension); board.setMinimumSize(dimension); initUI(); pack(); }
/** 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); }
/** * 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(); }
/** * 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; }
/** 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()); }
/** 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); }
public boolean movePiece(Point startLocation, Point endLocation) { Piece piece = b.get(startLocation); if (piece.pieceMovement(startLocation, endLocation) && checkIfPathIsClear(startLocation, endLocation)) { b.put(endLocation, b.get(startLocation)); b.remove(startLocation); piece.setHasNotMoved(false); return true; } return false; }
public boolean validTake(Point start, Point end) { Piece piece = b.get(start); Piece ending = b.get(end); if (ending != null && piece.validCapture(start, end) && checkIfPathIsClear(start, end) && piece.getColor() != ending.getColor()) { return true; } return false; }
/** * 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); } }
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(); }
/** * 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); } } } }
public boolean isKingInCheck() { Point kingsLocation = null; for (Entry<Point, Piece> p : b.entrySet()) { if (p.getValue() instanceof King && isWhitesTurn == p.getValue().getColor()) { kingsLocation = p.getKey(); } } for (Entry<Point, Piece> p : b.entrySet()) { if ((isWhitesTurn != p.getValue().getColor()) && validTake(p.getKey(), kingsLocation)) { return true; } } return false; }
public boolean castling(Point k1, Point k2, Point r1, Point r2) { Piece k = b.get(k1); Piece r = b.get(r1); if (checkIfPathIsClear(k1, r1) && k.pieceMovement(k1, k2) && r.pieceMovement(r1, r2)) { b.put(k2, b.get(k1)); b.put(r2, b.get(r1)); b.remove(k1); b.remove(r1); k.setHasNotMoved(false); r.setHasNotMoved(false); return true; } return false; }
public ArrayList<Point> validMove(Point p) { ArrayList<Point> validMoves = new ArrayList<Point>(); Piece piece = b.get(p); for (int i = 1; i <= 8; i++) { for (int j = 1; j <= 8; j++) { Piece p2 = b.get(new Point(i, j)); if ((p2 == null && piece.pieceMovement(p, new Point(i, j)) || (validTake(p, new Point(i, j)))) && !isKingInCheck() && checkIfPathIsClear(p, new Point(i, j))) { validMoves.add(new Point(i, j)); // System.out.println(p + " " + i + " " + j ); } } } return validMoves; }
/** * 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(); }
/** * 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); } } } } }
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; }
/** * 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); }
public TetrisWindow(String hostName, int serverPortNumber) throws IOException { super("Rainbow Tetris"); connection = new TetrisClient(hostName, serverPortNumber); myID = connection.getID(); board = new Board(); message = new JLabel("Waiting for two players to connect.", JLabel.CENTER); board.setBackground(Color.WHITE); board.setPreferredSize(new Dimension(300, 660)); board.addMouseListener( new MouseAdapter() { public void mousePressed(MouseEvent evt) { doMouseClick(); } }); message.setBackground(Color.LIGHT_GRAY); message.setOpaque(true); JPanel content = new JPanel(); content.setLayout(new BorderLayout(2, 2)); content.setBorder(BorderFactory.createLineBorder(Color.GRAY, 2)); content.setBackground(Color.GRAY); content.add(board, BorderLayout.CENTER); content.add(message, BorderLayout.SOUTH); setContentPane(content); pack(); setResizable(false); setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent evt) { dispose(); connection.disconnect(); try { Thread.sleep(333); } catch (InterruptedException e) { } System.exit(0); } }); setLocation(200, 100); setVisible(true); }
/** * @param enemies An array of the different enemies * @param board The current board (level) */ public LevelCanvas(Enemy[] enemies, Board board) { super(); this.enemies = enemies; this.player = Globals.game.getPlayer(); this.board = board; this.stopped = false; this.waiting = true; this.addKeyListener(this); this.setIgnoreRepaint(true); this.setBounds(0, 0, board.getColumns() * TILE_WIDTH, board.getRows() * TILE_HEIGHT + 20); this.addFocusListener( new FocusListener() { public void focusLost(FocusEvent e) { update(); } public void focusGained(FocusEvent e) { update(); } }); }