From 7a3b7482ec776d8da6104b4bfbbdddad9bb6d318 Mon Sep 17 00:00:00 2001 From: Armaghan Date: Thu, 17 Mar 2016 05:26:36 -0400 Subject: [PATCH] Min/Max Map conditions --- src/code/map/MapMaker.java | 53 +++++++++++++++++----------------- src/code/map/MyGuiFile.java | 52 +++++++++++++++++---------------- src/code/map/NewMapDialog.java | 17 ++++++----- 3 files changed, 63 insertions(+), 59 deletions(-) diff --git a/src/code/map/MapMaker.java b/src/code/map/MapMaker.java index 76a163c..fee8d23 100644 --- a/src/code/map/MapMaker.java +++ b/src/code/map/MapMaker.java @@ -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"); @@ -65,11 +65,11 @@ 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(); @@ -77,7 +77,7 @@ public boolean saveToFile() { 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."); @@ -103,6 +103,7 @@ 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) { @@ -110,14 +111,14 @@ public void windowClosing(WindowEvent arg0) { } }); - 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++; @@ -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"); @@ -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); @@ -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++; @@ -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); } } @@ -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 { @@ -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); @@ -382,10 +383,10 @@ public void mouseClicked(MouseEvent e) { } } else { - for(int k=0;k m_combobox; + private JPanel m_contentPane; + private JComboBox m_comboBox; private final String DEFAULTFILEPATH = System.getProperty("user.dir") + "/maps"; private MapModel m_mapObj; @@ -95,8 +95,8 @@ 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); @@ -104,7 +104,7 @@ public void updateTxtPn() { for (File file : listOfFiles) { if (file.isFile() && file.getName().endsWith(".map")) { - m_combobox.addItem(file.getName()); + m_comboBox.addItem(file.getName()); } } }catch(Exception ex){} @@ -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); - } + } } @@ -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(); - m_combobox.setModel(new DefaultComboBoxModel(new String[] {" "})); - panel.add(m_combobox); + m_comboBox = new JComboBox(); + m_comboBox.setModel(new DefaultComboBoxModel(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); } }); diff --git a/src/code/map/NewMapDialog.java b/src/code/map/NewMapDialog.java index 55112a8..42378a9 100644 --- a/src/code/map/NewMapDialog.java +++ b/src/code/map/NewMapDialog.java @@ -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. @@ -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"); @@ -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); @@ -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(); } @@ -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(); }