import java.io.*;
import javax.swing.*; //Needed for the swing class
import java.awt.*;
import java.awt.event.*; //Needed for action event listeners for the buttons
import java.awt.image.*;
class SavingClass
{
public static void saveGame() throws Exception //Change the return type (void) to something else later.
{
BufferedWriter saveFile = new BufferedWriter(new FileWriter("TextSave.txt"));
saveFile.write("test1, test2"); //Saves the variables listed to the first line of the CSV file. If you wanted to make another line then you would end this one with \r\n so that a new line is created. Then you would make another write method
saveFile.close(); //Closes the file writer now that we're finished.
}
}
class LoadingClass
{
public static void loadGame() throws Exception //Change the return type (void) to something else later.
{
String exampleString1;
String exampleString2;
BufferedReader saveFile = new BufferedReader(new FileReader("TextSave.txt"));
exampleString1 = saveFile.readLine();
exampleString2 = saveFile.readLine();
saveFile.close();
System.out.println(""+exampleString1+" "+exampleString2+"");
}
}
class CreateGameWindowFirst extends JFrame
{
//The below method is used for the creation of the game window.
public CreateGameWindowFirst() //If there is a return type set for this method then the code below will not show.
{
setSize(640, 480); //Sets the size of the game window
setLocationRelativeTo(null); //Centers the window on the users screen.
setTitle("The Game - By Tyler MacEachern"); //Sets the title of the game window
setDefaultCloseOperation(EXIT_ON_CLOSE); //Basically this makes it so when you hit the red X the window and program will close.
setResizable(false); //Makes it so that the user cannot resize the JFrame.
final JPanel gamePanel = new JPanel(); //Creates a new JPanel to put everything on.
getContentPane().add(gamePanel);
gamePanel.setLayout(null); //Makes it so you can manually positon everything using XY coords.
CreateMainMenu gameWindow = new CreateMainMenu(gamePanel); //Tells the program to run the CreateMainMenu class so that the main menu will be created.
setVisible(true); //Makes the JFrame, also known ass gameWindow visible to the user.
}
}
class CreateMainMenu extends JFrame
{
public CreateMainMenu(final JPanel gamePanel)
{
//Create the main menu's look with buttons and that
Insets insets = gamePanel.getInsets(); //Gets the amount of space that the gameWindow's boarders take up so that you can use it to better position your JButtons and stuff.
final JButton newGameButton = new JButton(""); //Creates a button object for the New Game button on the main menu.
final JButton loadGameButton = new JButton(""); //Creates a button object for the Load Game button on the main menu.
final JButton optionsButton = new JButton(""); //Creates a button object for the Options button on the main menu.
gamePanel.add(newGameButton); //Adds the New Game button to the JPanel
newGameButton.setBounds(200 + insets.left, 140 + insets.top, 180, 40);
newGameButton.setBorder(null); //Disables the thin black box around the button
newGameButton.setFocusPainted(false); //Makes it so that when you select the button there wont be a little box around the words to signify that the button is currently selected.
newGameButton.setContentAreaFilled(false); //Gets rid of the grey fill when you select the button.
Icon newGameButtonIcon = new ImageIcon("button_newGame_normal.png"); //Creates a new ImageIcon object that will be used as the newGameButton.
newGameButton.setIcon(newGameButtonIcon); //Covers the newGameButton icon with the testbutton.png image.
Icon newGameButtonIconRollover = new ImageIcon("button_newGame_rollover.png"); //Creates a new ImageIcon object that will be used as the rollover version of the newGameButton.
newGameButton.setRolloverIcon(newGameButtonIconRollover); //Tells the newGameButton to, when the mouse is over it, change to the rollover version of the newGamebutton.
Icon newGameButtonIconPressed = new ImageIcon("button_newGame_press.png");
newGameButton.setPressedIcon(newGameButtonIconPressed);
newGameButton.addActionListener(new ActionListener() { //Action listener for the newGameButton
public void actionPerformed(ActionEvent e) {
System.out.println("You've pressed the New Game Button on the main menu screen.");
gamePanel.remove(newGameButton); //Removes the newGameButton
gamePanel.remove(loadGameButton); //Removes the loadGamebutton
gamePanel.remove(optionsButton); //Removes the optionsButton
gamePanel.repaint(); //Re-loads the JPanel, also known as gamePanel, so that the deleted buttons disappear.
}});
//
gamePanel.add(loadGameButton); //Adds the Load Game button to the JPanel
loadGameButton.setBounds(200 + insets.left, 210 + insets.top, 180, 40);
loadGameButton.setBorder(null); //Disables the thin black box around the button
loadGameButton.setFocusPainted(false); //Makes it so that when you select the button there wont be a little box around the words to signify that the button is currently selected.
loadGameButton.setContentAreaFilled(false); //Gets rid of the grey fill when you select the button.
Icon loadGameButtonIcon = new ImageIcon("button_loadGame_normal.png"); //Creates a new ImageIcon object that will be used as the newGameButton.
loadGameButton.setIcon(loadGameButtonIcon); //Covers the newGameButton icon with the testbutton.png image.
Icon loadGameButtonIconRollover = new ImageIcon("button_loadGame_rollover.png"); //Creates a new ImageIcon object that will be used as the rollover version of the newGameButton.
loadGameButton.setRolloverIcon(loadGameButtonIconRollover); //Tells the newGameButton to, when the mouse is over it, change to the rollover version of the newGamebutton.
Icon loadGameButtonIconPressed = new ImageIcon("button_loadGame_press.png");
loadGameButton.setPressedIcon(loadGameButtonIconPressed);
loadGameButton.addActionListener(new ActionListener() { //Action listener for the loadGameButton
public void actionPerformed(ActionEvent e) {
System.out.println("You've pressed the Load Game Button on the main menu screen.");
gamePanel.remove(newGameButton); //Removes the newGameButton
gamePanel.remove(loadGameButton); //Removes the loadGamebutton
gamePanel.remove(optionsButton); //Removes the optionsButton
gamePanel.repaint(); //Re-loads the JPanel, also known as gamePanel, so that the deleted buttons disappear.
CreateLoadGameMenu gameWindow = new CreateLoadGameMenu(gamePanel); //Passes the gamePanel object to the LoadGameMenu class so that it can be used there
}});
//
gamePanel.add(optionsButton); //Adds the Options Game button to the JPanel
optionsButton.setBounds(200 + insets.left, 280 + insets.top, 180, 40);
optionsButton.setBorder(null); //Disables the thin black box around the button
optionsButton.setFocusPainted(false); //Makes it so that when you select the button there wont be a little box around the words to signify that the button is currently selected.
optionsButton.setContentAreaFilled(false); //Gets rid of the grey fill when you select the button.
Icon optionsButtonIcon = new ImageIcon("button_optionsButton_normal.png"); //Creates a new ImageIcon object that will be used as the newGameButton.
optionsButton.setIcon(optionsButtonIcon); //Covers the newGameButton icon with the testbutton.png image.
Icon optionsButtonIconRollover = new ImageIcon("button_optionsButton_rollover.png"); //Creates a new ImageIcon object that will be used as the rollover version of the newGameButton.
optionsButton.setRolloverIcon(optionsButtonIconRollover); //Tells the newGameButton to, when the mouse is over it, change to the rollover version of the newGamebutton.
Icon optionsButtonIconPressed = new ImageIcon("button_optionsButton_press.png");
optionsButton.setPressedIcon(optionsButtonIconPressed);
optionsButton.addActionListener(new ActionListener() { //Action listener for the newGameButton
public void actionPerformed(ActionEvent e) {
System.out.println("You've pressed the Options Button on the main menu screen.");
gamePanel.remove(newGameButton); //Removes the newGameButton
gamePanel.remove(loadGameButton); //Removes the loadGamebutton
gamePanel.remove(optionsButton); //Removes the optionsButton
gamePanel.repaint(); //Re-loads the JPanel, also known as gamePanel, so that the deleted buttons disappear.
CreateOptionsMenu gameWindow = new CreateOptionsMenu(gamePanel);
}});
}
}
class CreateLoadGameMenu extends JFrame
{
public CreateLoadGameMenu(final JPanel gamePanel)
{
Insets insets = gamePanel.getInsets(); //Gets the amount of space that the gameWindow's boarders take up so that you can use it to better position your JButtons and stuff.
final JButton backButton = new JButton(""); //Creates a button object for the Back button on the main menu.
gamePanel.add(backButton); //Adds the Back button to the JPanel
backButton.setBounds(0 + insets.left, 400 + insets.top, 180, 40);
backButton.setBorder(null); //Disables the thin black box around the button
backButton.setFocusPainted(false); //Makes it so that when you select the button there wont be a little box around the words to signify that the button is currently selected.
backButton.setContentAreaFilled(false); //Gets rid of the grey fill when you select the button.
Icon backButtonIcon = new ImageIcon("button_backButton_normal.png"); //Creates a new ImageIcon object that will be used as the backButton.
backButton.setIcon(backButtonIcon); //Covers the backButton icon with the testbutton.png image.
Icon backButtonIconRollover = new ImageIcon("button_backButton_rollover.png"); //Creates a new ImageIcon object that will be used as the rollover version of the backButton.
backButton.setRolloverIcon(backButtonIconRollover); //Tells the backButton to, when the mouse is over it, change to the rollover version of the backButton.
Icon backButtonIconPressed = new ImageIcon("button_backButton_press.png");
backButton.setPressedIcon(backButtonIconPressed);
backButton.addActionListener(new ActionListener() { //Action listener for the newGameButton
public void actionPerformed(ActionEvent e) {
System.out.println("You've pressed the Back Button on the Load Game screen.");
gamePanel.remove(backButton);
gamePanel.repaint(); //Re-loads the JPanel, also known as gamePanel, so that the deleted buttons disappear.
CreateMainMenu gameWindow = new CreateMainMenu(gamePanel);
}});
}
}
class CreateOptionsMenu extends JFrame
{
public CreateOptionsMenu(final JFrame gamePanel)
{
Insets insets = gamePanel.getInsets(); //Gets the amount of space that the gameWindow's boarders take up so that you can use it to better position your JButtons and stuff.
final JButton backButton = new JButton(""); //Creates a button object for the Back button on the main menu.
gamePanel.add(backButton); //Adds the Back button to the JPanel
backButton.setBounds(0 + insets.left, 400 + insets.top, 180, 40);
backButton.setBorder(null); //Disables the thin black box around the button
backButton.setFocusPainted(false); //Makes it so that when you select the button there wont be a little box around the words to signify that the button is currently selected.
backButton.setContentAreaFilled(false); //Gets rid of the grey fill when you select the button.
Icon backButtonIcon = new ImageIcon("button_backButton_normal.png"); //Creates a new ImageIcon object that will be used as the backButton.
backButton.setIcon(backButtonIcon); //Covers the backButton icon with the testbutton.png image.
Icon backButtonIconRollover = new ImageIcon("button_backButton_rollover.png"); //Creates a new ImageIcon object that will be used as the rollover version of the backButton.
backButton.setRolloverIcon(backButtonIconRollover); //Tells the backButton to, when the mouse is over it, change to the rollover version of the backButton.
Icon backButtonIconPressed = new ImageIcon("button_backButton_press.png");
backButton.setPressedIcon(backButtonIconPressed);
backButton.addActionListener(new ActionListener() { //Action listener for the newGameButton
public void actionPerformed(ActionEvent e) {
System.out.println("You've pressed the Back Button on the Options Menu screen.");
gamePanel.remove(backButton);
gamePanel.repaint(); //Re-loads the JPanel, also known as gamePanel, so that the deleted buttons disappear.
CreateMainMenu gameWindow = new CreateMainMenu(gamePanel);
}});
}
}
public class Game extends JFrame
{
public static void main(String args[]) //This is the main menu of the game, the method has been called 'main' so it will be the first thing to run when the program is loaded.
{
CreateGameWindowFirst gameWindow = new CreateGameWindowFirst(); //Calls the CreateGameWindow method of the CreateGameWindow class so that the window and that will be set up
}
}