I've made the GUI for flipping puzzle, Cancel button still need fixing
[puzzles.git] / src / com / github / puzzles / gui / MainWindow.java
blob686025c6b3792633ca36a780cd52a0c23bcb3ff2
1 package com.github.puzzles.gui;
3 import java.awt.EventQueue;
4 import java.awt.FlowLayout;
5 import java.awt.event.MouseAdapter;
6 import java.awt.event.MouseEvent;
8 import javax.swing.BoxLayout;
9 import javax.swing.JDialog;
10 import javax.swing.JFrame;
11 import javax.swing.JMenu;
12 import javax.swing.JMenuBar;
13 import javax.swing.JMenuItem;
14 import javax.swing.JPanel;
16 import com.github.puzzles.core.FlippingPuzzle;
17 import com.github.puzzles.core.SlidingPuzzle;
19 public class MainWindow {
21 private JFrame frame;
22 private FlippingPuzzle flippingPuzzle;
23 private SlidingPuzzle sliddingPuzzle;
24 private JPanel mainPanel;
25 private JPanel puzzlePanel;
27 /**
28 * Launch the application.
30 public static void main(String[] args) {
31 EventQueue.invokeLater(new Runnable() {
32 public void run() {
33 try {
34 MainWindow window = new MainWindow();
35 window.frame.setVisible(true);
36 } catch (Exception e) {
37 e.printStackTrace();
40 });
43 /**
44 * Create the application.
46 public MainWindow() {
47 initialize();
50 /**
51 * Initialize the contents of the frame.
53 private void initialize() {
55 frame = new JFrame();
56 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
58 frame.setBounds(0, 0, 800, 600);
60 JMenuBar topMenuBar = new JMenuBar();
61 frame.setJMenuBar(topMenuBar);
63 JMenu menuFile = new JMenu("File");
64 topMenuBar.add(menuFile);
66 final JMenu newPuzzleMenu = new JMenu("New puzzle");
67 menuFile.add(newPuzzleMenu);
69 JMenuItem flippingPuzzle = new JMenuItem("Flipping Puzzle");
70 flippingPuzzle.addMouseListener(new MouseAdapter() {
71 @Override
72 public void mouseReleased(MouseEvent e) {
74 JDialog flippingPuzzleDialog = new FlippingPuzzleDialog(
75 MainWindow.this);
76 flippingPuzzleDialog
77 .setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
78 flippingPuzzleDialog.setAlwaysOnTop(true);
79 flippingPuzzleDialog.setModal(true);
80 flippingPuzzleDialog.setVisible(true);
81 int rowsNumber = MainWindow.this.flippingPuzzle.getWidth();
82 int colsNumber = MainWindow.this.flippingPuzzle.getHeight();
84 //*
86 puzzlePanel.add(new FlippingPuzzlePanel(MainWindow.this, rowsNumber, colsNumber));
88 frame.revalidate();
89 frame.repaint();
90 //*/
92 });
93 newPuzzleMenu.add(flippingPuzzle);
95 JMenuItem slidingPuzzle = new JMenuItem("Sliding Puzzle");
96 slidingPuzzle.addMouseListener(new MouseAdapter() {
97 @Override
98 public void mouseReleased(MouseEvent e) {
99 JDialog slidingPuzzleDialog = new SlidingPuzzleDialog(
100 MainWindow.this);
101 slidingPuzzleDialog
102 .setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
103 slidingPuzzleDialog.setAlwaysOnTop(true);
104 slidingPuzzleDialog.setModal(true);
105 slidingPuzzleDialog.setVisible(true);
109 newPuzzleMenu.add(slidingPuzzle);
111 JMenu helpMenu = new JMenu("Help");
112 topMenuBar.add(helpMenu);
114 JMenuItem aboutMenu = new JMenuItem("About me");
115 aboutMenu.addMouseListener(new MouseAdapter() {
116 @Override
117 public void mouseReleased(MouseEvent e) {
118 JDialog aboutMeDialog = new AboutMeDialog();
119 aboutMeDialog.setAlwaysOnTop(true);
120 aboutMeDialog.setModal(true);
121 aboutMeDialog.setVisible(true);
124 helpMenu.add(aboutMenu);
125 frame.getContentPane().setLayout(
126 new FlowLayout(FlowLayout.CENTER, 5, 5));
128 mainPanel = new JPanel();
129 frame.getContentPane().add(mainPanel);
130 mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));
132 puzzlePanel = new JPanel();
133 mainPanel.add(puzzlePanel);
134 puzzlePanel.setLayout(new BoxLayout(puzzlePanel, BoxLayout.PAGE_AXIS));
138 public void reset(){
139 flippingPuzzle = null;
140 sliddingPuzzle = null;
141 //frame.remove(puzzlePanel);
142 //frame.removeAll();
143 puzzlePanel.removeAll();
144 frame.revalidate();
145 frame.repaint();
146 System.gc();
149 public FlippingPuzzle getFlippingPuzzle() {
150 return flippingPuzzle;
153 public void setFlippingPuzzle(FlippingPuzzle flippingPuzzle) {
154 this.flippingPuzzle = flippingPuzzle;
157 public SlidingPuzzle getSliddingPuzzle() {
158 return sliddingPuzzle;
161 public void setSliddingPuzzle(SlidingPuzzle sliddingPuzzle) {
162 this.sliddingPuzzle = sliddingPuzzle;
165 public JPanel getMainPanel() {
166 return mainPanel;
169 public JPanel getPanel() {
170 return puzzlePanel;
173 public JPanel getPuzzlePanel() {
174 return puzzlePanel;
177 public void setPuzzlePanel(JPanel puzzlePanel) {
178 this.puzzlePanel = puzzlePanel;