-
Notifications
You must be signed in to change notification settings - Fork 0
/
Puzzle.java
208 lines (160 loc) · 5.78 KB
/
Puzzle.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import java.awt.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
import javax.swing.*;
import java.util.*;
import java.io.IOException;
//class Puzzle that encapsulates all the frames
public class Puzzle extends JFrame {
//main method where the execution of the program starts
public static void main(String[] args) {
//creating a frame diaplays the name of the game
JFrame f=new JFrame();
//maximizes the frame automatically
f.setExtendedState( f.getExtendedState()|JFrame.MAXIMIZED_BOTH );
f.setVisible(true);
JButton b=new JButton("NUMBER SLIDING PUZZLE");
b.setFont(new Font("Sans Serif", Font.PLAIN,100));
b.setBackground(Color.lightGray);
b.setForeground(Color.RED);
f.add(b);
//displays the thread (frame) for 2 seconds and then sleeps
try{
Thread.sleep(2000);
}
catch(InterruptedException e)
{
e.printStackTrace();}
f.setVisible(false);
f.dispose();
//frame displays the menu of the game
JFrame frame=new JFrame();
frame.setExtendedState( frame.getExtendedState()|JFrame.MAXIMIZED_BOTH );
frame.setSize(1500,5000);
frame.setVisible(true);
frame.setTitle("");
JButton start=new JButton("START");
start.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
Level ob= new Level();
//invokes leveleasy method in level class
ob.Leveleasy();
}
});
start.setBounds(80,30,120,40);
start.setVisible(true);
JButton level=new JButton("LEVEL");
level.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{ //Another frame for displaying 3 levels
JFrame l=new JFrame();
l.setExtendedState( f.getExtendedState()|JFrame.MAXIMIZED_BOTH );
JButton easy=new JButton("EASY");
easy.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
Level ob =new Level();
//invokes leveleasy() present in Levelclass
ob.Leveleasy();
}
});
easy.setBounds(80,30,120,40);
easy.setVisible(true);
l.add(easy);
JButton medium=new JButton("MEDIUM");
medium.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
Level ob =new Level();
//invokes levelmedium() present in Levelclass
ob.Levelmedium();
}
});
medium.setBounds(80,70,120,40);
medium.setVisible(true);
l.add(medium);
JButton hard=new JButton("HARD");
hard.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
Level ob =new Level();
//invokes levelhard() present in Levelclass
ob.Levelhard();
}
});
hard.setBounds(80,100,120,40);
hard.setVisible(true);
l.add(hard);
easy.setFont(new Font("Sans Serif", Font.PLAIN, 60));
easy.setBackground(Color.WHITE);
easy.setForeground(Color.RED);
medium.setFont(new Font("Sans Serif", Font.PLAIN, 60));
medium.setBackground(Color.WHITE);
medium.setForeground(Color.RED);
hard.setFont(new Font("Sans Serif", Font.PLAIN, 60));
hard.setBackground(Color.WHITE);
hard.setForeground(Color.RED);
l.setSize(200,200);
l.setTitle("");
l.dispose();
l.pack();
l.setVisible(true);
l.setLayout(new GridLayout(3,1,20,0));
}});
level.setBounds(80,70,120,40);
level.setVisible(true);
JButton instructions=new JButton("INSTRUCTIONS");
instructions.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{ //Another frame for displaying instructions of game
JFrame p= new JFrame();
p.setExtendedState( p.getExtendedState()|JFrame.MAXIMIZED_BOTH );
//creating another GUI component to display information
JTextArea textArea = new JTextArea();
textArea.setText("\n\n\n\n PUZZLE GAME INSTRUCTIONS\n\n\n 1.Click on the button adjacent to the Empty Button so that the button will be replaced by the empty slot\n\n\n 2.Arrange all the numbers in ascending order(1 2 3 4 ...)\n\n\n 3.Arrange them in given time or else the game will be stopped");
Font font = new Font("Sans Serif", Font.BOLD, 20);
//seteditable sets the editing prorperty to be true/false
textArea.setEditable(false);
textArea.setFont(font);
textArea.setForeground(Color.BLACK);
p.add(textArea);
p.dispose();
p.pack();
p.setVisible(true);
}
});
instructions.setBounds(80,100,120,40);
instructions.setVisible(true);
JButton quit=new JButton("QUIT");
quit.setBounds(80,150,120,40);
quit.addActionListener(e -> {
int selectedOption = JOptionPane.showConfirmDialog(null,
"Are you sure to quit the game?",
"Choose",
JOptionPane.YES_NO_OPTION);
if (selectedOption == JOptionPane.YES_OPTION) {
System.exit(0);
}
});
start.setFont(new Font("Sans Serif", Font.PLAIN, 60));
start.setBackground(Color.WHITE);
start.setForeground(Color.RED);
level.setFont(new Font("Sans Serif", Font.PLAIN, 60));
level.setBackground(Color.WHITE);
level.setForeground(Color.RED);
instructions.setFont(new Font("Sans Serif", Font.PLAIN, 60));
instructions.setBackground(Color.WHITE);
instructions.setForeground(Color.RED);
quit.setFont(new Font("Sans Serif", Font.PLAIN, 60));
quit.setBackground(Color.WHITE);
quit.setForeground(Color.RED);
//adding all the buttons to main frame
frame.add(start);
frame.add(level);
frame.add(instructions);
frame.add(quit);
frame.setLayout(new GridLayout(4,1,20,0));
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}