MCPcopy Create free account
hub / github.com/MolinDeng/Princeton-algs4 / BoggleGame

Method BoggleGame

LabEnv/09Lab/BoggleGame.java:138–419  ·  view source on GitHub ↗

Construct the GUI for the Boggle game.

(int rows, int cols)

Source from the content-addressed store, hash-verified

136 * Construct the GUI for the Boggle game.
137 */
138 public BoggleGame(int rows, int cols) {
139 // set the number of rows and columns
140 BOARD_ROWS = rows;
141 BOARD_COLS = cols;
142
143 // this.setPreferredSize(new Dimension(DEF_WIDTH, DEF_HEIGHT));
144 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
145 setTitle("Boggle");
146 setLocationRelativeTo(null);
147 this.makeMenuBar();
148
149 // timer panel
150 JPanel timerPanel = new JPanel();
151 JLabel timerLabel = new JLabel("Timer:");
152 String seconds = String.format("%02d", (GAME_TIME - elapsedTime) % SECONDS_PER_MINUTE);
153 String minutes = String.format("%02d", (GAME_TIME - elapsedTime) / SECONDS_PER_MINUTE);
154 String time = minutes + ":" + seconds;
155 clock = new JLabel(time);
156 timerPanel.add(timerLabel);
157 timerPanel.add(clock);
158
159 // text entry field
160 entryField = new JTextField(DEF_COLUMNS);
161 entryField.setMaximumSize(new Dimension(entryField.getPreferredSize().width,
162 entryField.getPreferredSize().height));
163 entryField.addActionListener(new ActionListener() {
164 @Override
165 public void actionPerformed(ActionEvent e) {
166 checkWord();
167 }
168 });
169 entryField.addKeyListener(new KeyListener() {
170 @Override
171 public void keyPressed(KeyEvent e) { }
172 @Override
173 public void keyReleased(KeyEvent e) {
174 JTextField txtSrc = (JTextField) e.getSource();
175 String text = txtSrc.getText().toUpperCase();
176 bp.matchWord(text);
177 }
178 @Override
179 public void keyTyped(KeyEvent e) { }
180 });
181
182 // list of typed words
183 foundWordsList = new JList();
184 foundWordsList.setPrototypeCellValue(MAX_WORD_SIZE);
185 foundWordsList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
186 foundWordsList.setListData(emptyList);
187 foundWordsList.setVisibleRowCount(FOUND_WORDS_DISPLAY_COUNT);
188 foundWordsList.setLayoutOrientation(JList.VERTICAL_WRAP);
189 foundWordsList.setCellRenderer(new DefaultListCellRenderer() {
190 @Override
191 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
192 Component comp = super.getListCellRendererComponent(list, value, index, false, false);
193 JComponent jc = (JComponent) comp;
194 String word = (String) value;
195 if (!inGame && inGame) {

Callers

nothing calls this directly

Calls 2

makeMenuBarMethod · 0.95
newGameMethod · 0.95

Tested by

no test coverage detected