1
+ package thuongtruong1009 ;
2
+
3
+ import root .Root ;
4
+
5
+ import java .awt .BorderLayout ;
6
+ import java .awt .Desktop ;
7
+ import java .awt .Dimension ;
8
+ import java .awt .FlowLayout ;
9
+ import java .awt .Image ;
10
+ import java .awt .event .ActionEvent ;
11
+ import java .awt .event .ActionListener ;
12
+ import java .awt .event .KeyEvent ;
13
+ import java .io .BufferedReader ;
14
+ import java .io .FileReader ;
15
+ import java .io .IOException ;
16
+ import java .net .URI ;
17
+ import java .net .URISyntaxException ;
18
+
19
+ import javax .swing .BorderFactory ;
20
+ import javax .swing .ImageIcon ;
21
+ import javax .swing .JFrame ;
22
+ import javax .swing .JLabel ;
23
+ import javax .swing .JMenu ;
24
+ import javax .swing .JMenuBar ;
25
+ import javax .swing .JMenuItem ;
26
+ import javax .swing .JOptionPane ;
27
+ import javax .swing .JPanel ;
28
+ import javax .swing .JScrollPane ;
29
+ import javax .swing .JTextArea ;
30
+ import javax .swing .ScrollPaneConstants ;
31
+ import javax .swing .Timer ;
32
+ /**
33
+ * @author https://www.github.com/thuongtruong1009
34
+ */
35
+ public class Frame extends JPanel {
36
+ JFrame frame = new JFrame ();
37
+ JPanel panel = new JPanel ();
38
+ private static final long serialVersionUID = 1L ;
39
+ static int DELAY =100 ;
40
+ Timer timer ;
41
+ Root root = new Root ();
42
+
43
+ JLabel pic ;
44
+ Timer tm ;
45
+ int x = 0 ;
46
+
47
+
48
+ @ SuppressWarnings ("static-access" )
49
+ public Frame () {
50
+ pic = new JLabel ();
51
+ pic .setBounds (0 , 0 , 500 , 235 );
52
+
53
+ //Call The Function SetImageSize
54
+ SetImageSize (5 );
55
+ //set a timer
56
+ tm = new Timer (1000 ,new ActionListener () {
57
+
58
+ @ Override
59
+ public void actionPerformed (ActionEvent e ) {
60
+ SetImageSize (x );
61
+ x += 1 ;
62
+ if (x >= root .list .length )
63
+ x = 0 ;
64
+ }
65
+ });
66
+ panel .add (pic , BorderLayout .CENTER );
67
+ panel .setLayout (new FlowLayout ());
68
+ tm .start ();
69
+ //
70
+ // ImageIcon image= new ImageIcon("./src/backgrounds/background2.jpg");
71
+ // Image img= image.getImage();
72
+ // Image temp_img= img.getScaledInstance(500, 235, Image.SCALE_SMOOTH);
73
+ // image= new ImageIcon(temp_img);
74
+ //
75
+ // JLabel background= new JLabel("", image, JLabel.CENTER);
76
+ // background.setBounds(0, 0, 500, 300);
77
+ // panel.add(background, BorderLayout.CENTER);
78
+
79
+ JMenuBar menubar = new JMenuBar ();
80
+ frame .add (menubar , BorderLayout .NORTH );
81
+
82
+ //menu1
83
+ JMenu fileMenu = new JMenu ("File" );
84
+ fileMenu .setMnemonic (KeyEvent .VK_F );
85
+ fileMenu .addSeparator ();
86
+ menubar .add (fileMenu );
87
+
88
+ JMenuItem scoreItem = new JMenuItem ("History score" , root .ic1 );
89
+ fileMenu .add (scoreItem );
90
+ scoreItem .addActionListener (new ActionListener () {
91
+ @ Override
92
+ public void actionPerformed (ActionEvent e ) {
93
+ try {
94
+ readFile ("./src/high_score.txt" , "history" );
95
+ } catch (IOException e1 ) {
96
+ e1 .printStackTrace ();
97
+ }
98
+ }
99
+ });
100
+
101
+ JMenuItem newGameItem = new JMenuItem ("New Game" , root .ic2 );
102
+ fileMenu .add (newGameItem );
103
+ newGameItem .addActionListener (new ActionListener () {
104
+ @ Override
105
+ public void actionPerformed (ActionEvent e ) {
106
+ new SetupData ();
107
+ }
108
+ });
109
+
110
+ JMenuItem exitItem = new JMenuItem ("Quit" , root .ic3 );
111
+ fileMenu .add (exitItem );
112
+ exitItem .addActionListener (new ActionListener () {
113
+ @ Override
114
+ public void actionPerformed (ActionEvent e ) {
115
+ System .exit (0 );
116
+ }
117
+ });
118
+
119
+ //menu2
120
+ JMenu levelMenu = new JMenu ("Level" );
121
+ levelMenu .setMnemonic (KeyEvent .VK_L );
122
+ levelMenu .addSeparator ();
123
+ menubar .add (levelMenu );
124
+
125
+ JMenuItem [] menuItem2 = new JMenuItem [3 ];
126
+ String [] array = {"Easy" , "Medium" , "Difficult" };
127
+
128
+ for (int i = 0 ; i <3 ; i ++) {
129
+ int level = i + 1 ;
130
+ ImageIcon ii = new ImageIcon (root .array2 [i ]);
131
+ menuItem2 [i ]= new JMenuItem (array [i ] + " (" + level + ")" , ii );
132
+ menuItem2 [i ].addActionListener (new ActionListener () {
133
+ @ Override
134
+ public void actionPerformed (ActionEvent e ) {
135
+ //menuItem2[i].setBackground(Color.green);
136
+ DELAY = 100 - 20 *level ;
137
+ }
138
+ });
139
+ levelMenu .add (menuItem2 [i ]);
140
+ }
141
+
142
+ //menu3
143
+ JMenu referMenu = new JMenu ("More" );
144
+ referMenu .setMnemonic (KeyEvent .VK_M );
145
+ referMenu .addSeparator ();
146
+ menubar .add (referMenu );
147
+
148
+ JMenuItem discussItem = new JMenuItem ("Discuss" , root .icn1 );
149
+ referMenu .add (discussItem );
150
+ discussItem .addActionListener (new ActionListener () {
151
+ @ Override
152
+ public void actionPerformed (ActionEvent e ) {
153
+ openLink (root .discuss );
154
+ }
155
+ });
156
+
157
+ JMenuItem licenseItem = new JMenuItem ("License" , root .icn2 );
158
+ referMenu .add (licenseItem );
159
+ licenseItem .addActionListener (new ActionListener () {
160
+ @ Override
161
+ public void actionPerformed (ActionEvent e ) {
162
+ try {
163
+ readFile ("./src/LICENSE.txt" , "ECL-2.0 LICENSE" );
164
+ } catch (IOException e1 ) {
165
+ e1 .printStackTrace ();
166
+ }
167
+ }
168
+ });
169
+
170
+ JMenuItem contactItem = new JMenuItem ("Contact" , root .icn3 );
171
+ referMenu .add (contactItem );
172
+ contactItem .addActionListener (new ActionListener () {
173
+ @ Override
174
+ public void actionPerformed (ActionEvent e ) {
175
+ openLink (root .contact );
176
+ }
177
+ });
178
+
179
+ //menu4
180
+ JMenu helpMenu = new JMenu ("Help" );
181
+ helpMenu .setMnemonic (KeyEvent .VK_H );
182
+ helpMenu .addSeparator ();
183
+ menubar .add (helpMenu );
184
+
185
+ JMenuItem tutorialItem = new JMenuItem ("How to play?" , root .Icn1 );
186
+ helpMenu .add (tutorialItem );
187
+ tutorialItem .addActionListener (new ActionListener () {
188
+ @ Override
189
+ public void actionPerformed (ActionEvent e ) {
190
+ openLink (root .how_to_play );
191
+ }
192
+ });
193
+
194
+ JMenuItem reportItem = new JMenuItem ("Report bugs" , root .Icn2 );
195
+ helpMenu .add (reportItem );
196
+ reportItem .addActionListener (new ActionListener () {
197
+ @ Override
198
+ public void actionPerformed (ActionEvent e ) {
199
+ openLink (root .bug );
200
+ }
201
+ });
202
+
203
+ JMenuItem donateItem = new JMenuItem ("Donate" , root .Icn3 );
204
+ helpMenu .add (donateItem );
205
+ donateItem .addActionListener (new ActionListener () {
206
+ @ Override
207
+ public void actionPerformed (ActionEvent e ) {
208
+ try {
209
+ readFile ("./src/LICENSE.txt" , "ECL-2.0 LICENSE" );
210
+ } catch (IOException e1 ) {
211
+ e1 .printStackTrace ();
212
+ }
213
+ }
214
+ });
215
+
216
+ //frame
217
+ frame .add (panel );
218
+ frame .setTitle ("Snake Game OOP - v2.0" );
219
+ frame .setSize (500 , 300 );
220
+ frame .setResizable (false );
221
+ frame .setLocationRelativeTo (null );
222
+ frame .setDefaultCloseOperation (frame .EXIT_ON_CLOSE );
223
+ frame .setVisible (true );
224
+
225
+ Image imageIcon = root .icon .getImage ();
226
+ frame .setIconImage (imageIcon );
227
+ }
228
+
229
+ //create a function to resize the image
230
+ public void SetImageSize (int i ){
231
+ ImageIcon icon = new ImageIcon (root .list [i ]);
232
+ Image img = icon .getImage ();
233
+ Image newImg = img .getScaledInstance (pic .getWidth (), pic .getHeight (), Image .SCALE_SMOOTH );
234
+ ImageIcon newImc = new ImageIcon (newImg );
235
+ pic .setIcon (newImc );
236
+ }
237
+
238
+ public static void readFile (String file , String name )throws IOException {
239
+ BufferedReader br = new BufferedReader (new FileReader (file ));
240
+ String aLineFromFile = null ;
241
+ StringBuilder everything = new StringBuilder ();
242
+ while ((aLineFromFile = br .readLine ()) != null ){
243
+ everything .append (aLineFromFile + "\n " );
244
+ }
245
+ JTextArea textArea = new JTextArea (everything .toString ());
246
+ textArea .setBorder (BorderFactory .createCompoundBorder (textArea .getBorder (), BorderFactory .createEmptyBorder (10 , 10 , 10 , 0 )));
247
+ JScrollPane scrollPane = new JScrollPane (textArea );
248
+ scrollPane .setVerticalScrollBarPolicy (ScrollPaneConstants .VERTICAL_SCROLLBAR_ALWAYS );
249
+ scrollPane .setPreferredSize ( new Dimension ( 570 , 450 ));
250
+
251
+ JOptionPane .showMessageDialog (null , scrollPane , name , JOptionPane .INFORMATION_MESSAGE );
252
+ br .close ();
253
+ return ;
254
+ }
255
+
256
+ public static void openLink (String uri ) {
257
+ try {
258
+ Desktop .getDesktop ().browse (new URI (uri ));
259
+ } catch (IOException | URISyntaxException e ) {
260
+ e .printStackTrace ();
261
+ }
262
+ }
263
+
264
+ public void play () {
265
+ System .out .println ("The speed you choose is: " + DELAY );
266
+ timer = new Timer (DELAY , (ActionListener ) this );
267
+ timer .start ();
268
+ }
269
+ }
0 commit comments