227 lines
6.1 KiB
Java
227 lines
6.1 KiB
Java
/**
|
|
* CS1322: Programming Assignment #6 - Fall 2002
|
|
*
|
|
* <PRE>
|
|
* IDBox for: PacmanFrame.java
|
|
*
|
|
* Revisions: 1.0 Nov. 24, 2002
|
|
* Created the PacmanFrame class
|
|
*
|
|
* </PRE>
|
|
*
|
|
* Collaboration statement:
|
|
* "I worked on the homework assignment alone, using only
|
|
* course materials."
|
|
*
|
|
* @author
|
|
<A HREF="mailto:gtg142g@mail.gatech.edu">Roland Krystian Alberciak</A>
|
|
* @version Version 1.0, Nov. 24, 2002
|
|
*/
|
|
|
|
import javax.swing.*;
|
|
import javax.swing.event.*;
|
|
import java.awt.*;
|
|
import java.awt.event.*;
|
|
|
|
/*
|
|
*o an instance of GameBoard
|
|
o a JMenu with two JMenuItems:
|
|
- one that activates a new game: This should restart the
|
|
game no matter what the state of the current game.
|
|
- one that exits the program. This should stop the JVM just
|
|
as if we clicked on the X in the top right corner of the
|
|
frame.
|
|
o a default constructor which sets up the frame with a
|
|
BorderLayout, instantiates a new GameBoard and adds it to the
|
|
center. There is a constant in P6Constants called BOARD_FILE
|
|
containing the name of the file to tokenize into a GamePiece
|
|
array.
|
|
o We recommend you set the size of the frame to be about 520x520
|
|
o helper methods that set up the menus.
|
|
o We recommend you have this class be a listener for its own
|
|
JMenuItems. You will need to then have this class implement
|
|
ActionListener.
|
|
o The program should exit gracefully if either the quit menu is
|
|
chosen or the X in the top right is clicked.
|
|
*/
|
|
|
|
public class PacmanFrame extends JFrame implements ActionListener{
|
|
|
|
GameBoard cGameBoard;
|
|
|
|
JMenu cGameMenu;
|
|
JMenuItem jNewGame;
|
|
JMenuItem jExitGame; //why the heck would you want to do that!!
|
|
|
|
/**
|
|
*PacmanFrame Default Constructor
|
|
*Basically sets up the game
|
|
*/
|
|
public PacmanFrame(){
|
|
|
|
|
|
|
|
|
|
|
|
public class PacmanFrame extends JFrame implements ActionListener
|
|
{
|
|
/** Debug value for toggling System.out.* output */
|
|
public static boolean DEBUG = false;
|
|
public static boolean DEBUG_CODE = false;
|
|
public static boolean DEBUG_MAIN = false;
|
|
|
|
|
|
|
|
/**
|
|
* Constructor 2. Specified default-overloading constructor for PacmanFrame.
|
|
* @param void No parameters.
|
|
* @return PacmanFrame instance/object.
|
|
*/
|
|
public PacmanFrame()
|
|
{
|
|
super("Pacman: Pac-a-delic, yo");
|
|
super.getContentPane().setLayout( new BorderLayout() );
|
|
|
|
// also, remember "board2.txt"
|
|
// this.setGb(new GameBoard("board_2.txt") );
|
|
this.setGb(new GameBoard(P6Constants.BOARD_FILE) );
|
|
super.getContentPane().add(getGb(), BorderLayout.CENTER);
|
|
|
|
makeGUI();
|
|
addToGUI();
|
|
|
|
super.setSize(520, 520);
|
|
super.setResizable(false); //processWindowEvent();
|
|
super.setDefaultCloseOperation(EXIT_ON_CLOSE);
|
|
super.setVisible(true); //super.show();
|
|
} //end of PacmanFrame()
|
|
|
|
|
|
public void makeGUI()
|
|
{
|
|
newGame = new JMenuItem("Restart Pacman");
|
|
exitGame = new JMenuItem("Exit Pacman");
|
|
|
|
mainMenu = new JMenu("Pacman Menu");
|
|
|
|
menuBar = new JMenuBar();
|
|
}
|
|
|
|
|
|
public void addToGUI()
|
|
{
|
|
mainMenu.add(newGame);
|
|
mainMenu.add(exitGame);
|
|
|
|
menuBar.add(mainMenu); // mainMenu is placed into menuBar.
|
|
|
|
super.setJMenuBar(menuBar);
|
|
}
|
|
|
|
|
|
public void actionPerformed(ActionEvent ae)
|
|
{
|
|
if( (ae.getSource() instanceof JMenuItem) ==true )
|
|
{
|
|
//Restart Pacman
|
|
if( ae.getActionCommand().equals("Restart Pacman") ==true )
|
|
{
|
|
System.out.println("Restarted Pacman");
|
|
} //endif
|
|
else if( ae.getActionCommand().equals("Exit Pacman") ==true )
|
|
{
|
|
System.exit(1); //Exits gracefully. Compassionate Conservative.
|
|
System.out.println("Exiting Pacman");
|
|
} //endif
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
// =======================================================================
|
|
// Methods dealing directly with the data within this instance
|
|
// (accessors, modifiers, etc)
|
|
// =======================================================================
|
|
*/
|
|
|
|
/**
|
|
* Get the value of gb.
|
|
* @return value of gb.
|
|
*/
|
|
public GameBoard getGb()
|
|
{
|
|
return gb;
|
|
}
|
|
|
|
/**
|
|
* Set the value of gb.
|
|
* @param v Value to assign to gb.
|
|
*/
|
|
public void setGb(GameBoard v)
|
|
{
|
|
this.gb = v;
|
|
}
|
|
|
|
|
|
/*
|
|
// =======================================================================
|
|
// Protected Variables local to this instance
|
|
// =======================================================================
|
|
*/
|
|
|
|
|
|
/*
|
|
// =======================================================================
|
|
// Private Variables local to this instance
|
|
// =======================================================================
|
|
*/
|
|
|
|
/**
|
|
* gb. An instance of GameBoard.
|
|
* To be used in accessing GameBoard's elements, and displaying in Frame.
|
|
*/
|
|
private GameBoard gb;
|
|
|
|
private JMenu mainMenu;
|
|
|
|
private JMenuBar menuBar;
|
|
|
|
private JMenuItem newGame;
|
|
private JMenuItem exitGame;
|
|
|
|
|
|
/*
|
|
// =======================================================================
|
|
// Main method (overrides this commenting system for readibility reasons)
|
|
// =======================================================================
|
|
*/
|
|
|
|
/**
|
|
* Debugging main for class PacmanFrame.
|
|
* This method will rigorously test my code.
|
|
*
|
|
* <br><br>
|
|
* @param args a String array of command line arguments.
|
|
*/
|
|
public static void main(String[] args)
|
|
{
|
|
|
|
if ( (args.length==1) && (args[0].equals("-db") ) )
|
|
DEBUG_MAIN=true; //else leave false: ignore input.
|
|
|
|
if (DEBUG_MAIN)
|
|
{ // NOTE: This {} encapsulates most of main and is leftmost margin as a flag.
|
|
// redundant ifs left in, in case the main if(DEBUG) statement was ommitted,
|
|
// this would then print out neat output, as an example of real running.
|
|
|
|
PacmanFrame pf = new PacmanFrame();
|
|
|
|
|
|
|
|
if (DEBUG) System.out.println("end main(String[] args) call:");
|
|
} //end of DEBUG if
|
|
|
|
} //end of main(String[] args)
|
|
|
|
} //end of class PacmanFrame
|