From 0dce84aaf0251824c626c177a703cd1e3f48b77d Mon Sep 17 00:00:00 2001 From: La VloZ Date: Fri, 31 Jan 2014 05:15:21 +0100 Subject: [PATCH] I've made the GUI for flipping puzzle, Cancel button still need fixing in the flipping puzzle dialog :) --- src/com/github/puzzles/core/AbstractPuzzle.java | 2 +- .../puzzles/core/AbstractRectangularPuzzle.java | 2 + src/com/github/puzzles/core/FlippingPuzzle.java | 12 +-- .../puzzles/core/InvalidPuzzleSizeException.java | 10 +++ .../puzzles/core/NegativePuzzleSizeException.java | 10 +++ ...NoEmptyCaseAdjacentSlidingPuzzleException.java} | 2 +- src/com/github/puzzles/core/SlidingPuzzle.java | 2 +- ...zzlePanel.java => AbstractCellPuzzlePanel.java} | 8 +- .../github/puzzles/gui/FlippingPuzzleDialog.java | 66 ++++++++++----- .../github/puzzles/gui/FlippingPuzzlePanel.java | 97 +++++++++++++++++++++- src/com/github/puzzles/gui/MainWindow.java | 54 ++++++------ 11 files changed, 203 insertions(+), 62 deletions(-) create mode 100644 src/com/github/puzzles/core/InvalidPuzzleSizeException.java create mode 100644 src/com/github/puzzles/core/NegativePuzzleSizeException.java rename src/com/github/puzzles/core/{NoEmptyCaseAdjacentSlidingPuzzleExecption.java => NoEmptyCaseAdjacentSlidingPuzzleException.java} (84%) rename src/com/github/puzzles/gui/{AbstractPuzzlePanel.java => AbstractCellPuzzlePanel.java} (65%) diff --git a/src/com/github/puzzles/core/AbstractPuzzle.java b/src/com/github/puzzles/core/AbstractPuzzle.java index 2d0627f..e11c2b8 100644 --- a/src/com/github/puzzles/core/AbstractPuzzle.java +++ b/src/com/github/puzzles/core/AbstractPuzzle.java @@ -34,7 +34,7 @@ public abstract class AbstractPuzzle { * @return the counter. */ protected int incrementCounter() { - return --counter; + return ++counter; } /** diff --git a/src/com/github/puzzles/core/AbstractRectangularPuzzle.java b/src/com/github/puzzles/core/AbstractRectangularPuzzle.java index 4503f5c..d84a61c 100644 --- a/src/com/github/puzzles/core/AbstractRectangularPuzzle.java +++ b/src/com/github/puzzles/core/AbstractRectangularPuzzle.java @@ -21,6 +21,8 @@ public abstract class AbstractRectangularPuzzle extends AbstractPuzzle { this.height = Matrices.getHeight(puzzle); this.puzzle = puzzle; this.correctPuzzle = makeCorrectPuzzle(); + if(width < 0 || height < 0) + throw new InvalidPuzzleSizeException(); } protected AbstractRectangularPuzzle( diff --git a/src/com/github/puzzles/core/FlippingPuzzle.java b/src/com/github/puzzles/core/FlippingPuzzle.java index 22c56a6..5677dab 100644 --- a/src/com/github/puzzles/core/FlippingPuzzle.java +++ b/src/com/github/puzzles/core/FlippingPuzzle.java @@ -78,28 +78,28 @@ public class FlippingPuzzle extends AbstractRectangularPuzzle public void flip(int y, int x) { try { puzzle[y][x] = toggle(puzzle[y][x]); - } catch (RectangularPuzzleIndexOutOfBoundsException ex) { - throw ex; + } catch (ArrayIndexOutOfBoundsException ex) { + throw new RectangularPuzzleIndexOutOfBoundsException(); } try { puzzle[y][x + 1] = toggle(puzzle[y][x + 1]); - } catch (RectangularPuzzleIndexOutOfBoundsException ex) { + } catch (ArrayIndexOutOfBoundsException ex) { } try { puzzle[y][x - 1] = toggle(puzzle[y][x - 1]); - } catch (RectangularPuzzleIndexOutOfBoundsException ex) { + } catch (ArrayIndexOutOfBoundsException ex) { } try { puzzle[y + 1][x] = toggle(puzzle[y + 1][x]); - } catch (RectangularPuzzleIndexOutOfBoundsException ex) { + } catch (ArrayIndexOutOfBoundsException ex) { } try { puzzle[y - 1][x] = toggle(puzzle[y - 1][x]); - } catch (RectangularPuzzleIndexOutOfBoundsException ex) { + } catch (ArrayIndexOutOfBoundsException ex) { } incrementCounter(); diff --git a/src/com/github/puzzles/core/InvalidPuzzleSizeException.java b/src/com/github/puzzles/core/InvalidPuzzleSizeException.java new file mode 100644 index 0000000..04340e9 --- /dev/null +++ b/src/com/github/puzzles/core/InvalidPuzzleSizeException.java @@ -0,0 +1,10 @@ +package com.github.puzzles.core; + +public class InvalidPuzzleSizeException extends RuntimeException { + + /** + * + */ + private static final long serialVersionUID = -6404963499585790729L; + +} diff --git a/src/com/github/puzzles/core/NegativePuzzleSizeException.java b/src/com/github/puzzles/core/NegativePuzzleSizeException.java new file mode 100644 index 0000000..a4a3a93 --- /dev/null +++ b/src/com/github/puzzles/core/NegativePuzzleSizeException.java @@ -0,0 +1,10 @@ +package com.github.puzzles.core; + +public class NegativePuzzleSizeException extends InvalidPuzzleSizeException { + + /** + * + */ + private static final long serialVersionUID = 5002961665921487804L; + +} diff --git a/src/com/github/puzzles/core/NoEmptyCaseAdjacentSlidingPuzzleExecption.java b/src/com/github/puzzles/core/NoEmptyCaseAdjacentSlidingPuzzleException.java similarity index 84% rename from src/com/github/puzzles/core/NoEmptyCaseAdjacentSlidingPuzzleExecption.java rename to src/com/github/puzzles/core/NoEmptyCaseAdjacentSlidingPuzzleException.java index f5fd652..059d7e1 100644 --- a/src/com/github/puzzles/core/NoEmptyCaseAdjacentSlidingPuzzleExecption.java +++ b/src/com/github/puzzles/core/NoEmptyCaseAdjacentSlidingPuzzleException.java @@ -10,7 +10,7 @@ package com.github.puzzles.core; * * @author root */ -public class NoEmptyCaseAdjacentSlidingPuzzleExecption extends RuntimeException { +public class NoEmptyCaseAdjacentSlidingPuzzleException extends RuntimeException { /** * diff --git a/src/com/github/puzzles/core/SlidingPuzzle.java b/src/com/github/puzzles/core/SlidingPuzzle.java index 5d428da..f683e75 100644 --- a/src/com/github/puzzles/core/SlidingPuzzle.java +++ b/src/com/github/puzzles/core/SlidingPuzzle.java @@ -137,6 +137,6 @@ public class SlidingPuzzle extends AbstractRectangularPuzzle implements } catch (ArrayIndexOutOfBoundsException ex) { } - throw new NoEmptyCaseAdjacentSlidingPuzzleExecption(); + throw new NoEmptyCaseAdjacentSlidingPuzzleException(); } } diff --git a/src/com/github/puzzles/gui/AbstractPuzzlePanel.java b/src/com/github/puzzles/gui/AbstractCellPuzzlePanel.java similarity index 65% rename from src/com/github/puzzles/gui/AbstractPuzzlePanel.java rename to src/com/github/puzzles/gui/AbstractCellPuzzlePanel.java index 50ec8c4..0e9c43d 100644 --- a/src/com/github/puzzles/gui/AbstractPuzzlePanel.java +++ b/src/com/github/puzzles/gui/AbstractCellPuzzlePanel.java @@ -2,7 +2,7 @@ package com.github.puzzles.gui; import javax.swing.JPanel; -abstract public class AbstractPuzzlePanel extends JPanel { +abstract public class AbstractCellPuzzlePanel extends JPanel { /** * @@ -15,16 +15,16 @@ abstract public class AbstractPuzzlePanel extends JPanel { /** * Create the panel. */ - public AbstractPuzzlePanel(int xIndex, int yIndex) { + public AbstractCellPuzzlePanel(int xIndex, int yIndex) { this.xIndex = xIndex; this.yIndex = yIndex; } - public int getxIndex() { + public int getXIndex() { return xIndex; } - public int getyIndex() { + public int getYIndex() { return yIndex; } diff --git a/src/com/github/puzzles/gui/FlippingPuzzleDialog.java b/src/com/github/puzzles/gui/FlippingPuzzleDialog.java index 9fc81db..5a5837e 100644 --- a/src/com/github/puzzles/gui/FlippingPuzzleDialog.java +++ b/src/com/github/puzzles/gui/FlippingPuzzleDialog.java @@ -14,6 +14,7 @@ import com.github.puzzles.core.FlippingPuzzle; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; +import javax.swing.JLayeredPane; public class FlippingPuzzleDialog extends JDialog { @@ -25,9 +26,11 @@ public class FlippingPuzzleDialog extends JDialog { private JTextField rowsText; private JTextField colsText; private JLabel rowsNumberMessage; - private JLabel rowsErrorMessage; + private JLabel rowsNotANumberErrorMessage; private JLabel colsNumberMessage; - private JLabel colsErrorMessage; + private JLabel colsNotANumberErrorMessage; + private JLabel colsNegativeNumberErrorMessage; + private JLabel rowsNegativeNumberErrorMessage; /** * Launch the application. @@ -61,11 +64,13 @@ public class FlippingPuzzleDialog extends JDialog { panel.add(rowsText); rowsText.setColumns(10); } - { - rowsErrorMessage = new JLabel("* should be a number."); - rowsErrorMessage.setVisible(false); - panel.add(rowsErrorMessage); - } + rowsNotANumberErrorMessage = new JLabel("* should be a number."); + panel.add(rowsNotANumberErrorMessage); + + rowsNegativeNumberErrorMessage = new JLabel("* should be a positif value."); + rowsNegativeNumberErrorMessage.setVisible(false); + panel.add(rowsNegativeNumberErrorMessage); + rowsNotANumberErrorMessage.setVisible(false); } JPanel panel = new JPanel(); @@ -80,9 +85,14 @@ public class FlippingPuzzleDialog extends JDialog { colsText.setColumns(10); } { - colsErrorMessage = new JLabel("* should be a number"); - colsErrorMessage.setVisible(false); - panel.add(colsErrorMessage); + colsNotANumberErrorMessage = new JLabel("* should be a number"); + colsNotANumberErrorMessage.setVisible(false); + panel.add(colsNotANumberErrorMessage); + } + { + colsNegativeNumberErrorMessage = new JLabel("* should be a positif value."); + colsNegativeNumberErrorMessage.setVisible(false); + panel.add(colsNegativeNumberErrorMessage); } { JPanel buttonPane = new JPanel(); @@ -96,29 +106,39 @@ public class FlippingPuzzleDialog extends JDialog { int rows = 0; int cols = 0; boolean error = false; - rowsErrorMessage.setVisible(false); - colsErrorMessage.setVisible(false); + + rowsNotANumberErrorMessage.setVisible(false); + rowsNegativeNumberErrorMessage.setVisible(false); + colsNotANumberErrorMessage.setVisible(false); + colsNegativeNumberErrorMessage.setVisible(false); try { rows = Integer.parseInt(rowsText.getText()); - error = true; + if(rows <= 0){ + error = true; + rowsNegativeNumberErrorMessage.setVisible(true); + } } catch (NumberFormatException e1) { - rowsErrorMessage.setVisible(true); + rowsNotANumberErrorMessage.setVisible(true); + error = true; } + try { cols = Integer.parseInt(colsText.getText()); - error = true; + if(cols <= 0){ + error = true; + colsNegativeNumberErrorMessage.setVisible(true); + } } catch (NumberFormatException e1) { - colsErrorMessage.setVisible(true); + colsNotANumberErrorMessage.setVisible(true); + error = true; } - if (error) { + if (!error) { + mainWindow.reset(); mainWindow.setFlippingPuzzle(new FlippingPuzzle( rows, cols)); FlippingPuzzleDialog.this.dispose(); } - - FlippingPuzzleDialog.this.dispose(); - } }); okButton.setActionCommand("OK"); @@ -138,4 +158,10 @@ public class FlippingPuzzleDialog extends JDialog { } } } + public JLabel getColsNegativeNumberErrorMessage() { + return colsNegativeNumberErrorMessage; + } + public JLabel getRowsNegativeNumberErrorMessage() { + return rowsNegativeNumberErrorMessage; + } } diff --git a/src/com/github/puzzles/gui/FlippingPuzzlePanel.java b/src/com/github/puzzles/gui/FlippingPuzzlePanel.java index 7f792e5..121a8f1 100644 --- a/src/com/github/puzzles/gui/FlippingPuzzlePanel.java +++ b/src/com/github/puzzles/gui/FlippingPuzzlePanel.java @@ -1,18 +1,109 @@ package com.github.puzzles.gui; +import java.awt.Color; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; -public class FlippingPuzzlePanel extends AbstractPuzzlePanel { +import javax.swing.JLabel; +import javax.swing.JOptionPane; +import javax.swing.JPanel; + +public class FlippingPuzzlePanel extends JPanel { + final private int rows; + final private int cols; + private Cell puzzlePanels[][]; + MainWindow mainWindow; + + public class Cell extends AbstractCellPuzzlePanel { + + /** + * + */ + private static final long serialVersionUID = 7439563525463254651L; + + public Cell(int xIndex, int yIndex) { + super(xIndex, yIndex); + add(new JLabel(" ")); + + addMouseListener(new MouseAdapter() { + + @Override + public void mouseReleased(MouseEvent e) { + FlippingPuzzlePanel.this.mainWindow.getFlippingPuzzle().flip(getXIndex(), getYIndex()); + FlippingPuzzlePanel.this.paint(); + count.setText("Count : " + mainWindow.getFlippingPuzzle().getCounter()); + + if(mainWindow.getFlippingPuzzle().check()){ + StringBuffer message = new StringBuffer("Congratz\nYou have won it in " + mainWindow.getFlippingPuzzle().getCounter() + " time"); + if(mainWindow.getFlippingPuzzle().getCounter() > 1) + message.append('s'); + JOptionPane.showMessageDialog(null, message); + mainWindow.reset(); + } + } + + }); + } + + public void reset(){ + FlippingPuzzlePanel.this.reset(); + } + } /** * */ private static final long serialVersionUID = 4872124821136999470L; + private JLabel count; /** * Create the panel. */ - public FlippingPuzzlePanel(int xIndex, int yIndex) { - super(xIndex, yIndex); + public FlippingPuzzlePanel(MainWindow mainWindow, int rows, int cols) { + this.rows = rows; + this.cols = cols; + this.puzzlePanels = new Cell[rows][cols]; + this.mainWindow = mainWindow; + + JPanel panel = new JPanel(); + add(panel); + + count = new JLabel("Count : " + mainWindow.getFlippingPuzzle().getCounter()); + panel.add(count); + for (int i = 0; i < rows; i++) { + JPanel rowsPanel = new JPanel(); + for (int j = 0; j < cols; j++) { + puzzlePanels[i][j] = new Cell(i, j); + rowsPanel.add(puzzlePanels[i][j]); + } + this.mainWindow.getPuzzlePanel().add(rowsPanel); + } + + paint(); + } + + private void paint(){ + Boolean [][] puzzle = mainWindow.getFlippingPuzzle().getPuzzle(); + for(int i = 0; i < rows; i++) + for(int j = 0; j < cols; j++) + puzzlePanels[i][j].setBackground((puzzle[i][j] == true) ? Color.BLUE : Color.RED); + } + + public void reset(){ + puzzlePanels = null; + mainWindow.reset(); + } + + public int getRows() { + return rows; + } + + public int getCols() { + return cols; } + + public JPanel[][] getPuzzlePanels() { + return puzzlePanels; + } } diff --git a/src/com/github/puzzles/gui/MainWindow.java b/src/com/github/puzzles/gui/MainWindow.java index 1a0ce85..686025c 100644 --- a/src/com/github/puzzles/gui/MainWindow.java +++ b/src/com/github/puzzles/gui/MainWindow.java @@ -8,7 +8,6 @@ import java.awt.event.MouseEvent; import javax.swing.BoxLayout; import javax.swing.JDialog; import javax.swing.JFrame; -import javax.swing.JLabel; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; @@ -16,7 +15,6 @@ import javax.swing.JPanel; import com.github.puzzles.core.FlippingPuzzle; import com.github.puzzles.core.SlidingPuzzle; -import java.awt.Color; public class MainWindow { @@ -65,13 +63,14 @@ public class MainWindow { JMenu menuFile = new JMenu("File"); topMenuBar.add(menuFile); - JMenu newPuzzleMenu = new JMenu("New puzzle"); + final JMenu newPuzzleMenu = new JMenu("New puzzle"); menuFile.add(newPuzzleMenu); JMenuItem flippingPuzzle = new JMenuItem("Flipping Puzzle"); flippingPuzzle.addMouseListener(new MouseAdapter() { @Override public void mouseReleased(MouseEvent e) { + JDialog flippingPuzzleDialog = new FlippingPuzzleDialog( MainWindow.this); flippingPuzzleDialog @@ -79,28 +78,15 @@ public class MainWindow { flippingPuzzleDialog.setAlwaysOnTop(true); flippingPuzzleDialog.setModal(true); flippingPuzzleDialog.setVisible(true); - int rowsNumber = MainWindow.this.flippingPuzzle.getWidth(); int colsNumber = MainWindow.this.flippingPuzzle.getHeight(); + + //* - //for (int i = 0; i < rowsNumber; i++) { - //* - - //*/ - //} - JPanel panel = new JPanel(); - panel.setBackground(Color.RED); - /* - for (int i = 0; i < rowsNumber; i++) { - JPanel rowsPanel = new JPanel(); - for (int j = 0; j < colsNumber; j++) { - FlippingPuzzlePanel cell = new FlippingPuzzlePanel(j, j); - cell.add(new JLabel("Label")); - cell.setBackground(Color.RED); - rowsPanel.add(cell); - } - puzzlePanel.add(rowsPanel); - } + puzzlePanel.add(new FlippingPuzzlePanel(MainWindow.this, rowsNumber, colsNumber)); + + frame.revalidate(); + frame.repaint(); //*/ } }); @@ -110,7 +96,6 @@ public class MainWindow { slidingPuzzle.addMouseListener(new MouseAdapter() { @Override public void mouseReleased(MouseEvent e) { - // * JDialog slidingPuzzleDialog = new SlidingPuzzleDialog( MainWindow.this); slidingPuzzleDialog @@ -119,7 +104,6 @@ public class MainWindow { slidingPuzzleDialog.setModal(true); slidingPuzzleDialog.setVisible(true); - // */ } }); newPuzzleMenu.add(slidingPuzzle); @@ -131,8 +115,6 @@ public class MainWindow { aboutMenu.addMouseListener(new MouseAdapter() { @Override public void mouseReleased(MouseEvent e) { - // JFrame aboutFrame = new AboutFrame(); - // aboutFrame.setVisible(true); JDialog aboutMeDialog = new AboutMeDialog(); aboutMeDialog.setAlwaysOnTop(true); aboutMeDialog.setModal(true); @@ -150,6 +132,18 @@ public class MainWindow { puzzlePanel = new JPanel(); mainPanel.add(puzzlePanel); puzzlePanel.setLayout(new BoxLayout(puzzlePanel, BoxLayout.PAGE_AXIS)); + + } + + public void reset(){ + flippingPuzzle = null; + sliddingPuzzle = null; + //frame.remove(puzzlePanel); + //frame.removeAll(); + puzzlePanel.removeAll(); + frame.revalidate(); + frame.repaint(); + System.gc(); } public FlippingPuzzle getFlippingPuzzle() { @@ -175,4 +169,12 @@ public class MainWindow { public JPanel getPanel() { return puzzlePanel; } + + public JPanel getPuzzlePanel() { + return puzzlePanel; + } + + public void setPuzzlePanel(JPanel puzzlePanel) { + this.puzzlePanel = puzzlePanel; + } } -- 2.11.4.GIT