Exemple #1
0
  public static void main(String[] args) {
    double n1;
    double n2;
    double n3;
    double sum;
    double average;

    String in, out;
    in = JOptionPane.showInputDialog("Enter the first number");
    n1 = Double.parseDouble(in);
    in = JOptionPane.showInputDialog("Enter the second number");
    n2 = Double.parseDouble(in);
    in = JOptionPane.showInputDialog("Enter the third number");
    n3 = Double.parseDouble(in);

    sum = n1 + n2 + n3;
    average = sum / 3;

    // Build the out String
    out = "";
    out = out + "The sum of " + n1 + "," + n2 + " and " + n3 + " is " + sum;
    out = out + "\nThe average of " + n1 + "," + n2 + " and " + n3 + " is " + average;

    JOptionPane.showMessageDialog(null, out);
    System.exit(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);
      }
    }
  }
Exemple #3
0
  // ask user for script file and name of new profile
  // return false if user cancelled operation
  private boolean getNewWkldInput(
      JFileChooser chooser, Object[] optionDlgMsg, StringBuffer name, StringBuffer scriptFile) {
    // let user select script file with queries
    boolean fileOk = false;
    int retval;
    File file = null;
    FileReader reader = null;
    while (!fileOk) {
      if ((retval = chooser.showDialog(this, "Ok")) != 0) {
        return false;
      }
      file = chooser.getSelectedFile();
      try {
        reader = new FileReader(file.getPath());
        fileOk = true;
        scriptFile.append(file.getPath());
      } catch (FileNotFoundException e) {
        JOptionPane.showMessageDialog(
            this,
            "Selected script file does not exist",
            "Error: New Profile",
            JOptionPane.ERROR_MESSAGE);
      }
    }

    // let user select filename for profile
    int response =
        JOptionPane.showOptionDialog(
            this,
            optionDlgMsg,
            "New Profile",
            JOptionPane.OK_CANCEL_OPTION,
            JOptionPane.PLAIN_MESSAGE,
            null,
            null,
            null);
    if (response != JOptionPane.OK_OPTION) {
      return false;
    }
    JTextField textFld = (JTextField) optionDlgMsg[1];
    name.append(file.getParent() + "/" + textFld.getText());
    return true;
  }
  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);
  }
 private void showErrorMessage(String message) {
   JOptionPane.showMessageDialog(
       null, message, ProvClientUtils.getString("Error"), JOptionPane.ERROR_MESSAGE);
 }