Exported from Notepad++
1 /*
2 * File name: GameView.java
3 * Author: Mostapha A
4 * Purpose: This makes the splash screen and creates/maintains visual components
5 * Class list: Game.java, GameView.java, GameModel.java, GameController.java
6 */
7
8 package piccross;
9
10 import java.awt.BorderLayout;
11 import java.awt.Color;
12 import java.awt.Component;
13 import java.awt.Dimension;
14 import java.awt.GridBagConstraints;
15 import java.awt.GridBagLayout;
16 import java.awt.GridLayout;
17 import java.awt.Toolkit;
18
19 import javax.swing.BorderFactory;
20 import javax.swing.BoxLayout;
21 import javax.swing.ImageIcon;
22 import javax.swing.JButton;
23 import javax.swing.JCheckBox;
24 import javax.swing.JFrame;
25 import javax.swing.JLabel;
26 import javax.swing.JMenu;
27 import javax.swing.JMenuBar;
28 import javax.swing.JMenuItem;
29 import javax.swing.JOptionPane;
30 import javax.swing.JPanel;
31 import javax.swing.JScrollPane;
32 import javax.swing.JTextArea;
33 import javax.swing.JTextField;
34 import javax.swing.JTextPane;
35 import javax.swing.JWindow;
36 import javax.swing.ScrollPaneConstants;
37 import javax.swing.border.Border;
38 import javax.swing.text.SimpleAttributeSet;
39 import javax.swing.text.StyleConstants;
40 import javax.swing.text.StyledDocument;
41
42 import java.awt.event.ActionListener;
43 import java.awt.event.ItemListener;
44
45 /**
46 * GameView class that sets up the splash screen and visual game components
47 *
48 * @author Mostapha Abdelaziz
49 * @version 1.1
50 * @see piccross package, Piccross.java, GameController.java
51 * @since Java 16
52 */
53 public class GameView extends JFrame {
54 /** Required long value */
55 private static final long serialVersionUID = 1L;
56 /** A grey border for use on different elements */
57 private static Border greyBorder = BorderFactory.createMatteBorder(2, 2, 2, 2, new Color(70, 70, 70));
58 /** A blue border for use on different elements */
59 private static Border blueBorder = BorderFactory.createMatteBorder(3, 3, 3, 3, new Color(97, 197, 255));
60 /** A grey background color for use on different elements */
61 private static Color greyBG = new Color(84, 91, 102);
62 /** A light grey background color for use on different elements */
63 private static Color lightGreyBG = new Color(107, 112, 122);
64 /** This is the check box for marking */
65 private JCheckBox mark = new JCheckBox();
66 /** This is the history area, the text box in the control panel */
67 private JTextArea historyArea = new JTextArea();
68 /** The panel that will contain the play area, hint area and mark check box */
69 private static JPanel playArea;
70 /**
71 * The panel that will contain the logo and control panel elements score, time,
72 * history area and reset button
73 */
74 private static JPanel controlPanel = new JPanel();
75 /** The text box for the score */
76 private JTextField score;
77 /** The text box for the time */
78 private JTextField time;
79 /** The array that holds all the play buttons */
80 private JButton[][] playButtons;
81 /** The array that holds all the hint areas */
82 private JPanel[] hintAreas;
83 /** The array that holds the hint texts for the top hints */
84 private JTextPane[] hintTextTop;
85 /** The array that holds the hint texts/labels for the side hints */
86 private JLabel[] hintTextSide;
87 /** Keeps track of the board dimension */
88 private int dimension = 5;
89
90 /**
91 * Default constructor, set the name
92 */
93 GameView() {
94 // set window name
95 super("piccross - mostapha");
96 }
97
98 /**
99 * Sets up all visual components of the piccross game
100 *
101 * @param menuHandler Tha handler for the menu
102 * @param playButtonHandler The handler for the playing buttons
103 * @param resetButtonHandler The handler for the reset button
104 * @param checkBoxHandler The handler for the mark check box
105 */
106 public void startGame(ActionListener menuHandler, ActionListener playButtonHandler,
107 ActionListener resetButtonHandler, ItemListener checkBoxHandler) {
108 // set attributes for main window
109 setSize(987, 710);
110 getContentPane().setBackground(greyBG);
111 setResizable(false);
112
113 // hides window if they close from the window button
114 addWindowListener(new java.awt.event.WindowAdapter() {
115 @Override
116 public void windowClosing(java.awt.event.WindowEvent windowEvent) {
117 setVisible(false);
118 }
119 });
120
121 // set the layout for our main window
122 setLayout(new BorderLayout());
123
124 // set the image for application
125 ImageIcon miniLogo;
126 try {
127 miniLogo = new ImageIcon("images/logomini.png");
128 setIconImage(miniLogo.getImage());
129 } catch (Exception e) {
130 // print error and default image will be displayed
131 e.printStackTrace();
132 }
133
134 // set up menu
135 setUpMenu(menuHandler);
136
137 // call methods that setup the separate panels
138 mark.addItemListener(checkBoxHandler);
139 setUpControlPanel(resetButtonHandler);
140 playArea = new JPanel();
141 setUpPlayingArea(playButtonHandler, checkBoxHandler);
142
143 // add components
144 add(controlPanel, BorderLayout.WEST);
145 add(playArea, BorderLayout.CENTER);
146
147 // set visible as true and center in the screen
148 setVisible(true);
149 setLocationRelativeTo(null);
150 pack();
151 }
152
153 /**
154 * sets the window as visible
155 */
156 public void setVisible() {
157 setVisible(true);
158 }
159
160 /**
161 * Returns whether the mark check box is selected
162 * @return Selected status of mark check box
163 */
164 public boolean checkMark() {
165 return mark.isSelected();
166 }
167
168 /**
169 * Resets a games playing area visual components
170 *
171 * @param playButtonHandler The handler for play buttons
172 * @param checkBoxHandler The handler for the check box
173 */
174 public void newGame(ActionListener playButtonHandler, ItemListener checkBoxHandler) {
175 // remove components to update
176 remove(playArea);
177 playArea.removeAll();
178 // set up playing are again
179 setUpPlayingArea(playButtonHandler, checkBoxHandler);
180 revalidate();
181 repaint();
182 // re add and validate
183 add(playArea, BorderLayout.CENTER);
184 revalidate();
185 repaint();
186 }
187
188 /**
189 * Sets up the menu bar and all it's items
190 *
191 * @param menuHandler The handler for the menu items
192 */
193 public void setUpMenu(ActionListener menuHandler) {
194 // create all the image icons
195 // ImageIcon newGameIcon;
196 ImageIcon solutionIcon;
197 ImageIcon exitIcon;
198 ImageIcon coloursIcon;
199 ImageIcon aboutIcon;
200 /*
201 * new game will be sub menu for different dimensions, no icon try { newGameIcon
202 * = new ImageIcon("images/newgame.gif"); } catch (Exception e) { // print error
203 * and no image will be displayed instead of selected image e.printStackTrace();
204 * newGameIcon = new ImageIcon(); }
205 */
206 try {
207 solutionIcon = new ImageIcon("images/solution.gif");
208 } catch (Exception e) { // print error and no image will be displayed instead of selected image
209 e.printStackTrace();
210 solutionIcon = new ImageIcon();
211 }
212 try {
213 exitIcon = new ImageIcon("images/exit.gif");
214 } catch (Exception e) { // print error and no image will be displayed instead of selected image
215 e.printStackTrace();
216 exitIcon = new ImageIcon();
217 }
218 try {
219 coloursIcon = new ImageIcon("images/colours.gif");
220 } catch (Exception e) { // print error and no image will be displayed instead of selected image
221 e.printStackTrace();
222 coloursIcon = new ImageIcon();
223 }
224 try {
225 aboutIcon = new ImageIcon("images/about.gif");
226 } catch (Exception e) { // print error and no image will be displayed instead of selected image
227 e.printStackTrace();
228 aboutIcon = new ImageIcon();
229 }
230 // Action commands
231 // 13 = 3x3 grid
232 // 15 = 5x5 grid
233 // 110 = 10x10 grid
234 // 2 = solution
235 // 3 = exit
236 // 4 = colours
237 // 5 = about
238
239 // Game menu
240 JMenu gameMenu = new JMenu("Game");
241
242 // create submenu for new game
243 JMenu newGameMenu = new JMenu("New");
244
245 // create new game menu options
246 JMenuItem newGame3 = new JMenuItem("3x3 Game");
247 newGameMenu.add(newGame3);
248 newGame3.setActionCommand("13");
249 newGame3.addActionListener(menuHandler);
250 JMenuItem newGame5 = new JMenuItem("5x5 Game");
251 newGameMenu.add(newGame5);
252 newGame5.setActionCommand("15");
253 newGame5.addActionListener(menuHandler);
254 JMenuItem newGame10 = new JMenuItem("10x10 Game");
255 newGameMenu.add(newGame10);
256 newGame10.setActionCommand("110");
257 newGame10.addActionListener(menuHandler);
258
259 // add new game to game menu
260 gameMenu.add(newGameMenu);
261
262 // create solution menu item - add in Game menu
263 JMenuItem solutionItem = new JMenuItem("Solution", solutionIcon);
264 gameMenu.add(solutionItem);
265 solutionItem.setActionCommand("2");
266 solutionItem.addActionListener(menuHandler);
267
268 // create exit menu item - add in Game menu
269 JMenuItem exitItem = new JMenuItem("Exit", exitIcon);
270 gameMenu.add(exitItem);
271 exitItem.setActionCommand("3");
272 exitItem.addActionListener(menuHandler);
273
274 // create menu bar, add game menu
275 JMenuBar menuBar = new JMenuBar();
276 setJMenuBar(menuBar);
277 menuBar.add(gameMenu);
278
279 // create help menu
280 JMenu helpMenu = new JMenu("Help");
281
282 // create colours menu item - add in Help menu
283 JMenuItem coloursItem = new JMenuItem("Colours", coloursIcon);
284 helpMenu.add(coloursItem);
285 coloursItem.setActionCommand("4");
286 coloursItem.addActionListener(menuHandler);
287
288 // create about menu item - add in Help menu
289 JMenuItem aboutItem = new JMenuItem("About", aboutIcon);
290 helpMenu.add(aboutItem);
291 aboutItem.setActionCommand("5");
292 aboutItem.addActionListener(menuHandler);
293
294 // add help menu to menu bar
295 menuBar.add(helpMenu);
296
297 }
298
299 /**
300 * Adds to the history area a passed through message
301 *
302 * @param message The string to be added to the history area
303 */
304 public void historyAreaMessage(String message) {
305 historyArea.append(message);
306 historyArea.setCaretPosition(historyArea.getDocument().getLength());
307 }
308
309 /**
310 * Changes a buttons colour
311 *
312 * @param column The button's column dimension
313 * @param row The button's row dimension
314 * @param colour The colour to change to
315 */
316 public void changeButton(int column, int row, Color colour) {
317 // change the indicated button to the indicated colour
318 playButtons[column][row].setBackground(colour);
319 }
320
321 /**
322 * Writes the hints in the hint areas
323 *
324 * @param hintsTop The hints to be written in the top hint areas
325 * @param hintsSide The hints to be written in the side hint areas
326 */
327 public void writeHints(Integer[][] hintsTop, Integer[][] hintsSide) {
328 int i = 0;
329 int j = 0;
330 String hint;
331
332 // loop to write hints in the top hint areas
333 for (i = 0; i < dimension; i++) {
334 // reset hint
335 hint = "";
336 for (j = 0; j < 6; j++) {
337 // if the hint is not 0, store at end of string
338 if (hintsTop[i][j] != 0) {
339 hint = hint + hintsTop[i][j].toString() + "\n";
340 } else {
341 // otherwise, hint is 0, put an empty space at the beginning of string
342 hint = " \n" + hint;
343 }
344 }
345 // store all the hints in the corresponding hint label for hint area
346 hintTextTop[i].setText(hint);
347 hintTextTop[i].setBackground(lightGreyBG);
348 hintTextTop[i].setHighlighter(null);
349 hintTextTop[i].setEditable(false);
350 // center the hints
351 StyledDocument doc = hintTextTop[i].getStyledDocument();
352 SimpleAttributeSet center = new SimpleAttributeSet();
353 StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER);
354 doc.setParagraphAttributes(0, doc.getLength(), center, false);
355 }
356
357 // loop to write hints in the side hint areas
358 for (i = 0; i < dimension; i++) {
359 // reset hint
360 hint = "";
361 for (j = 0; j < 6; j++) {
362 // if the hint is not 0, store at end of string
363 if (hintsSide[i][j] != 0) {
364 hint = hint + " " + hintsSide[i][j].toString();
365 } else {
366 // otherwise, hint is 0, put an empty space at the beginning of string
367 hint = " " + hint;
368 }
369 }
370 // store all the hints in the corresponding hint label for hint area
371 hint = hint + " ";
372 hintTextSide[i].setText(hint);
373 hintTextSide[i].setBackground(lightGreyBG);
374 }
375 }
376
377 /**
378 * Resets the history area and score
379 */
380 public void reset() {
381 // set each button colour to white
382 int row = dimension;
383 int column = dimension;
384 for (row = 0; row < dimension; row++) {
385 for (column = 0; column < dimension; column++) {
386 playButtons[column][row].setBackground(Color.WHITE);
387 }
388 }
389 mark.setSelected(false);
390 historyArea.setText("");
391
392 updateScore(0);
393 }
394
395 /**
396 * Updates the score
397 *
398 * @param scoreNum The new score
399 */
400 public void updateScore(Integer scoreNum) {
401 score.setText(scoreNum.toString());
402 }
403
404 /**
405 * Updates the timer
406 *
407 * @param seconds The new time
408 */
409 public void updateTimer(Integer seconds) {
410 time.setText(seconds.toString());
411 }
412
413 /**
414 * Creates and sets up the logo and control panel elements, including score,
415 * time, history text area and reset button
416 */
417 public void setUpControlPanel(ActionListener resetButtonHandler) {
418 // set up the panels and layouts
419 controlPanel.setLayout(new BorderLayout());
420 controlPanel.setBackground(greyBG);
421 controlPanel.setPreferredSize(new Dimension(300, 650));
422 // panel for scrolling history area
423 JPanel historyAreaPanel = new JPanel();
424 historyAreaPanel.setBackground(greyBG);
425 historyAreaPanel.setLayout(new BorderLayout());
426 // panel that will contain logo, and panel that contains the score and time
427 JPanel topArea = new JPanel();
428 topArea.setLayout(new BorderLayout());
429 topArea.setBackground(greyBG);
430 // panel that will contain the score and time
431 JPanel scoreAndTime = new JPanel();
432 scoreAndTime.setLayout(new BorderLayout());
433 scoreAndTime.setBackground(greyBG);
434 // panel that contains the score label and text box
435 JPanel scorePanel = new JPanel();
436 scorePanel.setLayout(new BorderLayout());
437 scorePanel.setBackground(greyBG);
438 scorePanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
439 // panel that contains the time label and text box
440 JPanel timePanel = new JPanel();
441 timePanel.setLayout(new BorderLayout());
442 timePanel.setBackground(greyBG);
443 timePanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
444 // panel that will contain the reset button
445 JPanel resetPanel = new JPanel();
446 resetPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
447 resetPanel.setBackground(greyBG);
448
449 // configure the elements that will be placed in the panels
450 // configure the label for score
451 JLabel labelScore = new JLabel("score ");
452 labelScore.setForeground(Color.white);
453 labelScore.setFont(controlPanel.getFont().deriveFont(25f));
454
455 // configure the score text box
456 score = new JTextField();
457 score.setEditable(false);
458 score.setText("0");
459 score.setBorder(blueBorder);
460 score.setBackground(lightGreyBG);
461 score.setForeground(Color.white);
462 score.setFont(controlPanel.getFont().deriveFont(18f));
463 score.setHorizontalAlignment(JTextField.CENTER);
464
465 // configure the label for the time
466 JLabel labelTime = new JLabel("time ");
467 labelTime.setForeground(Color.white);
468 labelTime.setFont(controlPanel.getFont().deriveFont(25f));
469
470 // configure the text box for the time
471 time = new JTextField();
472 time.setEditable(false);
473 time.setText("time");
474 time.setBorder(blueBorder);
475 time.setBackground(lightGreyBG);
476 time.setForeground(Color.white);
477 time.setFont(controlPanel.getFont().deriveFont(18f));
478 time.setHorizontalAlignment(JTextField.CENTER);
479
480 // configure history area text box
481 historyArea.setEditable(false);
482 historyArea.setFont(controlPanel.getFont().deriveFont(18f));
483 historyArea.setText("");
484 // historyArea.setBorder(blueBorder);
485 historyArea.setBackground(lightGreyBG);
486 historyArea.setForeground(Color.white);
487 // historyArea.setPreferredSize(new Dimension(280, 370));
488
489 // create scrolling pane for history area
490 JScrollPane scrollingHistory = new JScrollPane(historyArea);
491 scrollingHistory.setBackground(greyBG);
492 scrollingHistory.setBorder(blueBorder);
493 // scrollingHistory.setPreferredSize(new Dimension(280, 370));
494 scrollingHistory.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
495
496 // add history area elements
497 historyAreaPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
498 historyAreaPanel.add(scrollingHistory, BorderLayout.CENTER);
499
500 // configure the reset button
501 JButton reset = new JButton();
502 reset.addActionListener(resetButtonHandler);
503 reset.setPreferredSize(new Dimension(100, 40));
504 reset.setAlignmentX(Component.CENTER_ALIGNMENT);
505 reset.setBackground(new Color(84, 91, 102));
506 reset.setForeground(Color.white);
507 reset.setBorder(blueBorder);
508 reset.setText("reset");
509 reset.setFont(controlPanel.getFont().deriveFont(25f));
510
511 // Create and configure a label for the logo image
512 JLabel logo = new JLabel();
513 ImageIcon logoImg;
514 try {
515 logoImg = new ImageIcon("images/logocpl.png");
516 logo.setIcon(logoImg);
517 logo.setPreferredSize(new Dimension(300, 100));
518 } catch (Exception e) {
519 // print error and display no image
520 e.printStackTrace();
521 }
522
523 // add all elements to their respective panels
524 // the score
525 scorePanel.add(labelScore, BorderLayout.WEST);
526 scorePanel.add(score, BorderLayout.CENTER);
527 // the time
528 timePanel.add(labelTime, BorderLayout.WEST);
529 timePanel.add(time, BorderLayout.CENTER);
530 // reset button
531 resetPanel.add(reset);
532
533 // add our element panels to organizational panels
534 scoreAndTime.add(scorePanel, BorderLayout.NORTH);
535 scoreAndTime.add(timePanel, BorderLayout.SOUTH);
536 topArea.add(logo, BorderLayout.NORTH);
537 topArea.add(scoreAndTime, BorderLayout.CENTER);
538
539 // add all the panels to the control panel that will be placed west on main
540 // window
541 controlPanel.add(topArea, BorderLayout.NORTH);
542 controlPanel.add(historyAreaPanel, BorderLayout.CENTER);
543 controlPanel.add(resetPanel, BorderLayout.SOUTH);
544 }
545
546 /**
547 * Changes the boards dimension
548 *
549 * @param newDimension The new dimension
550 */
551 public void setDimension(int newDimension) {
552 dimension = newDimension;
553 }
554
555 /**
556 * Displays a dialog at the end of the game
557 *
558 * @param score The user's score
559 */
560 public void endGame(int score) {
561 // get end game images
562 ImageIcon victory;
563 ImageIcon failure;
564
565 // if perfect score show victory else defeat
566 if (score == dimension * dimension) {
567 try {
568 victory = new ImageIcon("images/gamepicwinner.png");
569 JOptionPane.showMessageDialog(null, null, "Victory!", JOptionPane.INFORMATION_MESSAGE, victory);
570 } catch (Exception e) { // print error and no image will be displayed instead of selected image
571 e.printStackTrace();
572 JOptionPane.showMessageDialog(null, "You won!", "Victory!", JOptionPane.INFORMATION_MESSAGE, null);
573 }
574 } else {
575 try {
576 failure = new ImageIcon("images/gamepicend.png");
577 JOptionPane.showMessageDialog(null, null, "Defeat!", JOptionPane.INFORMATION_MESSAGE, failure);
578 } catch (Exception e) { // print error and no image will be displayed instead of selected image
579 e.printStackTrace();
580 JOptionPane.showMessageDialog(null, "You lost the game!", "Defeat!", JOptionPane.INFORMATION_MESSAGE,
581 null);
582 }
583 }
584 }
585
586 /**
587 * Displays a dialog for about menu item
588 */
589 public void aboutDialog() {
590 // get end game images
591 ImageIcon about;
592
593 try {
594 about = new ImageIcon("images/piccross.png");
595 JOptionPane.showMessageDialog(null, null, "About", JOptionPane.INFORMATION_MESSAGE, about);
596 } catch (Exception e) { // print error and no image will be displayed instead of selected image
597 e.printStackTrace();
598 JOptionPane.showMessageDialog(null, "About", "About", JOptionPane.INFORMATION_MESSAGE, null);
599 }
600 }
601
602 /**
603 * Creates and sets up the mark check box and playing area elements, including
604 * the buttons and hint area
605 */
606 public void setUpPlayingArea(ActionListener playButtonHandler, ItemListener checkBoxHandler) {
607 // array for all the button names/action codes
608 String[] buttonNames;
609 // create an array for all the buttons, size of grid
610 playButtons = new JButton[dimension][dimension];
611 // create a panel to add the buttons to
612 JPanel buttonGrid = new JPanel();
613 buttonGrid.setLayout(new GridLayout(dimension, dimension));
614 // create arrays for the hint areas
615 hintAreas = new JPanel[dimension + dimension];
616 hintTextTop = new JTextPane[dimension];
617 hintTextSide = new JLabel[dimension];
618 // variables for grid size, and looping
619 int i = 0;
620 int row = 0;
621 int column = 0;
622
623 // set the JPanel for the play area attributes
624 playArea.setLayout(new GridBagLayout());
625 playArea.setBackground(greyBG);
626 playArea.setBorder(BorderFactory.createEmptyBorder());
627 // gridbaglayout constraints
628 GridBagConstraints c = new GridBagConstraints();
629
630 // set amount of names
631 buttonNames = new String[dimension * dimension];
632
633 // set up the panel for the mark box
634 JPanel markPanel = new JPanel();
635 // create check box and mark label
636 JLabel markLabel = new JLabel("mark");
637 markLabel.setForeground(Color.white);
638 markLabel.setFont(controlPanel.getFont().deriveFont(25f));
639 // set the item listener and add
640 mark.setBackground(greyBG);
641
642 // add the mark elements to the mark panel
643 markPanel.add(markLabel);
644 markPanel.add(mark);
645 markPanel.setBackground(greyBG);
646
647 // add mark panel to playing area
648 c.gridx = 1;
649 c.gridy = 1;
650 playArea.add(markPanel, c);
651
652 // loop to set the hint area
653 for (i = 0; i < (dimension * 2); i++) {
654 // create a panel
655 JPanel hintArea = new JPanel();
656
657 // set attributes
658 hintArea.setBackground(lightGreyBG);
659
660 // set positioning in play area
661 // if top row
662 if (i < dimension) {
663 hintArea.setBorder(BorderFactory.createMatteBorder(2, 2, 0, 2, greyBG));
664 hintArea.setPreferredSize(new Dimension((500 / dimension), 150));
665 hintArea.setLayout(new BoxLayout(hintArea, BoxLayout.PAGE_AXIS));
666
667 c.gridy = 0; // doesnt change
668 c.gridx = i + 2; // moves over one
669
670 // size
671 c.gridheight = 2;
672 c.gridwidth = 1;
673 } else {
674 // if vertical/side hint area
675 hintArea.setBorder(BorderFactory.createMatteBorder(2, 2, 2, 0, greyBG));
676 hintArea.setPreferredSize(new Dimension(150, (500 / dimension)));
677 hintArea.setLayout(new BorderLayout());
678
679 c.gridx = 0; // doesnt change
680 c.gridy = i + 2 - dimension; // moves down one
681
682 // size
683 c.gridheight = 1;
684 c.gridwidth = 2;
685 }
686 c.fill = GridBagConstraints.BOTH;
687
688 // add text and change postioning depending on top or side
689 if (i < dimension) {
690 // top hints
691 // create and add a text panel to top areas area
692 JTextPane hintLabel = new JTextPane();
693 hintLabel.setFont(controlPanel.getFont().deriveFont(18f));
694 hintLabel.setForeground(Color.WHITE);
695
696 hintLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
697 hintLabel.setAlignmentY(Component.BOTTOM_ALIGNMENT);
698
699 // add to array
700 hintTextTop[i] = hintLabel;
701 // add to JPanel hintArea
702 hintArea.add(hintTextTop[i]);
703 } else {
704 // side hints
705 // create and add a text panel to top areas area
706 JLabel hintLabel = new JLabel("", JLabel.RIGHT);
707 hintLabel.setFont(controlPanel.getFont().deriveFont(18f));
708 hintLabel.setForeground(Color.WHITE);
709
710 hintLabel.setAlignmentX(Component.RIGHT_ALIGNMENT);
711 hintLabel.setAlignmentY(Component.CENTER_ALIGNMENT);
712
713 // add to array
714 hintTextSide[i - dimension] = hintLabel;
715 // add to JPanel hintArea
716 hintArea.add(hintTextSide[i - dimension], BorderLayout.EAST);
717 }
718
719 // add to array list
720 hintAreas[i] = hintArea;
721
722 // add to play area
723 playArea.add(hintAreas[i], c);
724 }
725
726 // loop for grid size and create buttons
727 for (i = 0; i < (dimension * dimension); i++) {
728 // reset column after reaches gridSize
729 if (column >= dimension) {
730 column = 0;
731 row++;
732 }
733
734 // create button name/action
735 buttonNames[i] = (column + 1) + "," + (row + 1);
736
737 // create and add button to array list
738 playButtons[column][row] = createButton(buttonNames[i], playButtonHandler);
739
740 // thicker border for outside edge
741 if (column == 0 && row == 0) {
742 // top left corner
743 playButtons[column][row].setBorder(BorderFactory.createMatteBorder(4, 4, 2, 2, new Color(70, 70, 70)));
744 } else if (column == dimension - 1 && row == 0) {
745 // top right corner
746 playButtons[column][row].setBorder(BorderFactory.createMatteBorder(4, 2, 2, 4, new Color(70, 70, 70)));
747 } else if (column == 0 && row == dimension - 1) {
748 // bottom left corner
749 playButtons[column][row].setBorder(BorderFactory.createMatteBorder(2, 4, 4, 2, new Color(70, 70, 70)));
750 } else if (column == dimension - 1 && row == dimension - 1) {
751 // bottom right corner
752 playButtons[column][row].setBorder(BorderFactory.createMatteBorder(2, 2, 4, 4, new Color(70, 70, 70)));
753 } else if (column == 0) {
754 // left edge
755 playButtons[column][row].setBorder(BorderFactory.createMatteBorder(2, 4, 2, 2, new Color(70, 70, 70)));
756 } else if (row == 0) {
757 // top edge
758 playButtons[column][row].setBorder(BorderFactory.createMatteBorder(4, 2, 2, 2, new Color(70, 70, 70)));
759 } else if (column == dimension - 1) {
760 // right edge
761 playButtons[column][row].setBorder(BorderFactory.createMatteBorder(2, 2, 2, 4, new Color(70, 70, 70)));
762 } else if (row == dimension - 1) {
763 // bottom edge
764 playButtons[column][row].setBorder(BorderFactory.createMatteBorder(2, 2, 4, 2, new Color(70, 70, 70)));
765 }
766
767 // add to panel
768 buttonGrid.add(playButtons[column][row]);
769
770 column++;
771 }
772
773 // add button grid to play area layout
774 // set positions
775 c.gridx = 2;
776 c.gridy = 2;
777
778 // size
779 c.gridheight = dimension;
780 c.gridwidth = dimension;
781 c.fill = GridBagConstraints.BOTH;
782
783 // add to play area
784 playArea.add(buttonGrid, c);
785 }
786
787 /**
788 * Creates a button and sets it up for actions
789 *
790 * @param name The name of the button, or the position
791 * @param buttonHandler The action listener/event handler for the button
792 * @return Returns the created button
793 */
794 public JButton createButton(String name, ActionListener buttonHandler) {
795 // create button
796 JButton button = new JButton();
797
798 // set attributes
799 button.setPreferredSize(new Dimension((500 / dimension), (500 / dimension)));
800 button.setFocusable(false);
801 button.setBackground(Color.white);
802 button.setBorder(greyBorder);
803 // button.setText(name);
804
805 // set the action
806 button.setActionCommand(name);
807 button.addActionListener(buttonHandler);
808
809 // return the created button
810 return button;
811 }
812
813 /**
814 * GameSplash class that sets up the splash screen
815 *
816 * @author Mostapha Abdelaziz
817 * @version 1.0
818 * @see piccross package, Piccross.java
819 * @since Java 16
820 */
821 public static class GameSplash extends JWindow {
822 /** Required long value */
823 private static final long serialVersionUID = 1L;
824
825 /**
826 * Displays a splash screen for 5 seconds
827 */
828 public void showSplashWindow() {
829 // create panel that will hold image
830 JPanel splashPanel = new JPanel(new BorderLayout());
831
832 // create a label to place splash screen image
833 JLabel label;
834 try {
835 label = new JLabel(new ImageIcon("images/loadingscreen.gif"));
836 } catch (Exception e) {
837 // print error and no image will be displayed instead of selected image
838 e.printStackTrace();
839 label = new JLabel("No image");
840 }
841
842 // add the image to our panel
843 splashPanel.add(label, BorderLayout.CENTER);
844
845 // get the size of the screen
846 Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
847
848 // calculate the position to place window in center of screens
849 int x = (screen.width - (600)) / 2;
850 int y = (screen.height - (300)) / 2;
851
852 // set the window size and put in the center of the screen
853 setBounds(x, y, (600), (300));
854
855 // replace the window content with our image in the panel
856 setContentPane(splashPanel);
857
858 // ensure splash window is visible
859 setVisible(true);
860
861 // keep the screen up for 5 seconds
862 try {
863 // splash screen displays for 5000 milliseconds
864 Thread.sleep(5000);
865 } catch (InterruptedException e) {
866 // print error if one is caught
867 e.printStackTrace();
868 System.out.println("Interrupted Exception " + e);
869 }
870
871 // release resources
872 dispose();
873 }
874 }
875 }