コード例 #1
0
  public void removeBuilding(int i, int j) {

    if (tiles[i][j].getValue().equals("")) {
      JOptionPane.showMessageDialog(null, "There is no building here");
      return;
    }

    if (tiles[i][j].value.contains("-")) {
      String tText = tiles[i][j].value;
      i = Integer.parseInt(tText.split("-")[0]);
      j = Integer.parseInt(tText.split("-")[1]);
    }

    int index;
    for (index = 0; index < bb.size(); index++) {
      if (bb.get(index).getText().split("-")[0].equals(tiles[i][j].value)) {
        break;
      }
    }

    String temp = bb.get(index).getText();
    String[] parts = temp.split("-");
    int newQ = Integer.parseInt(parts[1]) + 1;

    Building tB = bList.get(0);
    for (int b = 0; b < bList.size(); b++) {
      if (bList.get(b).getName().equals(tiles[i][j].value)) {
        tB = bList.get(b);
        break;
      }
    }

    for (int c = 0; c < tB.getQWidth(); c++) {
      for (int r = 0; r < tB.getQHeight(); r++) {

        if ((i + c + j + r) % 2 == 0) tiles[i + c][j + r].setBackground(new Color(102, 255, 51));
        else tiles[i + c][j + r].setBackground(new Color(51, 204, 51));

        tiles[i + c][j + r].setIcon(null);

        tiles[i + c][j + r].value = "";
        bb.get(index).setText(parts[0] + "-" + newQ);
      }
    }
  }
コード例 #2
0
  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);
  }