1 package com
.github
.puzzles
.gui
;
4 import java
.awt
.event
.MouseAdapter
;
5 import java
.awt
.event
.MouseEvent
;
6 import java
.util
.ArrayList
;
9 import javax
.swing
.JLabel
;
10 import javax
.swing
.JOptionPane
;
11 import javax
.swing
.JPanel
;
13 import com
.github
.puzzles
.core
.RectangularPuzzleIndexOutOfBoundsException
;
15 public class FlippingPuzzlePanel
extends JPanel
{
16 final private int rows
;
17 final private int cols
;
18 private Cell puzzlePanels
[][];
19 MainWindow mainWindow
;
21 public class Cell
extends AbstractCellPuzzlePanel
{
26 private static final long serialVersionUID
= 7439563525463254651L;
28 public Cell(int xIndex
, int yIndex
) {
29 super(xIndex
, yIndex
);
32 addMouseListener(new MouseAdapter() {
35 public void mouseReleased(MouseEvent e
) {
36 FlippingPuzzlePanel
.this.mainWindow
.getFlippingPuzzle()
37 .flip(getXIndex(), getYIndex());
38 FlippingPuzzlePanel
.this.paint();
39 count
.setText("Count : "
40 + mainWindow
.getFlippingPuzzle().getCounter());
42 if (mainWindow
.getFlippingPuzzle().check()) {
43 StringBuffer message
= new StringBuffer(
44 "Congratz\nYou have won it in "
45 + mainWindow
.getFlippingPuzzle()
46 .getCounter() + " time");
47 if (mainWindow
.getFlippingPuzzle().getCounter() > 1)
49 JOptionPane
.showMessageDialog(null, message
);
55 public void mouseEntered(MouseEvent e
) {
56 Cell
[] cells
= FlippingPuzzlePanel
.this.getWillFlip(getXIndex(), getYIndex());
57 for(Cell cell
: cells
)
58 cell
.setBackground(Color
.GREEN
);
60 FlippingPuzzlePanel
.this.revalidate();
61 FlippingPuzzlePanel
.this.repaint();
65 public void mouseExited(MouseEvent e
) {
66 FlippingPuzzlePanel
.this.paint();
67 FlippingPuzzlePanel
.this.revalidate();
68 FlippingPuzzlePanel
.this.repaint();
74 FlippingPuzzlePanel
.this.reset();
81 private static final long serialVersionUID
= 4872124821136999470L;
84 private Cell
[] getWillFlip(int x
, int y
){
85 List
<Cell
> willFlip
= new ArrayList
<>();
87 willFlip
.add(puzzlePanels
[y
][x
]);
88 } catch (ArrayIndexOutOfBoundsException ex
) {
89 throw new RectangularPuzzleIndexOutOfBoundsException();
93 willFlip
.add(puzzlePanels
[y
][x
+ 1]);
94 } catch (ArrayIndexOutOfBoundsException ex
) {
98 willFlip
.add(puzzlePanels
[y
][x
- 1]);
99 } catch (ArrayIndexOutOfBoundsException ex
) {
103 willFlip
.add(puzzlePanels
[y
+ 1][x
]);
104 } catch (ArrayIndexOutOfBoundsException ex
) {
108 willFlip
.add(puzzlePanels
[y
- 1][x
]);
109 } catch (ArrayIndexOutOfBoundsException ex
) {
112 return willFlip
.toArray(new Cell
[0]);
118 public FlippingPuzzlePanel(MainWindow mainWindow
) {
119 this.rows
= mainWindow
.getFlippingPuzzle().getWidth();
120 this.cols
= mainWindow
.getFlippingPuzzle().getHeight();
121 this.puzzlePanels
= new Cell
[rows
][cols
];
122 this.mainWindow
= mainWindow
;
124 JPanel panel
= new JPanel();
127 count
= new JLabel("Count : "
128 + mainWindow
.getFlippingPuzzle().getCounter());
131 for (int i
= 0; i
< rows
; i
++) {
132 JPanel rowsPanel
= new JPanel();
133 for (int j
= 0; j
< cols
; j
++) {
134 puzzlePanels
[i
][j
] = new Cell(j
, i
);
135 rowsPanel
.add(puzzlePanels
[i
][j
]);
137 this.mainWindow
.getPuzzlePanel().add(rowsPanel
);
143 private void paint() {
144 Boolean
[][] puzzle
= mainWindow
.getFlippingPuzzle().getPuzzle();
145 for (int i
= 0; i
< rows
; i
++)
146 for (int j
= 0; j
< cols
; j
++)
148 .setBackground((puzzle
[i
][j
] == true) ? Color
.BLUE
152 public void reset() {
157 public int getRows() {
161 public int getCols() {
165 public JPanel
[][] getPuzzlePanels() {