299 lines
7.8 KiB
Java
299 lines
7.8 KiB
Java
import javax.swing.*;
|
|
import java.awt.*;
|
|
|
|
/**
|
|
* CS1322: Programming Assignment #6 - Fall 2002
|
|
*
|
|
* <PRE>
|
|
* IDBox for: GameBoard.java
|
|
*
|
|
* Revisions: 1.0 Nov. 24, 2002
|
|
* Created the GameBoard class
|
|
*
|
|
* </PRE>
|
|
*
|
|
* Collaboration statement:
|
|
* I collaborated with Roland Krystian Alberciak on this phase of the
|
|
* program.
|
|
*
|
|
* @author <A HREF="mailto:gtg184g@mail.gatech.edu">Jose M. Cabank</A>
|
|
* @version Version 1.0, Nov. 24, 2002
|
|
*/
|
|
|
|
|
|
/**
|
|
* VIEW.
|
|
* GameBoard. GameBoard extends JPanel.
|
|
* Responsible for drawing or painting the board
|
|
* according to the GamePiece array created in PacmanFileReader.
|
|
*/
|
|
public class GameBoard extends JPanel
|
|
{
|
|
|
|
/** Debug value for toggling System.out.* output */
|
|
public static boolean DEBUG = false;
|
|
public static boolean DEBUG_CODE = false;
|
|
public static boolean DEBUG_MAIN = false;
|
|
|
|
/**
|
|
* pieceArray. The board seen in the GUI.
|
|
* This array will model the board seen in the GUI.
|
|
*/
|
|
private GamePiece[][] pieceArray;
|
|
|
|
private PacmanFileReader pfr;
|
|
|
|
private ImageIcon iiPACMAN;
|
|
private ImageIcon iiGHOSTS[];
|
|
ImageIcon iiGH_RUN_AWAY;
|
|
|
|
/* End of Global Declarations */
|
|
|
|
////////////////
|
|
//Constructors//
|
|
////////////////
|
|
|
|
/**
|
|
* Constructor
|
|
* @param file_name a String file name of a board file.
|
|
* file_name is also passed off to a PacmanFileReader.
|
|
* @return GameBoard instance/object.
|
|
*/
|
|
public GameBoard(String file_name)
|
|
{
|
|
pfr = new PacmanFileReader(file_name);
|
|
setPieceArray( pfr.getPieceArray() );
|
|
|
|
init_images();
|
|
|
|
} //end of GameBoard(String)
|
|
|
|
///////////////////////
|
|
//Constructor Helpers//
|
|
///////////////////////
|
|
|
|
/**
|
|
* init_images. Initializes the images used in Pacman program.
|
|
* initializes the provided images of the four ghosts,
|
|
* the image of a ghost in run away mode, and of pacman.
|
|
*/
|
|
public void init_images( )
|
|
{
|
|
iiPACMAN= new ImageIcon( P6Constants.PACMAN );
|
|
|
|
iiGHOSTS = new ImageIcon[4];
|
|
|
|
iiGHOSTS[0]= new ImageIcon( P6Constants.GHOST_1 ); //Ghost 1
|
|
iiGHOSTS[1]= new ImageIcon( P6Constants.GHOST_2 ); //Ghost 2
|
|
iiGHOSTS[2]= new ImageIcon( P6Constants.GHOST_3 ); //Ghost 3
|
|
iiGHOSTS[3]= new ImageIcon( P6Constants.GHOST_4 ); //Ghost 4
|
|
|
|
iiGH_RUN_AWAY= new ImageIcon( P6Constants.GH_RUN_AWAY );
|
|
} //end init_images
|
|
|
|
|
|
///////////
|
|
//Methods//
|
|
///////////
|
|
|
|
/**
|
|
*
|
|
*/
|
|
protected void paintComponent( Graphics g )
|
|
{
|
|
int width, height; //of rectangle
|
|
Object pieceAt=null; //generic reference
|
|
|
|
super.paintComponent(g);
|
|
|
|
width= this.getWidth()/P6Constants.BOARD_SIZE_X;
|
|
height= this.getHeight()/P6Constants.BOARD_SIZE_Y;
|
|
|
|
for (int yc=0; yc<P6Constants.BOARD_SIZE_Y; yc=yc+1)
|
|
{
|
|
for (int xc=0; xc<P6Constants.BOARD_SIZE_Y; xc=xc+1)
|
|
{
|
|
try{
|
|
pieceAt= pieceArray[xc][yc];
|
|
}catch (Exception e){ //Perhaps NullPointerException, perhaps not.
|
|
String err="";
|
|
err= "Perhaps pieceArray[][] has not been initialized?";
|
|
System.out.println(err);
|
|
}
|
|
|
|
if (pieceAt instanceof WallPiece){
|
|
g.setColor(P6Constants.WALL_COLOR);
|
|
g.drawRect(xc*width,yc*height,width,height);//draw
|
|
g.fillRect(xc*width,yc*height,width,height);//then fill
|
|
} //endif
|
|
|
|
else if (pieceAt instanceof FloorPiece)
|
|
{
|
|
FloorPiece fpTemp = (FloorPiece)pieceAt;
|
|
|
|
// First we make a default floor Piece, where we assume
|
|
// (fpTemp.getNibble()==false) && (fpTemp.getSpecial()==false)
|
|
g.setColor(P6Constants.FLOOR_COLOR);
|
|
g.drawRect(xc*width,yc*height,width,height);//draw
|
|
g.fillRect(xc*width,yc*height,width,height);//then fill
|
|
|
|
//then we modify over it.
|
|
if (
|
|
(fpTemp.getNibble()==true)
|
|
&& (fpTemp.getSpecial()==false)
|
|
) //Floor with Nibble but not a Special.
|
|
{
|
|
g.setColor(P6Constants.NIBBLE_COLOR);
|
|
g.drawRect(xc*width+width/2-width/4,
|
|
yc*height+height/2-height/4,
|
|
width/4,
|
|
height/4);//draw
|
|
g.fillRect(xc*width+width/2-width/4,
|
|
yc*height+height/2-height/4,
|
|
width/4,
|
|
height/4);//then fill
|
|
}//endif
|
|
|
|
else if (
|
|
(fpTemp.getNibble()==false)
|
|
&& (fpTemp.getSpecial()==true)
|
|
)
|
|
{
|
|
g.setColor(P6Constants.SPECIAL_COLOR);
|
|
g.drawRect(xc*width+width/2-width/4,
|
|
yc*height+height/2-height/4,
|
|
width/4,
|
|
height/4);//draw
|
|
g.fillRect(xc*width+width/2-width/4,
|
|
yc*height+height/2-height/4,
|
|
width/4,
|
|
height/4);//then fill
|
|
}// endelse
|
|
|
|
//for error. Flag to programmer to correct something.
|
|
else if (
|
|
(fpTemp.getNibble()==true)
|
|
&& (fpTemp.getSpecial()==true)
|
|
)
|
|
{
|
|
String errMsg="";
|
|
errMsg=" Error: Cannot draw Nibble with Special";
|
|
|
|
String fpWas="";
|
|
fpWas="A floorpiece of:"+ fpTemp.toString()+" ";
|
|
|
|
System.out.println(this.getClass()+fpWas+"\n"+errMsg);
|
|
} //endelse
|
|
}//endelse
|
|
|
|
}//end xc-for
|
|
}//end yc-for
|
|
|
|
//Now to draw Pacman, Ghosts.
|
|
|
|
// First, Pacman.
|
|
g.drawImage(iiPACMAN.getImage(),
|
|
pfr.getPacman().getX()*width, //get X position on board..
|
|
pfr.getPacman().getY()*height, //get Y position on board..
|
|
iiPACMAN.getImageObserver()
|
|
);
|
|
|
|
// Now, the Ghosts.
|
|
for (int loop=0; loop<iiGHOSTS.length; loop=loop+1)
|
|
{
|
|
Ghost tempG = pfr.getGhosts()[loop];
|
|
|
|
if (tempG.getRunAwayFlag()==false) //draw normal Ghost
|
|
g.drawImage(iiGHOSTS[loop].getImage(),
|
|
tempG.getX()*width, //get X position on board..
|
|
tempG.getY()*height, //get Y position on board..
|
|
iiGHOSTS[loop].getImageObserver()
|
|
);
|
|
|
|
else // if (tempG.getRunAwayFlag()==true) //draw spooked Ghost
|
|
g.drawImage(iiGH_RUN_AWAY.getImage(),
|
|
tempG.getX()*width, //get X position on board..
|
|
tempG.getY()*height, //get Y position on board..
|
|
iiGH_RUN_AWAY.getImageObserver()
|
|
);
|
|
}//endfor-go through ghosts
|
|
|
|
} //end PaintComponent(Graphics)
|
|
|
|
///////////////////////
|
|
//Accessors/Modifiers//
|
|
///////////////////////
|
|
|
|
/**
|
|
* Get the value of pieceArray.
|
|
* @return value of pieceArray.
|
|
*/
|
|
public GamePiece[][] getPieceArray()
|
|
{
|
|
return pieceArray;
|
|
}
|
|
|
|
/**
|
|
* Set the value of pieceArray.
|
|
* @param v Value to assign to pieceArray.
|
|
*/
|
|
public void setPieceArray(GamePiece[][] v)
|
|
{
|
|
this.pieceArray = v;
|
|
}
|
|
|
|
/**
|
|
* Get the value of pfr.
|
|
* @return value of pfr.
|
|
*/
|
|
public PacmanFileReader getPfr()
|
|
{
|
|
return pfr;
|
|
}
|
|
|
|
/**
|
|
* Set the value of pfr.
|
|
* @param v Value to assign to pfr.
|
|
*/
|
|
public void setPfr(PacmanFileReader v)
|
|
{
|
|
this.pfr = v;
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
// =======================================================================
|
|
// Main method (overrides this commenting system for readibility reasons)
|
|
// =======================================================================
|
|
*/
|
|
|
|
/**
|
|
* Debugging main for class GameBoard.
|
|
* 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.
|
|
|
|
|
|
|
|
|
|
|
|
if (DEBUG) System.out.println("end main(String[] args) call:");
|
|
} //end of DEBUG if
|
|
|
|
} //end of main(String[] args)
|
|
|
|
} //end of class GameBoard
|