public void insertBuilding(int i, int j, int index) { if (!tiles[i][j].getValue().equals("")) { JOptionPane.showMessageDialog(null, "There is already a building here"); return; } String temp = bb.get(index).getText(); String[] parts = temp.split("-"); int newQ = Integer.parseInt(parts[1]) - 1; if (newQ < 0) return; Building tB = bList.get(0); for (int b = 0; b < bList.size(); b++) { if (bList.get(b).getName().equals(parts[0])) { tB = bList.get(b); // System.out.println("here"); break; } } Image image = null; BufferedImage buffered; try { image = ImageIO.read(new File("images/" + tB.getName().replace(" ", "_") + ".png")); System.out.println("Loaded image"); } catch (IOException e) { System.out.println("Failed to load image"); } buffered = (BufferedImage) image; for (int c = 0; c < tB.getQWidth(); c++) { for (int r = 0; r < tB.getQHeight(); r++) { if (i + c == 40 || j + r == 40) { JOptionPane.showMessageDialog(null, "Placing a building here would be out of bounds"); return; } if (!tiles[i + c][j + r].getValue().equals("")) { JOptionPane.showMessageDialog(null, "Placing a building here will result to a overlap"); return; } } } double w = buffered.getWidth() / tB.getQWidth(); double h = buffered.getHeight() / tB.getQHeight(); for (int c = 0; c < tB.getQWidth(); c++) { for (int r = 0; r < tB.getQHeight(); r++) { tiles[i + c][j + r].setBackground(new Color(51, 204, 51)); tiles[i + c][j + r].setIcon( new ImageIcon( resize( buffered.getSubimage((int) w * r, (int) h * c, (int) w, (int) h), tiles[i + c][j + r].getWidth(), tiles[i + c][j + r].getHeight()))); String tValue = (c == 0 && r == 0) ? tB.getName() : i + "-" + j; // System.out.println(tValue); tiles[i + c][j + r].setValue(tValue); } } // tiles[i][j].setBackground(Color.BLUE); bb.get(index).setText(parts[0] + "-" + newQ); }