Skip to content

Commit

Permalink
Merge pull request #43 from thuongtruong1009/refactor
Browse files Browse the repository at this point in the history
fix: refactor root
  • Loading branch information
thuongtruong1009 authored Dec 7, 2021
2 parents 2cf556f + 82975bf commit d2ee8f3
Show file tree
Hide file tree
Showing 62 changed files with 758 additions and 393 deletions.
3 changes: 3 additions & 0 deletions refer.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
background.png: https://i3.ytimg.com/vi/UpGCxdTXfSY/hqdefault.jpg
background2.jpg : https://image.winudf.com/v2/image1/Y29tLnN5bHBoYm94LmNsYXNzaWMuc25ha2Vfc2NyZWVuXzBfMTYwMjUwNzU0NF8wNjA/screen-0.jpg?fakeurl=1&type=.jpg
java: https://www.oracle.com/a/tech/img/rc10-java-badge-3.png
Binary file added src/backgrounds/background1.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/backgrounds/background2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/backgrounds/background3.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/backgrounds/background4.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/backgrounds/background5.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
File renamed without changes
File renamed without changes
Binary file removed src/image/background.jpg
Binary file not shown.
Binary file added src/image/bug.ico
Binary file not shown.
Binary file added src/image/bug.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/image/contact.ico
Binary file not shown.
Binary file added src/image/contact.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/image/difficult.ico
Binary file not shown.
Binary file added src/image/difficult.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/image/discuss.ico
Binary file not shown.
Binary file added src/image/discuss.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/image/easy.ico
Binary file not shown.
Binary file added src/image/easy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/image/file.ico
Binary file not shown.
Binary file added src/image/file.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/image/help.ico
Binary file not shown.
Binary file added src/image/help.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/image/high_score.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/image/how_to_paly.ico
Binary file not shown.
Binary file added src/image/level.ico
Binary file not shown.
Binary file added src/image/level.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/image/level2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/image/license.ico
Binary file not shown.
Binary file added src/image/license.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/image/love_icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/image/medium.ico
Binary file not shown.
Binary file added src/image/medium.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/image/more.ico
Binary file not shown.
Binary file added src/image/more.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/image/more2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/image/new_game.ico
Binary file not shown.
Binary file added src/image/new_game.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/image/quit.ico
Binary file not shown.
Binary file added src/image/quit.png
14 changes: 14 additions & 0 deletions src/image/refer.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
bug: https://icon-icons.com/download/9156/PNG/32/
contact: https://icon-icons.com/icon/add-contact-twitter-user/127131
help: https://icon-icons.com/download/14267/PNG/32/
easy: https://icon-icons.com/download/35019/PNG/32/
medium: https://icon-icons.com/download/34935/PNG/32/
difficult: https://icon-icons.com/download/35026/PNG/32/
quit: https://icon-icons.com/download/106118/PNG/32/
license: https://icon-icons.com/download/35763/PNG/32/
file: https://icon-icons.com/download/106991/PNG/32/
new game: https://icon-icons.com/download/13225/PNG/32/
level: https://icon-icons.com/download/83772/PNG/32/
level2: https://icon-icons.com/download/102343/PNG/32/
more: https://icon-icons.com/download/79906/PNG/32/
more2: https://icon-icons.com/download/81375/PNG/32/
Binary file added src/image/snake_icon.png
Binary file removed src/image/snake_image.png
Diff not rendered.
Binary file removed src/music/ting toong.m4r
Binary file not shown.
File renamed without changes.
File renamed without changes.
File renamed without changes.
5 changes: 5 additions & 0 deletions src/root/IRoot.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package root;

public interface IRoot {

}
36 changes: 36 additions & 0 deletions src/root/Root.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package root;

import javax.swing.ImageIcon;

public class Root {
public ImageIcon icon = new ImageIcon("./src/image/snake_icon.png");

public ImageIcon ic1 = new ImageIcon("./src/image/high_score.png");
public ImageIcon ic2 = new ImageIcon("./src/image/new_game.png");
public ImageIcon ic3 = new ImageIcon("./src/image/quit.png");

public ImageIcon icn1 = new ImageIcon("./src/image/discuss.png");
public ImageIcon icn2 = new ImageIcon("./src/image/license.png");
public ImageIcon icn3 = new ImageIcon("./src/image/contact.png");

public ImageIcon Icn1 = new ImageIcon("./src/image/help.png");
public ImageIcon Icn2 = new ImageIcon("./src/image/bug.png");
public ImageIcon Icn3 = new ImageIcon("./src/image/love_icon.png");

//Images Path In Array
public String[] list = {
"./src/backgrounds/background1.jpeg",
"./src/backgrounds/background2.jpg",
"./src/backgrounds/background3.jpg",
"./src/backgrounds/background4.jpg",
"./src/backgrounds/background5.jpg",
"./src/backgrounds/background1.jpeg"
};

public String[] array2 = {"./src/image/easy.png", "./src/image/medium.png", "./src/image/difficult.png"};

public String contact = "mailto:[email protected]";
public String discuss = "https://github.com/thuongtruong1009/Snake-Game-OOP/discuss";
public String bug = "https://github.com/thuongtruong1009/Snake-Game-OOP/issues";
public String how_to_play = "https://github.com/thuongtruong1009/Snake-Game-OOP/README.md";
}
269 changes: 269 additions & 0 deletions src/thuongtruong1009/Frame.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,269 @@
package thuongtruong1009;

import root.Root;

import java.awt.BorderLayout;
import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;
import javax.swing.Timer;
/**
* @author https://www.github.com/thuongtruong1009
*/
public class Frame extends JPanel {
JFrame frame= new JFrame();
JPanel panel= new JPanel();
private static final long serialVersionUID = 1L;
static int DELAY=100;
Timer timer;
Root root = new Root();

JLabel pic;
Timer tm;
int x = 0;


@SuppressWarnings("static-access")
public Frame() {
pic = new JLabel();
pic.setBounds(0, 0, 500, 235);

//Call The Function SetImageSize
SetImageSize(5);
//set a timer
tm = new Timer(1000,new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
SetImageSize(x);
x += 1;
if(x >= root.list.length )
x = 0;
}
});
panel.add(pic, BorderLayout.CENTER);
panel.setLayout(new FlowLayout());
tm.start();
//
// ImageIcon image= new ImageIcon("./src/backgrounds/background2.jpg");
// Image img= image.getImage();
// Image temp_img= img.getScaledInstance(500, 235, Image.SCALE_SMOOTH);
// image= new ImageIcon(temp_img);
//
// JLabel background= new JLabel("", image, JLabel.CENTER);
// background.setBounds(0, 0, 500, 300);
// panel.add(background, BorderLayout.CENTER);

JMenuBar menubar= new JMenuBar();
frame.add(menubar, BorderLayout.NORTH);

//menu1
JMenu fileMenu = new JMenu("File");
fileMenu.setMnemonic(KeyEvent.VK_F);
fileMenu.addSeparator();
menubar.add(fileMenu);

JMenuItem scoreItem= new JMenuItem("History score", root.ic1);
fileMenu.add(scoreItem);
scoreItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
readFile("./src/high_score.txt", "history");
} catch (IOException e1) {
e1.printStackTrace();
}
}
});

JMenuItem newGameItem= new JMenuItem("New Game", root.ic2);
fileMenu.add(newGameItem);
newGameItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new SetupData();
}
});

JMenuItem exitItem= new JMenuItem("Quit", root.ic3);
fileMenu.add(exitItem);
exitItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});

//menu2
JMenu levelMenu= new JMenu("Level");
levelMenu.setMnemonic(KeyEvent.VK_L);
levelMenu.addSeparator();
menubar.add(levelMenu);

JMenuItem[] menuItem2= new JMenuItem[3];
String[] array= {"Easy", "Medium", "Difficult"};

for(int i= 0 ; i<3 ; i++) {
int level = i + 1;
ImageIcon ii = new ImageIcon(root.array2[i]);
menuItem2[i]= new JMenuItem(array[i] + " (" + level + ")", ii);
menuItem2[i].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//menuItem2[i].setBackground(Color.green);
DELAY = 100 - 20*level;
}
});
levelMenu.add(menuItem2[i]);
}

//menu3
JMenu referMenu= new JMenu("More");
referMenu.setMnemonic(KeyEvent.VK_M);
referMenu.addSeparator();
menubar.add(referMenu);

JMenuItem discussItem= new JMenuItem("Discuss", root.icn1);
referMenu.add(discussItem);
discussItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
openLink(root.discuss);
}
});

JMenuItem licenseItem= new JMenuItem("License", root.icn2);
referMenu.add(licenseItem);
licenseItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
readFile("./src/LICENSE.txt", "ECL-2.0 LICENSE");
} catch (IOException e1) {
e1.printStackTrace();
}
}
});

JMenuItem contactItem= new JMenuItem("Contact", root.icn3);
referMenu.add(contactItem);
contactItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
openLink(root.contact);
}
});

//menu4
JMenu helpMenu= new JMenu("Help");
helpMenu.setMnemonic(KeyEvent.VK_H);
helpMenu.addSeparator();
menubar.add(helpMenu);

JMenuItem tutorialItem= new JMenuItem("How to play?", root.Icn1);
helpMenu.add(tutorialItem);
tutorialItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
openLink(root.how_to_play);
}
});

JMenuItem reportItem= new JMenuItem("Report bugs", root.Icn2);
helpMenu.add(reportItem);
reportItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
openLink(root.bug);
}
});

JMenuItem donateItem = new JMenuItem("Donate", root.Icn3);
helpMenu.add(donateItem);
donateItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
readFile("./src/LICENSE.txt", "ECL-2.0 LICENSE");
} catch (IOException e1) {
e1.printStackTrace();
}
}
});

//frame
frame.add(panel);
frame.setTitle("Snake Game OOP - v2.0");
frame.setSize(500, 300);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setVisible(true);

Image imageIcon = root.icon.getImage();
frame.setIconImage(imageIcon);
}

//create a function to resize the image
public void SetImageSize(int i){
ImageIcon icon = new ImageIcon(root.list[i]);
Image img = icon.getImage();
Image newImg = img.getScaledInstance(pic.getWidth(), pic.getHeight(), Image.SCALE_SMOOTH);
ImageIcon newImc = new ImageIcon(newImg);
pic.setIcon(newImc);
}

public static void readFile(String file, String name)throws IOException{
BufferedReader br = new BufferedReader(new FileReader(file));
String aLineFromFile = null;
StringBuilder everything = new StringBuilder();
while ((aLineFromFile = br.readLine()) != null){
everything.append(aLineFromFile + "\n");
}
JTextArea textArea = new JTextArea(everything.toString());
textArea.setBorder(BorderFactory.createCompoundBorder(textArea.getBorder(), BorderFactory.createEmptyBorder(10, 10, 10, 0)));
JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setPreferredSize( new Dimension( 570, 450 ));

JOptionPane.showMessageDialog(null, scrollPane, name, JOptionPane.INFORMATION_MESSAGE);
br.close();
return;
}

public static void openLink(String uri) {
try {
Desktop.getDesktop().browse(new URI(uri));
} catch (IOException | URISyntaxException e) {
e.printStackTrace();
}
}

public void play() {
System.out.println("The speed you choose is: " + DELAY);
timer = new Timer(DELAY, (ActionListener) this);
timer.start();
}
}
Loading

0 comments on commit d2ee8f3

Please sign in to comment.