Skip to content

Commit

Permalink
Min/Max Map conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
armaghan143 committed Mar 17, 2016
1 parent a32e364 commit 7a3b748
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 59 deletions.
53 changes: 27 additions & 26 deletions src/code/map/MapMaker.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class MapMaker extends JFrame {

// class attribute declarations
private JPanel contentPane; // main container panel
private MapModel m_currmap; // MapModel class to hold the map object
private MapModel m_currMap; // MapModel class to hold the map object
private Panel panel_1 = new Panel(); // panel to hold the map grid
private JPanel[][] panelsHolder; // panel array to hold the cells in the grid
private JButton buttonStart = new JButton("Start Point");
Expand All @@ -65,19 +65,19 @@ public class MapMaker extends JFrame {
public boolean saveToFile() {
try {
// validate the map for correctness before saving
if(m_currmap.validateMap()) {
if(m_currMap.validateMap()) {

JFileChooser fileChooser = new JFileChooser();
fileChooser.setCurrentDirectory(new File(DEFAULT_FILE_PATH));
fileChooser.setSelectedFile(new File(m_currmap.GetName() + ".map"));
fileChooser.setSelectedFile(new File(m_currMap.GetName() + ".map"));
if (fileChooser.showSaveDialog(MapMaker.this) == JFileChooser.APPROVE_OPTION) {

File file = fileChooser.getSelectedFile();
// save to file

FileOutputStream fos= new FileOutputStream(file);
ObjectOutputStream oos= new ObjectOutputStream(fos);
oos.writeObject(m_currmap.GetMapArray());
oos.writeObject(m_currMap.GetMapArray());
oos.close();
fos.close();
JOptionPane.showMessageDialog(null, "Your map is saved successfully.");
Expand All @@ -103,21 +103,22 @@ public boolean saveToFile() {
* @param prntfile to save the file
*/
public MapMaker(MapModel mMapModel, boolean isExistingFile, MyGuiFile prntfile) {

addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent arg0) {
prntfile.updateTxtPn();
}
});

m_currmap = mMapModel;
m_currMap = mMapModel;
MigLayout myGrid = new MigLayout();

if(isExistingFile) {
for(int k=0;k < m_currmap.rSize;k++) {
for(int i=0;i < m_currmap.cSize;i++) {
if(pathTempValue < m_currmap.GetMapArray()[k][i] && m_currmap.GetMapArray()[k][i] != 9999)
pathTempValue = m_currmap.GetMapArray()[k][i];
for(int k=0;k < m_currMap.rSize;k++) {
for(int i=0;i < m_currMap.cSize;i++) {
if(pathTempValue < m_currMap.GetMapArray()[k][i] && m_currMap.GetMapArray()[k][i] != 9999)
pathTempValue = m_currMap.GetMapArray()[k][i];
}
}
pathTempValue++;
Expand All @@ -126,7 +127,7 @@ public void windowClosing(WindowEvent arg0) {
panel_1.setLayout(myGrid);
panel_1.setLayout(new MigLayout());

panelsHolder = new JPanel[m_currmap.rSize][m_currmap.cSize];
panelsHolder = new JPanel[m_currMap.rSize][m_currMap.cSize];
DrawMap(isExistingFile, panel_1);

setTitle("Tower Defence - Map Maker");
Expand Down Expand Up @@ -232,18 +233,18 @@ public void click(MouseEvent e, JPanel cell) {
int y = Integer.parseInt(String.valueOf(name_exploded[1]));
//1=StartPoint, 9999=End, 2=Path, 3=Delete
if(selectedTool== Util.TOOL_DELETE) {
if(m_currmap.GetMapArray()[x][y] == 1) {
if(m_currMap.GetMapArray()[x][y] == 1) {
isStartAdded = false;
buttonStart.setEnabled(true);
} else if(m_currmap.GetMapArray()[x][y] == Util.POINT_EXIT) {
} else if(m_currMap.GetMapArray()[x][y] == Util.POINT_EXIT) {
isEndAdded = false;
buttonEnd.setEnabled(true);
}
else if(m_currmap.GetMapArray()[x][y] > 1) {
else if(m_currMap.GetMapArray()[x][y] > 1) {
pathTempValue--;
}

m_currmap.DeleteFromMap(x, y);
m_currMap.DeleteFromMap(x, y);

cell.removeAll();
cell.setBackground(null);
Expand All @@ -258,7 +259,7 @@ else if(m_currmap.GetMapArray()[x][y] > 1) {
buttonStart.setEnabled(false);

} else if(selectedTool == Util.TOOL_POINT_PATH) {
if(m_currmap.GetMapArray()[x][y] != 1 && m_currmap.GetMapArray()[x][y] != Util.POINT_EXIT) {
if(m_currMap.GetMapArray()[x][y] != 1 && m_currMap.GetMapArray()[x][y] != Util.POINT_EXIT) {
DrawMapItem(2, cell);
// mapArray[x][y] = pathTempValue;
// pathTempValue++;
Expand All @@ -275,10 +276,10 @@ else if(m_currmap.GetMapArray()[x][y] > 1) {

if(selectedTool != 3 && !overideExisting) {
if(selectedTool==2) {
m_currmap.AddToMap(pathTempValue, x, y);
m_currMap.AddToMap(pathTempValue, x, y);
pathTempValue++;
} else {
m_currmap.AddToMap(selectedTool, x, y);
m_currMap.AddToMap(selectedTool, x, y);
}
}

Expand Down Expand Up @@ -323,11 +324,11 @@ private void DrawMapItem(int type, JPanel cell) {

private void DrawMap(boolean isExisting, Panel parentPanel) {
if(isExisting) {
for(int k=0;k < m_currmap.rSize;k++) {
for(int i=0;i < m_currmap.cSize;i++) {
for(int k=0;k < m_currMap.rSize;k++) {
for(int i=0;i < m_currMap.cSize;i++) {

String _append = "";
if(i == m_currmap.cSize-1) {
if(i == m_currMap.cSize-1) {
_append = ", wrap";
} else {

Expand Down Expand Up @@ -360,17 +361,17 @@ public void mouseClicked(MouseEvent e) {
}
});

if(m_currmap.GetMapArray()[k][i] == 1) {
if(m_currMap.GetMapArray()[k][i] == 1) {
DrawMapItem(1, temp);
isStartAdded = true;
buttonStart.setEnabled(false);

} else if(m_currmap.GetMapArray()[k][i] == 9999) {
} else if(m_currMap.GetMapArray()[k][i] == 9999) {
DrawMapItem(9999, temp);
isEndAdded = true;
buttonEnd.setEnabled(false);

} else if(m_currmap.GetMapArray()[k][i] == 0) {
} else if(m_currMap.GetMapArray()[k][i] == 0) {
DrawMapItem(0, temp);
} else {
DrawMapItem(2,temp);
Expand All @@ -382,10 +383,10 @@ public void mouseClicked(MouseEvent e) {
}

} else {
for(int k=0;k<m_currmap.rSize;k++) {
for(int i=0;i<m_currmap.cSize;i++) {
for(int k=0;k<m_currMap.rSize;k++) {
for(int i=0;i<m_currMap.cSize;i++) {
String _append = "";
if(i==m_currmap.cSize-1) {
if(i==m_currMap.cSize-1) {
_append = ", wrap";
} else {

Expand Down
52 changes: 27 additions & 25 deletions src/code/map/MyGuiFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
public class MyGuiFile extends JFrame {

// class variable declarations
private JPanel m_contentpane;
private JComboBox<String> m_combobox;
private JPanel m_contentPane;
private JComboBox<String> m_comboBox;
private final String DEFAULTFILEPATH = System.getProperty("user.dir") + "/maps";
private MapModel m_mapObj;

Expand Down Expand Up @@ -95,16 +95,16 @@ public void readMapFrmFile(String filename, String path) {

public void updateTxtPn() {
//m_comboBox
m_combobox.removeAllItems();
m_combobox.addItem(" ");
m_comboBox.removeAllItems();
m_comboBox.addItem(" ");

try{
File folder = new File(DEFAULTFILEPATH);
File[] listOfFiles = folder.listFiles();

for (File file : listOfFiles) {
if (file.isFile() && file.getName().endsWith(".map")) {
m_combobox.addItem(file.getName());
m_comboBox.addItem(file.getName());
}
}
}catch(Exception ex){}
Expand All @@ -114,16 +114,18 @@ public void updateTxtPn() {
* To create the new map dialog
*/
private void createNewMap() {
NewMapDialog mapDlg = new NewMapDialog();
mapDlg.setVisible(true);
NewMapDialog mapDialog = new NewMapDialog();
mapDialog.setVisible(true);

int rownum = (int) mapDlg.row_input.getValue();
int colnum = (int) mapDlg.col_input.getValue();
int rowNum = (int) mapDialog.row_input.getValue();
int colNum = (int) mapDialog.col_input.getValue();

if(mapDlg.IsCompleted) {
m_mapObj = new MapModel(rownum, colnum);
if(rowNum>9 || colNum>9){
JOptionPane.showMessageDialog(rootPane, "Map's height of width cannot exceed 9 block. Please try again.");
}else if(mapDialog.isCompleted) {
m_mapObj = new MapModel(rowNum, colNum);
new MapMaker(m_mapObj, false, this).setVisible(true);
}
}

}

Expand Down Expand Up @@ -179,34 +181,34 @@ public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(rootPane, "This application is used for building a map for the Tower Defence Game.\r\n Built by:\r\nMuhammad Umer\r\nLokesh\r\nIftekhar Ahmed");
}
});

mnHelp.add(mntmAbout);
m_contentpane = new JPanel();
m_contentpane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(m_contentpane);
m_contentpane.setLayout(new BorderLayout(0, 0));
m_contentPane = new JPanel();
m_contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(m_contentPane);
m_contentPane.setLayout(new BorderLayout(0, 0));

JLabel lblNewLabel = new JLabel("Please select a file from below or an option from the file menu.");
lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 18));
m_contentpane.add(lblNewLabel, BorderLayout.NORTH);
m_contentPane.add(lblNewLabel, BorderLayout.NORTH);

JPanel panel = new JPanel();
m_contentpane.add(panel, BorderLayout.CENTER);
m_contentPane.add(panel, BorderLayout.CENTER);
panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

m_combobox = new JComboBox<String>();
m_combobox.setModel(new DefaultComboBoxModel<String>(new String[] {" "}));
panel.add(m_combobox);
m_comboBox = new JComboBox<String>();
m_comboBox.setModel(new DefaultComboBoxModel<String>(new String[] {" "}));
panel.add(m_comboBox);

JButton btnNewButton = new JButton("Open File");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if(m_combobox.getItemCount() == 1 || m_combobox.getSelectedItem() == null)
if(m_comboBox.getItemCount() == 1 || m_comboBox.getSelectedItem() == null)
return;
if(((String)m_combobox.getSelectedItem()).trim().length() == 0)
if(((String)m_comboBox.getSelectedItem()).trim().length() == 0)
return;

readMapFrmFile((String)m_combobox.getSelectedItem(), DEFAULTFILEPATH);
readMapFrmFile((String)m_comboBox.getSelectedItem(), DEFAULTFILEPATH);
new MapMaker(m_mapObj ,true, MyGuiFile.this).setVisible(true);
}
});
Expand Down
17 changes: 9 additions & 8 deletions src/code/map/NewMapDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class NewMapDialog extends JDialog {

public JSpinner col_input = new JSpinner();
public JSpinner row_input = new JSpinner();
public boolean IsCompleted = false;
public boolean isCompleted = false;

/**
* Launch the application.
Expand All @@ -48,16 +48,16 @@ public NewMapDialog() {
super((java.awt.Frame) null, true);
setModalityType(java.awt.Dialog.ModalityType.APPLICATION_MODAL);

setBounds(100, 100, 528, 362);
setBounds(100, 100, 628, 362);

getContentPane().setLayout(new BorderLayout());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
contentPanel.setLayout(null);

JLabel lblNewLabel = new JLabel("Please specify the size of the Map in terms of Rows & Columns");
JLabel lblNewLabel = new JLabel("Please specify the size of the Map in terms of Rows & Columns [MAX : 9]");
lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 15));
lblNewLabel.setBounds(15, 16, 476, 53);
lblNewLabel.setBounds(15, 16, 576, 70);
contentPanel.add(lblNewLabel);

JLabel lblRow = new JLabel("Row");
Expand All @@ -67,11 +67,11 @@ public NewMapDialog() {
JLabel lblColumn = new JLabel("Column");
lblColumn.setBounds(15, 174, 69, 20);
contentPanel.add(lblColumn);
row_input.setModel(new SpinnerNumberModel(2, 2, 9, 1));
row_input.setModel(new SpinnerNumberModel(4, 4, 9, 1));

row_input.setBounds(120, 109, 83, 26);
contentPanel.add(row_input);
col_input.setModel(new SpinnerNumberModel(2, 2, 9, 1));
col_input.setModel(new SpinnerNumberModel(4, 4, 9, 1));


col_input.setBounds(120, 171, 83, 26);
Expand All @@ -81,9 +81,10 @@ public NewMapDialog() {
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
getContentPane().add(buttonPane, BorderLayout.SOUTH);
JButton okButton = new JButton("OK");

okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
IsCompleted = true;
isCompleted = true;
setVisible(false);
dispose();
}
Expand All @@ -95,7 +96,7 @@ public void actionPerformed(ActionEvent e) {
JButton cancelButton = new JButton("Cancel");
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
IsCompleted = false;
isCompleted = false;
setVisible(false);
dispose();
}
Expand Down

0 comments on commit 7a3b748

Please sign in to comment.