I implemented the focused cells changing color, still a lots of work :)
[puzzles.git] / src / com / github / puzzles / gui / FlippingPuzzlePanel.java
blob520257158892a093fd08d0c8e1fb1e1add9ea61b
1 package com.github.puzzles.gui;
3 import java.awt.Color;
4 import java.awt.event.MouseAdapter;
5 import java.awt.event.MouseEvent;
6 import java.util.ArrayList;
7 import java.util.List;
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 {
23 /**
26 private static final long serialVersionUID = 7439563525463254651L;
28 public Cell(int xIndex, int yIndex) {
29 super(xIndex, yIndex);
30 add(new JLabel(" "));
32 addMouseListener(new MouseAdapter() {
34 @Override
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)
48 message.append('s');
49 JOptionPane.showMessageDialog(null, message);
50 mainWindow.reset();
54 @Override
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();
64 @Override
65 public void mouseExited(MouseEvent e) {
66 FlippingPuzzlePanel.this.paint();
67 FlippingPuzzlePanel.this.revalidate();
68 FlippingPuzzlePanel.this.repaint();
70 });
73 public void reset() {
74 FlippingPuzzlePanel.this.reset();
78 /**
81 private static final long serialVersionUID = 4872124821136999470L;
82 private JLabel count;
84 private Cell[] getWillFlip(int x, int y){
85 List<Cell> willFlip = new ArrayList<>();
86 try {
87 willFlip.add(puzzlePanels[y][x]);
88 } catch (ArrayIndexOutOfBoundsException ex) {
89 throw new RectangularPuzzleIndexOutOfBoundsException();
92 try {
93 willFlip.add(puzzlePanels[y][x + 1]);
94 } catch (ArrayIndexOutOfBoundsException ex) {
97 try {
98 willFlip.add(puzzlePanels[y][x - 1]);
99 } catch (ArrayIndexOutOfBoundsException ex) {
102 try {
103 willFlip.add(puzzlePanels[y + 1][x]);
104 } catch (ArrayIndexOutOfBoundsException ex) {
107 try {
108 willFlip.add(puzzlePanels[y - 1][x]);
109 } catch (ArrayIndexOutOfBoundsException ex) {
112 return willFlip.toArray(new Cell[0]);
116 * Create the panel.
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();
125 add(panel);
127 count = new JLabel("Count : "
128 + mainWindow.getFlippingPuzzle().getCounter());
129 panel.add(count);
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);
140 paint();
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++)
147 puzzlePanels[i][j]
148 .setBackground((puzzle[i][j] == true) ? Color.BLUE
149 : Color.RED);
152 public void reset() {
153 puzzlePanels = null;
154 mainWindow.reset();
157 public int getRows() {
158 return rows;
161 public int getCols() {
162 return cols;
165 public JPanel[][] getPuzzlePanels() {
166 return puzzlePanels;