1 package com
.github
.puzzles
.gui
;
4 import java
.awt
.event
.MouseAdapter
;
5 import java
.awt
.event
.MouseEvent
;
7 import javax
.swing
.JLabel
;
8 import javax
.swing
.JOptionPane
;
9 import javax
.swing
.JPanel
;
11 public class FlippingPuzzlePanel
extends JPanel
{
12 final private int rows
;
13 final private int cols
;
14 private Cell puzzlePanels
[][];
15 MainWindow mainWindow
;
17 public class Cell
extends AbstractCellPuzzlePanel
{
22 private static final long serialVersionUID
= 7439563525463254651L;
24 public Cell(int xIndex
, int yIndex
) {
25 super(xIndex
, yIndex
);
28 addMouseListener(new MouseAdapter() {
31 public void mouseReleased(MouseEvent e
) {
32 FlippingPuzzlePanel
.this.mainWindow
.getFlippingPuzzle().flip(getXIndex(), getYIndex());
33 FlippingPuzzlePanel
.this.paint();
34 count
.setText("Count : " + mainWindow
.getFlippingPuzzle().getCounter());
36 if(mainWindow
.getFlippingPuzzle().check()){
37 StringBuffer message
= new StringBuffer("Congratz\nYou have won it in " + mainWindow
.getFlippingPuzzle().getCounter() + " time");
38 if(mainWindow
.getFlippingPuzzle().getCounter() > 1)
40 JOptionPane
.showMessageDialog(null, message
);
49 FlippingPuzzlePanel
.this.reset();
56 private static final long serialVersionUID
= 4872124821136999470L;
62 public FlippingPuzzlePanel(MainWindow mainWindow
, int rows
, int cols
) {
65 this.puzzlePanels
= new Cell
[rows
][cols
];
66 this.mainWindow
= mainWindow
;
68 JPanel panel
= new JPanel();
71 count
= new JLabel("Count : " + mainWindow
.getFlippingPuzzle().getCounter());
74 for (int i
= 0; i
< rows
; i
++) {
75 JPanel rowsPanel
= new JPanel();
76 for (int j
= 0; j
< cols
; j
++) {
77 puzzlePanels
[i
][j
] = new Cell(i
, j
);
78 rowsPanel
.add(puzzlePanels
[i
][j
]);
80 this.mainWindow
.getPuzzlePanel().add(rowsPanel
);
87 Boolean
[][] puzzle
= mainWindow
.getFlippingPuzzle().getPuzzle();
88 for(int i
= 0; i
< rows
; i
++)
89 for(int j
= 0; j
< cols
; j
++)
90 puzzlePanels
[i
][j
].setBackground((puzzle
[i
][j
] == true) ? Color
.BLUE
: Color
.RED
);
98 public int getRows() {
102 public int getCols() {
106 public JPanel
[][] getPuzzlePanels() {