Beispiel #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);
  }
  private void setExceptionPane(Object msg) {
    // If debug is enabled display dialog.
    if (debugEnabled) {
      // Display message and retrieve selected value.
      int result =
          JOptionPane.showConfirmDialog(this, msg, "Information", JOptionPane.YES_NO_OPTION);

      // Display message and retrieve selected value.
      switch (result) {
        case JOptionPane.CLOSED_OPTION:
          resetExceptionString();
          break;

        case JOptionPane.YES_OPTION:
          getStackTraceDialog();
          break;

        case JOptionPane.NO_OPTION:
          resetExceptionString();
          break;
      } // End of result evaluation switch.

    } // End of if debugEnabled evaluation.
    else {
      // Disgard the error and reset the class instance exception String.
      // This resets the value to a zero length String.
      resetExceptionString();
    } // End of else debugEnabled evaluation.
  } // End of setExceptionPane() method.
Beispiel #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 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);
      }
    }
  }
Beispiel #5
0
 protected boolean informUserError(String msg) // this is just for SPPmon
     {
   if (!ExpCoordinator.isSPPMon()) return false;
   final String opt0 = "Try Again";
   final String opt1 = "Cancel";
   JLabel lbl = new JLabel(msg);
   TextFieldwLabel tfaddress = new TextFieldwLabel(30, "daemon addr:");
   tfaddress.setText(host);
   TextFieldwLabel tfport = new TextFieldwLabel(30, "daemon port:");
   tfport.setText(String.valueOf(port));
   Object[] params = {lbl, tfaddress, tfport};
   Object[] options = {opt0, opt1};
   int rtn =
       JOptionPane.showOptionDialog(
           ExpCoordinator.getMainWindow(),
           params,
           "Connection Error",
           JOptionPane.YES_NO_OPTION,
           JOptionPane.WARNING_MESSAGE,
           null,
           options,
           options[0]);
   if (rtn == JOptionPane.NO_OPTION) {
     connectionFailed();
     return false;
   } else {
     String tmp_host = host;
     boolean change = false;
     if (tfaddress.getText().length() > 0) tmp_host = tfaddress.getText();
     int tmp_port = port;
     if (tfport.getText().length() > 0) tmp_port = Integer.parseInt(tfport.getText());
     if (!tmp_host.equals(host)) {
       host = new String(tmp_host);
       change = true;
     }
     if (port != tmp_port) {
       port = tmp_port;
       change = true;
     }
     if (change) fireEvent(new ConnectionEvent(this, ConnectionEvent.ADDRESS_CHANGED));
     state = ConnectionEvent.CONNECTION_CLOSED;
     return (connect());
   }
 }
  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);
  }
Beispiel #7
0
    public void actionPerformed(ActionEvent ae) {
      String cmd = ae.getActionCommand().intern();
      if (ae.getSource() == butChannel) {
        String pass = null;
        String channel = (String) cboChannels.getSelectedItem();
        if (channels.containsKey(channel) && ((Boolean) channels.get(channel)).booleanValue()) {
          String message = "Password for " + channel + "? (blank for no password)";
          pass =
              JOptionPane.showInputDialog(
                  (Component) ae.getSource(),
                  message,
                  "Join Channel",
                  JOptionPane.QUESTION_MESSAGE);
        }
        server.writeObject(new SD_Channel(false, channel, ("".equals(pass) ? null : pass)));
        showUserStatus = false;
      } else if (ae.getSource() == butCreate) {
        String channel =
            JOptionPane.showInputDialog(
                (Component) ae.getSource(),
                "Enter the name of the channel",
                "Create Channel",
                JOptionPane.INFORMATION_MESSAGE);
        if (channel == null) return;
        String message = "Password for " + channel + "? (blank for no password)";
        String pass =
            JOptionPane.showInputDialog(
                (Component) ae.getSource(), message, "Join Channel", JOptionPane.QUESTION_MESSAGE);
        server.writeObject(new SD_Channel(true, channel, ("".equals(pass) ? null : pass)));
        showUserStatus = false;
        // ---------- Invite a Specific User
      } else if (ae.getSource() == butInvite) {
        String invite =
            JOptionPane.showInputDialog(
                (Component) ae.getSource(),
                "Enter the name of the User to Invite",
                "Invite User",
                JOptionPane.INFORMATION_MESSAGE);
        if (invite == null) return;
        messageText.setText("\\invite " + invite);
        sendMessage();

      } else if (cmd == "whisper") {
        String user = (String) userList.getSelectedValue();
        if (user != null && !messageText.getText().equals("") && !user.equals(username)) {
          messageText.setText("\\whisper " + user + " " + messageText.getText());
          sendMessage();
        } else {
          error(
              "invalid user or no message, type a message below,"
                  + " select a user, and then whisper");
        }
      } else if (cmd == "private message") {
        String user = (String) userList.getSelectedValue();
        if (user != null && !user.equals(username)) {
          privates.newPrivate(user);
        } else {
          error("invalid user");
        }
      } else if (ae.getActionCommand().equals("ignore")) {
        String user = (String) userList.getSelectedValue();
        if (user != null) {
          ignore(user, false);
        } else {
          error("no user selected");
        }
      } else if (cmd == "clear ignore list") {
        ignores.clear();
        updateList();
        serverMessage("ignore list cleared");
      } else if (cmd == "kick user") {
        String user = (String) userList.getSelectedValue();
        if (user != null) {
          if (user.equals(username)) {
            error("cannot kick yourself");
          } else {
            server.writeObject(new SD_Kick(user));
          }
        } else {
          error("no user selected");
        }
      }
    }
Beispiel #8
0
  /** Initializes the graphical components */
  public void init() {
    username = getParameter("username");
    if (username == null) {
      username =
          JOptionPane.showInputDialog(
              this, "Please enter a username", "Login", JOptionPane.QUESTION_MESSAGE);
    }
    try {
      PORT = Integer.valueOf(getParameter("port")).intValue();
    } catch (NumberFormatException e) {
      PORT = 42412;
    }

    URL url = getDocumentBase();
    site = url.getHost();
    locationURL = "http://" + site + ":" + url.getPort() + "/" + url.getPath();

    setSize(615, 362);
    c = getContentPane();

    c.setBackground(new Color(224, 224, 224));

    if (site == null || locationURL == null) {
      c.add(new JLabel("ERROR: did not recieve needed data from page"));
    }

    myAction = new MyAction();
    myKeyListener = new MyKeyListener();
    myMouseListener = new MyMouseListener();
    myHyperlinkListener = new MyHyperlinkListener();

    c.setLayout(null);

    cboChannels = new JComboBox();
    cboChannels.setBounds(5, 5, 150, 24);

    butChannel = new JButton("Join");
    butChannel.setToolTipText("Join channel");
    butChannel.addActionListener(myAction);
    butChannel.setBounds(160, 5, 60, 24);

    butCreate = new JButton("Create");
    butCreate.setToolTipText("Create new channel");
    butCreate.addActionListener(myAction);
    butCreate.setBounds(230, 5, 100, 24);
    butCreate.setEnabled(false);

    butInvite = new JButton("Invite");
    butInvite.setToolTipText("Invite Friend");
    butInvite.addActionListener(myAction);
    butInvite.setBounds(340, 5, 80, 24);

    mainChat = new ChatPane(this);
    textScroller =
        new JScrollPane(
            mainChat,
            JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
            JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    textScroller.setBounds(5, 34, 500, 270);

    userList = new JList();
    userList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    userList.setCellRenderer(new MyCellRenderer());
    userList.setBackground(new Color(249, 249, 250));
    JScrollPane userScroller = new JScrollPane(userList);
    userScroller.setBounds(510, 34, 100, 297);

    messageText = new JTextField();
    messageText.setBounds(5, 309, 500, 22);
    messageText.setColumns(10);
    messageText.setBackground(new Color(249, 249, 250));

    JMenuItem item;
    popup = new JPopupMenu("test");
    popup.add("whisper").addActionListener(myAction);
    popup.add("private message").addActionListener(myAction);
    popup.add("ignore").addActionListener(myAction);
    popup.add("clear ignore list").addActionListener(myAction);

    conNo = new ImageIcon(getURL("images/connect_no.gif"));
    conYes = new ImageIcon(getURL("images/connect_established.gif"));
    secNo = new ImageIcon(getURL("images/decrypted.gif"));
    secYes = new ImageIcon(getURL("images/encrypted.gif"));

    conIcon = new JLabel(conNo);
    conIcon.setBorder(new EtchedBorder());
    secIcon = new JLabel(secNo);
    secIcon.setBorder(new EtchedBorder());

    conIcon.setBounds(563, 334, 22, 22);
    secIcon.setBounds(588, 334, 22, 22);

    bottomText =
        new JLabel(
            "<html><body><font color=#445577><b>"
                + "LlamaChat "
                + VERSION
                + "</b></font> &nbsp;&copy; "
                + "<a href=\""
                + linkURL
                + "\">Joseph Monti</a> 2002-2003"
                + "</body></html>");
    bottomText.setBounds(5, 336, 500, 20);

    c.add(cboChannels);
    c.add(butChannel);
    c.add(butCreate);
    c.add(butInvite);
    c.add(textScroller);
    c.add(userScroller);
    c.add(messageText);
    c.add(conIcon);
    c.add(secIcon);
    c.add(bottomText);

    userList.addMouseListener(myMouseListener);
    messageText.addKeyListener(myKeyListener);
    bottomText.addMouseListener(myMouseListener);

    users = new ArrayList();
    ignores = new ArrayList(5);
    afks = new ArrayList(5);
    admins = new ArrayList(5);
    history = new CommandHistory(10);
    admin = false;
    channels = new Hashtable();
    privates = new PrivateMsg(this);
    showUserStatus = false;

    myColors[0] = new Color(200, 0, 0);
    myColors[1] = new Color(0, 150, 0);
    myColors[2] = new Color(0, 0, 200);

    rect = new Rectangle(0, 0, 1, 1);

    String opening =
        "<font color=#333333>"
            + "==================================<br>"
            + "Welcome to LlamaChat "
            + VERSION
            + "<br>"
            + "If you need assistance, type \\help<br>"
            + "Enjoy your stay!<br>"
            + "Maestria Aplicada en Redes<br>"
            + "==================================<br></font>";
    HTMLDocument doc = (HTMLDocument) mainChat.getDocument();
    HTMLEditorKit kit = (HTMLEditorKit) mainChat.getEditorKit();
    try {
      kit.insertHTML(doc, doc.getLength(), opening, 0, 0, HTML.Tag.FONT);
    } catch (Throwable t) {
      t.printStackTrace(System.out);
    }

    // validate the name
    if (!username.matches("[\\w_-]+?")) {
      error(
          "username contains invalid characters, changing to "
              + "'invalid' for now. "
              + "Type \\rename to chose a new name");
      username = "******";
    }
    if (username.length() > 10) {
      username = username.substring(0, 10);
      error("username too long, changed to " + username);
    }

    connect();
  }
 private void showErrorMessage(String message) {
   JOptionPane.showMessageDialog(
       null, message, ProvClientUtils.getString("Error"), JOptionPane.ERROR_MESSAGE);
 }
Beispiel #10
0
 public PowerSupply() {
   super();
   String str = JOptionPane.showInputDialog("Enter frequency");
   this.frequency = Double.parseDouble(str);
   this.impedance = new Complex(0.0, 0.0);
 }