Skip to content

Commit

Permalink
adding javadoc comments
Browse files Browse the repository at this point in the history
  • Loading branch information
iftikhar2k16 committed Mar 17, 2016
1 parent 7a3b748 commit 5fcdc49
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 53 deletions.
41 changes: 28 additions & 13 deletions src/code/map/MapMaker.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,13 @@ public boolean saveToFile() {
* It takes as arguments the mapModel which either need to be created or is read from an existing file, an argument
* to specify whether it is a new map or is read from an existing file and reference of the parent frame.
*
* @param mMapModel create map model object
* @param isExistingFile for valid map
* @param prntfile to save the file
* @param mMapModel the map model object which is to be created or modified
* @param isExistingFile if the map model already exist
* @param prntfile the parent form of the current object
*/
public MapMaker(MapModel mMapModel, boolean isExistingFile, MyGuiFile prntfile) {

// update the parent combo box
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent arg0) {
Expand Down Expand Up @@ -148,6 +149,7 @@ public void windowClosing(WindowEvent arg0) {
panel.add(lblOptions);


// start button event handler
buttonStart.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(!isStartAdded){
Expand All @@ -161,6 +163,7 @@ public void actionPerformed(ActionEvent e) {
buttonStart.setBounds(69, 38, 115, 29);
panel.add(buttonStart);

// end button event handler
buttonEnd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(!isEndAdded){
Expand All @@ -184,6 +187,7 @@ public void actionPerformed(ActionEvent e) {
buttonPath.setBounds(334, 38, 115, 29);
panel.add(buttonPath);

// delete button definition and event handler
JButton buttonDelete = new JButton("Delete");
buttonDelete.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Expand All @@ -204,6 +208,7 @@ public void actionPerformed(ActionEvent arg0) {
menuBar.add(mnFile);

JMenuItem mntmSave = new JMenuItem("Save");
// save button event handler
mntmSave.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
saveToFile();
Expand All @@ -220,9 +225,11 @@ public void actionPerformed(ActionEvent e) {
}

/**
* Method to handle the click event generated from the map grid. When user clicks on the map editor grid, the
* call is received in this method and handled based on the selected tool.
*
* @param e for mouse event
* @param cell for Jpanel
* @param e the mouse event which triggered the invocation
* @param cell the cell which on which the event is triggered.
*/
public void click(MouseEvent e, JPanel cell) {

Expand All @@ -231,7 +238,9 @@ public void click(MouseEvent e, JPanel cell) {
char[] name_exploded = tempName.toCharArray();
int x = Integer.parseInt(String.valueOf(name_exploded[0]));
int y = Integer.parseInt(String.valueOf(name_exploded[1]));

//1=StartPoint, 9999=End, 2=Path, 3=Delete
// if the delete tool is selected
if(selectedTool== Util.TOOL_DELETE) {
if(m_currMap.GetMapArray()[x][y] == 1) {
isStartAdded = false;
Expand All @@ -252,6 +261,7 @@ else if(m_currMap.GetMapArray()[x][y] > 1) {
panel_1.repaint();
} else {

// if the other tool is selected, it is handled here
if(selectedTool == Util.TOOL_POINT_ENTRY){
DrawMapItem(1, cell);

Expand All @@ -274,6 +284,7 @@ else if(m_currMap.GetMapArray()[x][y] > 1) {
}
}

// modifying the map model object based tool action performed above
if(selectedTool != 3 && !overideExisting) {
if(selectedTool==2) {
m_currMap.AddToMap(pathTempValue, x, y);
Expand All @@ -288,11 +299,12 @@ else if(m_currMap.GetMapArray()[x][y] > 1) {
}

/**
* To draw the map
* @param type
* @param cell
* Method to draw the selected map object on the cell. The method draws
* the start point, path or end point depending on the type passed to the method.
*
* @param type type of the map object to be drawn
* @param cell cell on which the object needs to be drawn
*/

private void DrawMapItem(int type, JPanel cell) {

JLabel t = new JLabel();
Expand All @@ -317,11 +329,11 @@ private void DrawMapItem(int type, JPanel cell) {


/**
* Validate the map
* @param isExisting check the map if it exit or not
* @param parentPanel create validate map into main panel
* Method to draw the map grid on UI depending on whether the current map model is already existing or not on the parent panel.
*
* @param isExisting whether the map model is existing
* @param parentPanel parent panel on which the map is to be drawn
*/

private void DrawMap(boolean isExisting, Panel parentPanel) {
if(isExisting) {
for(int k=0;k < m_currMap.rSize;k++) {
Expand All @@ -337,6 +349,7 @@ private void DrawMap(boolean isExisting, Panel parentPanel) {
JPanel temp = new JPanel();
temp.setName(k +""+ i);
temp.setBorder(BorderFactory.createEtchedBorder(1));
// adding action listener for the panel on which the cell is drawn
temp.addMouseListener(new MouseListener() {

@Override
Expand All @@ -361,6 +374,7 @@ public void mouseClicked(MouseEvent e) {
}
});

// drawing the current map model on the grid
if(m_currMap.GetMapArray()[k][i] == 1) {
DrawMapItem(1, temp);
isStartAdded = true;
Expand Down Expand Up @@ -395,6 +409,7 @@ public void mouseClicked(MouseEvent e) {
JPanel temp = new JPanel();
temp.setName(k +""+ i);
temp.setBorder(BorderFactory.createEtchedBorder(1));
// adding action listener for the panel on which the cell is drawn
temp.addMouseListener(new MouseListener() {

@Override
Expand Down
92 changes: 59 additions & 33 deletions src/code/map/MapModel.java
Original file line number Diff line number Diff line change
@@ -1,60 +1,78 @@
package code.map;

/**
* To create the model of the map
* The MapModel class is the model class for the map object. The map details are stored in this class. The map editing
* is also performed using the methods exposed by this class. Further map validation operation is also implemented here
* which should be called before saving the map model object for ensuring map correctness.
*
*
* @author Lokesh
* @author M.Umer
* @author Umer
*
* @version 1.0.0.0
*/

public class MapModel {

// attributes of the class goes here
private String mapName;
private int[][] mapArray;
public int rSize, cSize;

/**
* To create the constructor for map model
* @param _mapName map name
* @param _mapArray length for map
* Constructor method of the class to create a map model from an already existing file.
* The map array and the map name needs to be passed to the constructor for the model initialization.
*
* @param _mapName name of the map (usually the filename)
* @param _mapArray the map array of the model (usually read from the file)
*/

public MapModel(String _mapName, int[][] _mapArray) {
this.mapName = _mapName;
rSize = _mapArray.length;
cSize = _mapArray[0].length;
this.mapArray = _mapArray.clone();
public MapModel(String mapName, int[][] mapArray) {
this.mapName = mapName;
rSize = mapArray.length;
cSize = mapArray[0].length;
this.mapArray = mapArray.clone();
}

/**
* To create the size for map
* @param row select the row for map
* @param col select the column for map
* Constructor method to create a new map model. The size of the map array to be created is
* passed to the constructor.
*
* @param row number of rows in the map
* @param col number of columns in the map
*/

public MapModel(int row, int col) {
this.mapName = this.toString().substring(9);
rSize = row;
cSize = col;
this.mapArray = new int[row][col];
}

/**
* @return map name
* Method to return the name of the current map object.
*
* @return name of the map.
*/

public String GetName() {
return this.mapName;
}

/**
* @return selected map array
* This method returns the map array of the current map model object.
*
* @return the map model array
*/
public int[][] GetMapArray() {
return this.mapArray;
}

/**
* @param type for map validation
* @param row for map
* @param col for map
* @return boolean
* Method to add a map object to the current map. The map object to be added can be a start point specified by 1, end point specified by 9999
* or a path specified by a number between 1 and 9999
*
* @param type type of the map object to be added
* @param row row position of the map where object is to be added
* @param col column position of the map where the object is to be added
* @return if the map object has been successfully added to the map.
*/
public Boolean AddToMap(int type, int row, int col) {
if(row > mapArray.length || col > mapArray[0].length) {
Expand All @@ -68,12 +86,12 @@ public Boolean AddToMap(int type, int row, int col) {
}

/**
* Delete the row and column from the created map
* @param row delete from map
* @param col delete from map
* @return boolean
* Method to delete a map object from the map from the specified position.
*
* @param row row position of the map where object is to be deleted
* @param col column position of the map where the object is to be deleted
* @return if the map object has been successfully deleted from the map.
*/

public Boolean DeleteFromMap(int row, int col) {
if(row > mapArray.length || col > mapArray[0].length) {
return false;
Expand All @@ -84,19 +102,28 @@ public Boolean DeleteFromMap(int row, int col) {
}

/**
* To check the map validation
* @return boolean
* Method to validate the current map model object. The methods checks if the map is a valid map
* containing a start point, an end point and a continuous path between them.
* It also checks if there is no orphan path in the map.
*
* @return if the map is a valid map
*/

public boolean validateMap() {
int[][] mapArrayClone = new int[rSize][];
// for cloning the map array
int[][] mapArrayClone = new int[rSize][];
// for checking if there is only 1 start and end point
int strtcnt = 0, endCount = 0;
boolean isPathExisting = false;

for(int i = 0; i < rSize; i++){
mapArrayClone[i] = mapArray[i].clone();
}
// breadth first search algorithm implementation

// breadth first search algorithm implementation for finding the shortest path
// between and the start and end point. The algorithm has been modified to calculate the
// distance from start node to a node, previous node of a node if a path exist through it.
// It also has some patches to validate a few necessary conditions for checking the
// validity of the map correctness.
String[] nodeQueue = new String[81];
int qStart = 0, qEnd = 0;
int[][] distArray = new int[rSize][cSize];
Expand Down Expand Up @@ -222,7 +249,6 @@ else if(mapArray[k][i] > 1)

mapArray = mapArrayClone;
return true;

}

}
10 changes: 9 additions & 1 deletion src/code/map/MyGuiFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
* This is the main view form which is displayed 1st to the user.
* The user can decide weather to create a new map or open an existing file
* for editing.
*
* @author lokesh
* @author M.Umer
* @version 1.0.1.0
Expand All @@ -48,6 +49,7 @@ public class MyGuiFile extends JFrame {
/**
* Main method of the class where the execution begins. The applications is invoked from this main method. It takes command line arguments
* which for now is not used.
*
* @param args command line arguments passed to the application during invocation.
*/
public static void main(String[] args) {
Expand All @@ -66,6 +68,7 @@ public void run() {
/**
* Method to read an already existing map from file and create a map model object of the read map. The name of the map file and
* the absolute path to the folder containing the file is passed to the method.
*
* @param filename name of the map file.
* @param path path to the folder containing the map file.
*/
Expand All @@ -77,6 +80,7 @@ public void readMapFrmFile(String filename, String path) {
try {
FileInputStream fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
// map array read from the file
maparray = (int[][]) ois.readObject();
ois.close();
fis.close();
Expand All @@ -93,6 +97,9 @@ public void readMapFrmFile(String filename, String path) {
}
}

/**
* Method to update the combo box component with the map files in the default directory.
*/
public void updateTxtPn() {
//m_comboBox
m_comboBox.removeAllItems();
Expand All @@ -104,14 +111,15 @@ public void updateTxtPn() {

for (File file : listOfFiles) {
if (file.isFile() && file.getName().endsWith(".map")) {
// file in the default directory
m_comboBox.addItem(file.getName());
}
}
}catch(Exception ex){}
}

/**
* To create the new map dialog
*
*/
private void createNewMap() {
NewMapDialog mapDialog = new NewMapDialog();
Expand Down
Loading

0 comments on commit 5fcdc49

Please sign in to comment.