GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — chess-java/BoardDisplay.java at main · lindazeng979/chess-java Expand file tree/ Copy pathBoardDisplay.javaMore file actions 185 lines (166 loc) · 4.79 KB / Copy pathBoardDisplay.javaFile metadata and controls185 lines (166 loc) · 4.79 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 import java.awt.*; import java.awt.event.*; import javax.swing.*; // Used to display the contents of a game board public class BoardDisplay implements ActionListener { private Board board; private JButton[][] grid; private Piece selectedPiece; private Move selectedMove; private JFrame frame; private Color[][] colors; // Constructs a new display for displaying the given board public BoardDisplay(Board board) { this.board = board; grid = new JButton[board.getNumRows()][board.getNumCols()]; colors = new Color[board.getNumRows()][board.getNumCols()]; //Schedule a job for the event-dispatching thread: //creating and showing this application's GUI. SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); //Wait until display has been drawn try { while (frame == null || !frame.isVisible()) Thread.sleep(1); } catch(InterruptedException e) { e.printStackTrace(); System.exit(1); } } /** * Create the GUI and show it. For thread safety, * this method should be invoked from the * event-dispatching thread. */ private void createAndShowGUI() { //Create and set up the window. frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(new GridLayout(board.getNumRows(), board.getNumCols())); //Create each square as a button. for (int row = 0; row < grid.length; row++) for (int col = 0; col < grid[row].length; col++) { grid[row][col] = new JButton(); grid[row][col].setOpaque(true); if ((row + col) % 2 == 0) grid[row][col].setBackground(new Color(155, 145, 115)); else grid[row][col].setBackground(new Color(110, 85, 55)); grid[row][col].setPreferredSize(new Dimension(50, 50)); grid[row][col].setActionCommand(row + "," + col); grid[row][col].addActionListener(this); frame.getContentPane().add(grid[row][col]); } //Show the pieces showBoard(); //Display the window. frame.pack(); frame.setVisible(true); } //Called when a square is clicked. public void actionPerformed(ActionEvent event) { //Determine location of clicked button. String command = event.getActionCommand(); int comma = command.indexOf(","); int row = Integer.parseInt(command.substring(0, comma)); int col = Integer.parseInt(command.substring(comma + 1)); Location loc = new Location(row, col); if (selectedPiece == null) { //we have just selected a piece for the first time. selectedPiece = board.get(loc); clearColors(); if (selectedPiece != null) setColor(loc, Color.YELLOW); } else if (loc.equals(selectedPiece.getLocation())) { //we are deselecting the piece selectedPiece = null; selectedMove = null; clearColors(); } else { //we have selected a move selectedMove = new Move(selectedPiece, loc); setColor(loc, Color.YELLOW); } } //Redraws the board to include the pieces and border colors. public void showBoard() { for (int row = 0; row < grid.length; row++) for (int col = 0; col < grid[row].length; col++) { Location loc = new Location(row, col); Piece piece = board.get(loc); Icon icon = null; if (piece != null) { //System.out.println(loc); grid[row][col].setForeground(piece.getColor()); icon = new ImageIcon(piece.getImageFileName()); } grid[row][col].setIcon(icon); Color color = colors[row][col]; if (color == null) grid[row][col].setBorder(null); else grid[row][col].setBorder(BorderFactory.createLineBorder(color)); } } // Waits for the user to select a move and returns this move. public Move selectMove() { try { selectedPiece = null; selectedMove = null; while (selectedMove == null) Thread.sleep(1); Move move = selectedMove; selectedPiece = null; selectedMove = null; return move; } catch(InterruptedException e) { e.printStackTrace(); System.exit(1); return null; } } // Sets the title of the window. public void setTitle(String title) { frame.setTitle(title); } // Sets the color of the border for the given location, and redraws it. public void setColor(Location loc, Color color) { colors[loc.getRow()][loc.getCol()] = color; showBoard(); } // Clears all border colors and redraws the board. public void clearColors() { for (int row = 0; row < colors.length; row++) for (int col = 0; col < colors[row].length; col++) colors[row][col] = null; showBoard(); } } |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |