I've made the GUI for flipping puzzle, Cancel button still need fixing
[puzzles.git] / src / com / github / puzzles / gui / FlippingPuzzlePanel.java
blob121a8f16a8a37372dfc0f2948b4a148b542232f1
1 package com.github.puzzles.gui;
3 import java.awt.Color;
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 {
19 /**
22 private static final long serialVersionUID = 7439563525463254651L;
24 public Cell(int xIndex, int yIndex) {
25 super(xIndex, yIndex);
26 add(new JLabel(" "));
28 addMouseListener(new MouseAdapter() {
30 @Override
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)
39 message.append('s');
40 JOptionPane.showMessageDialog(null, message);
41 mainWindow.reset();
45 });
48 public void reset(){
49 FlippingPuzzlePanel.this.reset();
53 /**
56 private static final long serialVersionUID = 4872124821136999470L;
57 private JLabel count;
59 /**
60 * Create the panel.
62 public FlippingPuzzlePanel(MainWindow mainWindow, int rows, int cols) {
63 this.rows = rows;
64 this.cols = cols;
65 this.puzzlePanels = new Cell[rows][cols];
66 this.mainWindow = mainWindow;
68 JPanel panel = new JPanel();
69 add(panel);
71 count = new JLabel("Count : " + mainWindow.getFlippingPuzzle().getCounter());
72 panel.add(count);
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);
83 paint();
86 private void paint(){
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);
93 public void reset(){
94 puzzlePanels = null;
95 mainWindow.reset();
98 public int getRows() {
99 return rows;
102 public int getCols() {
103 return cols;
106 public JPanel[][] getPuzzlePanels() {
107 return puzzlePanels;