waiting for a mouse click

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tommyny04
    New Member
    • Sep 2007
    • 6

    #1

    waiting for a mouse click

    Really need help with this one. I'm writing a program to simulate a Risk game and I need to write a method to make two countries neighbors, which would be done by adding each the neighbor country to each country's neighbor's array. To do this, I will need to click on the country, click on the map and then click on the other country. My method gets the country's name but how do I tell it to wait for a mouse click before trying to get the second country's name???? I've tried everything but to no avail. Any help would be appreciated.

    [CODE=java]public void addNeighbor(Cou ntry country) {

    boolean exists, click;

    Country tempCountry;
    tempCountry = searchCountry(r ed, green, blue);

    if (country == tempCountry){
    JOptionPane.sho wMessageDialog( null, "Error: Territories are the same. Please try again");
    }
    else {
    exists = country.borderE xists(tempCount ry);
    if (exists){
    JOptionPane.sho wMessageDialog( null, "Error: Territories are already borders.");
    }
    else {
    country.addBord erArray(tempCou ntry);
    tempCountry.add BorderArray(cou ntry);
    JOptionPane.sho wMessageDialog( null, "Border successfull added.");
    }

    }


    }[/CODE]
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    I believe you need a MouseListener, though I've never actually used one, I've seen them used in simulations of games like Concentration.

    Comment

    • eeriehunk
      New Member
      • Sep 2007
      • 55

      #3
      Originally posted by tommyny04
      Really need help with this one. I'm writing a program to simulate a Risk game and I need to write a method to make two countries neighbors, which would be done by adding each the neighbor country to each country's neighbor's array. To do this, I will need to click on the country, click on the map and then click on the other country. My method gets the country's name but how do I tell it to wait for a mouse click before trying to get the second country's name???? I've tried everything but to no avail. Any help would be appreciated.

      [CODE=java]public void addNeighbor(Cou ntry country) {

      boolean exists, click;

      Country tempCountry;
      tempCountry = searchCountry(r ed, green, blue);

      if (country == tempCountry){
      JOptionPane.sho wMessageDialog( null, "Error: Territories are the same. Please try again");
      }
      else {
      exists = country.borderE xists(tempCount ry);
      if (exists){
      JOptionPane.sho wMessageDialog( null, "Error: Territories are already borders.");
      }
      else {
      country.addBord erArray(tempCou ntry);
      tempCountry.add BorderArray(cou ntry);
      JOptionPane.sho wMessageDialog( null, "Border successfull added.");
      }

      }


      }[/CODE]
      To use an action listener you have to instantiate a button class (not just a button, you can use many others like check box or radio button or more). Check this sample code of a JButton class click me

      Comment

      Working...