Skip to content

Commit d2ee8f3

Browse files
Merge pull request #43 from thuongtruong1009/refactor
fix: refactor root
2 parents 2cf556f + 82975bf commit d2ee8f3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+758
-393
lines changed

refer.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
background.png: https://i3.ytimg.com/vi/UpGCxdTXfSY/hqdefault.jpg
2+
background2.jpg : https://image.winudf.com/v2/image1/Y29tLnN5bHBoYm94LmNsYXNzaWMuc25ha2Vfc2NyZWVuXzBfMTYwMjUwNzU0NF8wNjA/screen-0.jpg?fakeurl=1&type=.jpg
3+
java: https://www.oracle.com/a/tech/img/rc10-java-badge-3.png

src/backgrounds/background1.jpeg

310 KB

src/backgrounds/background2.jpg

38.5 KB

src/backgrounds/background3.jpg

33.7 KB

src/backgrounds/background4.jpg

1.78 MB

src/backgrounds/background5.jpg

53.1 KB
File renamed without changes.
File renamed without changes.
File renamed without changes.

src/image/background.jpg

-13.5 KB
Binary file not shown.

src/image/bug.ico

316 KB
Binary file not shown.

src/image/bug.png

1.57 KB

src/image/contact.ico

297 KB
Binary file not shown.

src/image/contact.png

1.84 KB

src/image/difficult.ico

149 KB
Binary file not shown.

src/image/difficult.png

1.2 KB

src/image/discuss.ico

22 KB
Binary file not shown.

src/image/discuss.png

1.33 KB

src/image/easy.ico

148 KB
Binary file not shown.

src/image/easy.png

995 Bytes

src/image/file.ico

275 KB
Binary file not shown.

src/image/file.png

1.86 KB

src/image/help.ico

175 KB
Binary file not shown.

src/image/help.png

1.87 KB

src/image/high_score.png

1.08 KB

src/image/how_to_paly.ico

177 KB
Binary file not shown.

src/image/level.ico

187 KB
Binary file not shown.

src/image/level.png

956 Bytes

src/image/level2.png

2.05 KB

src/image/license.ico

167 KB
Binary file not shown.

src/image/license.png

1.05 KB

src/image/love_icon.png

1.43 KB

src/image/medium.ico

147 KB
Binary file not shown.

src/image/medium.png

1.41 KB

src/image/more.ico

159 KB
Binary file not shown.

src/image/more.png

1.59 KB

src/image/more2.png

2.03 KB

src/image/new_game.ico

171 KB
Binary file not shown.

src/image/new_game.png

2.25 KB

src/image/quit.ico

221 KB
Binary file not shown.

src/image/quit.png

2.59 KB

src/image/refer.txt

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
bug: https://icon-icons.com/download/9156/PNG/32/
2+
contact: https://icon-icons.com/icon/add-contact-twitter-user/127131
3+
help: https://icon-icons.com/download/14267/PNG/32/
4+
easy: https://icon-icons.com/download/35019/PNG/32/
5+
medium: https://icon-icons.com/download/34935/PNG/32/
6+
difficult: https://icon-icons.com/download/35026/PNG/32/
7+
quit: https://icon-icons.com/download/106118/PNG/32/
8+
license: https://icon-icons.com/download/35763/PNG/32/
9+
file: https://icon-icons.com/download/106991/PNG/32/
10+
new game: https://icon-icons.com/download/13225/PNG/32/
11+
level: https://icon-icons.com/download/83772/PNG/32/
12+
level2: https://icon-icons.com/download/102343/PNG/32/
13+
more: https://icon-icons.com/download/79906/PNG/32/
14+
more2: https://icon-icons.com/download/81375/PNG/32/

src/image/snake_icon.png

30.7 KB

src/image/snake_image.png

-34.1 KB
Binary file not shown.

src/music/ting toong.m4r

-20.7 KB
Binary file not shown.
File renamed without changes.
File renamed without changes.
File renamed without changes.

src/root/IRoot.java

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package root;
2+
3+
public interface IRoot {
4+
5+
}

src/root/Root.java

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package root;
2+
3+
import javax.swing.ImageIcon;
4+
5+
public class Root {
6+
public ImageIcon icon = new ImageIcon("./src/image/snake_icon.png");
7+
8+
public ImageIcon ic1 = new ImageIcon("./src/image/high_score.png");
9+
public ImageIcon ic2 = new ImageIcon("./src/image/new_game.png");
10+
public ImageIcon ic3 = new ImageIcon("./src/image/quit.png");
11+
12+
public ImageIcon icn1 = new ImageIcon("./src/image/discuss.png");
13+
public ImageIcon icn2 = new ImageIcon("./src/image/license.png");
14+
public ImageIcon icn3 = new ImageIcon("./src/image/contact.png");
15+
16+
public ImageIcon Icn1 = new ImageIcon("./src/image/help.png");
17+
public ImageIcon Icn2 = new ImageIcon("./src/image/bug.png");
18+
public ImageIcon Icn3 = new ImageIcon("./src/image/love_icon.png");
19+
20+
//Images Path In Array
21+
public String[] list = {
22+
"./src/backgrounds/background1.jpeg",
23+
"./src/backgrounds/background2.jpg",
24+
"./src/backgrounds/background3.jpg",
25+
"./src/backgrounds/background4.jpg",
26+
"./src/backgrounds/background5.jpg",
27+
"./src/backgrounds/background1.jpeg"
28+
};
29+
30+
public String[] array2 = {"./src/image/easy.png", "./src/image/medium.png", "./src/image/difficult.png"};
31+
32+
public String contact = "mailto:ititiu19228@student.hcmiu.edu.vn";
33+
public String discuss = "https://github.com/thuongtruong1009/Snake-Game-OOP/discuss";
34+
public String bug = "https://github.com/thuongtruong1009/Snake-Game-OOP/issues";
35+
public String how_to_play = "https://github.com/thuongtruong1009/Snake-Game-OOP/README.md";
36+
}

src/thuongtruong1009/Frame.java

+269
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,269 @@
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

Comments
 (0)