diff --git a/src/code/map/MapMaker.java b/src/code/map/MapMaker.java index fe0d219..361cf83 100644 --- a/src/code/map/MapMaker.java +++ b/src/code/map/MapMaker.java @@ -43,8 +43,8 @@ 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 Panel panel_1 = new Panel(); // panel to hold the map grid + private MapModel currMap; // MapModel class to hold the map object + private Panel mapPanel = 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"); private JButton buttonEnd = new JButton("End 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(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(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(currMap.GetMapArray()); oos.close(); fos.close(); JOptionPane.showMessageDialog(null, "Your map is saved successfully."); @@ -112,24 +112,24 @@ public void windowClosing(WindowEvent arg0) { } }); - m_currMap = mMapModel; + 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 < currMap.rSize;k++) { + for(int i=0;i < currMap.cSize;i++) { + if(pathTempValue < currMap.GetMapArray()[k][i] && currMap.GetMapArray()[k][i] != 9999) + pathTempValue = currMap.GetMapArray()[k][i]; } } pathTempValue++; } - panel_1.setLayout(myGrid); - panel_1.setLayout(new MigLayout()); + mapPanel.setLayout(myGrid); + mapPanel.setLayout(new MigLayout()); - panelsHolder = new JPanel[m_currMap.rSize][m_currMap.cSize]; - DrawMap(isExistingFile, panel_1); + panelsHolder = new JPanel[currMap.rSize][currMap.cSize]; + DrawMap(isExistingFile, mapPanel); setTitle("Tower Defence - Map Maker"); //setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); @@ -219,7 +219,7 @@ public void actionPerformed(ActionEvent e) { ScrollPane sc_panel = new ScrollPane(); sc_panel.setBounds(10, 172, 914, 527); - sc_panel.add(panel_1,null); + sc_panel.add(mapPanel,null); contentPane.add(sc_panel); } @@ -242,23 +242,23 @@ public void click(MouseEvent e, JPanel cell) { //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) { + if(currMap.GetMapArray()[x][y] == 1) { isStartAdded = false; buttonStart.setEnabled(true); - } else if(m_currMap.GetMapArray()[x][y] == Util.POINT_EXIT) { + } else if(currMap.GetMapArray()[x][y] == Util.POINT_EXIT) { isEndAdded = false; buttonEnd.setEnabled(true); } - else if(m_currMap.GetMapArray()[x][y] > 1) { + else if(currMap.GetMapArray()[x][y] > 1) { pathTempValue--; } - m_currMap.DeleteFromMap(x, y); + currMap.DeleteFromMap(x, y); cell.removeAll(); cell.setBackground(null); - panel_1.revalidate(); - panel_1.repaint(); + mapPanel.revalidate(); + mapPanel.repaint(); } else { // if the other tool is selected, it is handled here @@ -269,7 +269,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(currMap.GetMapArray()[x][y] != 1 && currMap.GetMapArray()[x][y] != Util.POINT_EXIT) { DrawMapItem(2, cell); // mapArray[x][y] = pathTempValue; // pathTempValue++; @@ -287,10 +287,10 @@ 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); + currMap.AddToMap(pathTempValue, x, y); pathTempValue++; } else { - m_currMap.AddToMap(selectedTool, x, y); + currMap.AddToMap(selectedTool, x, y); } } @@ -336,11 +336,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 < currMap.rSize;k++) { + for(int i=0;i < currMap.cSize;i++) { String _append = ""; - if(i == m_currMap.cSize-1) { + if(i == currMap.cSize-1) { _append = ", wrap"; } else { @@ -375,17 +375,17 @@ public void mouseClicked(MouseEvent e) { }); // drawing the current map model on the grid - if(m_currMap.GetMapArray()[k][i] == 1) { + if(currMap.GetMapArray()[k][i] == 1) { DrawMapItem(1, temp); isStartAdded = true; buttonStart.setEnabled(false); - } else if(m_currMap.GetMapArray()[k][i] == 9999) { + } else if(currMap.GetMapArray()[k][i] == 9999) { DrawMapItem(9999, temp); isEndAdded = true; buttonEnd.setEnabled(false); - } else if(m_currMap.GetMapArray()[k][i] == 0) { + } else if(currMap.GetMapArray()[k][i] == 0) { DrawMapItem(0, temp); } else { DrawMapItem(2,temp); @@ -397,10 +397,10 @@ public void mouseClicked(MouseEvent e) { } } else { - for(int k=0;k m_comboBox; - private final String DEFAULTFILEPATH = System.getProperty("user.dir") + "/maps"; - private MapModel m_mapObj; + private JPanel contentPane; + private JComboBox comboBox; + private final String DEFAULT_FILE_PATH = System.getProperty("user.dir") + "/maps"; + private MapModel mapObj; - public MapModel getMapModelObj() {return m_mapObj;} + public MapModel getMapModelObj() {return mapObj;} /** * Main method of the class where the execution begins. The applications is invoked from this main method. It takes command line arguments @@ -85,7 +85,7 @@ public void readMapFrmFile(String filename, String path) { ois.close(); fis.close(); - m_mapObj = new MapModel(filename.substring(0, filename.length() - 4), maparray); + mapObj = new MapModel(filename.substring(0, filename.length() - 4), maparray); }catch(IOException ioe){ ioe.printStackTrace(); @@ -102,17 +102,17 @@ public void readMapFrmFile(String filename, String path) { */ public void updateTxtPn() { //m_comboBox - m_comboBox.removeAllItems(); - m_comboBox.addItem(" "); + comboBox.removeAllItems(); + comboBox.addItem(" "); try{ - File folder = new File(DEFAULTFILEPATH); + File folder = new File(DEFAULT_FILE_PATH); File[] listOfFiles = folder.listFiles(); for (File file : listOfFiles) { if (file.isFile() && file.getName().endsWith(".map")) { // file in the default directory - m_comboBox.addItem(file.getName()); + comboBox.addItem(file.getName()); } } }catch(Exception ex){} @@ -126,14 +126,14 @@ private void createNewMap() { NewMapDialog mapDialog = new NewMapDialog(); mapDialog.setVisible(true); - int rowNum = (int) mapDialog.row_input.getValue(); - int colNum = (int) mapDialog.col_input.getValue(); + int rowNum = (int) mapDialog.rowInput.getValue(); + int colNum = (int) mapDialog.colInput.getValue(); 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); + mapObj = new MapModel(rowNum, colNum); + new MapMaker(mapObj, false, this).setVisible(true); } } @@ -169,12 +169,12 @@ public void actionPerformed(ActionEvent arg0) { public void actionPerformed(ActionEvent e) { JFileChooser fileChooser = new JFileChooser(); - fileChooser.setCurrentDirectory(new File(DEFAULTFILEPATH)); + fileChooser.setCurrentDirectory(new File(DEFAULT_FILE_PATH)); if (fileChooser.showOpenDialog(MyGuiFile.this) == JFileChooser.APPROVE_OPTION) { File file = fileChooser.getSelectedFile(); readMapFrmFile(file.getName(), file.getParent()); - new MapMaker(m_mapObj ,true, MyGuiFile.this).setVisible(true); + new MapMaker(mapObj ,true, MyGuiFile.this).setVisible(true); } } }); @@ -195,34 +195,34 @@ public void actionPerformed(ActionEvent e) { }); 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)); + contentPane = new JPanel(); + contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); + setContentPane(contentPane); + 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); + contentPane.add(lblNewLabel, BorderLayout.NORTH); JPanel panel = new JPanel(); - m_contentPane.add(panel, BorderLayout.CENTER); + 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); + comboBox = new JComboBox(); + comboBox.setModel(new DefaultComboBoxModel(new String[] {" "})); + panel.add(comboBox); JButton btnNewButton = new JButton("Open File"); // action listener for open file button to open the file selected in the combo box. btnNewButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { - if(m_comboBox.getItemCount() == 1 || m_comboBox.getSelectedItem() == null) + if(comboBox.getItemCount() == 1 || comboBox.getSelectedItem() == null) return; - if(((String)m_comboBox.getSelectedItem()).trim().length() == 0) + if(((String)comboBox.getSelectedItem()).trim().length() == 0) return; - readMapFrmFile((String)m_comboBox.getSelectedItem(), DEFAULTFILEPATH); - new MapMaker(m_mapObj ,true, MyGuiFile.this).setVisible(true); + readMapFrmFile((String)comboBox.getSelectedItem(), DEFAULT_FILE_PATH); + new MapMaker(mapObj ,true, MyGuiFile.this).setVisible(true); } }); panel.add(btnNewButton); diff --git a/src/code/map/NewMapDialog.java b/src/code/map/NewMapDialog.java index 798fe26..4e5730d 100644 --- a/src/code/map/NewMapDialog.java +++ b/src/code/map/NewMapDialog.java @@ -26,8 +26,8 @@ public class NewMapDialog extends JDialog { // attributes of the class goes here private JPanel contentPanel = new JPanel(); - public JSpinner col_input = new JSpinner(); - public JSpinner row_input = new JSpinner(); + public JSpinner colInput = new JSpinner(); + public JSpinner rowInput = new JSpinner(); public boolean isCompleted = false; /** @@ -74,15 +74,15 @@ public NewMapDialog() { JLabel lblColumn = new JLabel("Column"); lblColumn.setBounds(15, 174, 69, 20); contentPanel.add(lblColumn); - row_input.setModel(new SpinnerNumberModel(4, 4, 9, 1)); + rowInput.setModel(new SpinnerNumberModel(4, 4, 9, 1)); - row_input.setBounds(120, 109, 83, 26); - contentPanel.add(row_input); - col_input.setModel(new SpinnerNumberModel(4, 4, 9, 1)); + rowInput.setBounds(120, 109, 83, 26); + contentPanel.add(rowInput); + colInput.setModel(new SpinnerNumberModel(4, 4, 9, 1)); - col_input.setBounds(120, 171, 83, 26); - contentPanel.add(col_input); + colInput.setBounds(120, 171, 83, 26); + contentPanel.add(colInput); JPanel buttonPane = new JPanel(); buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));