GladL33
GladL33

Reputation: 53

How can my program randomly press a button?

I'm trying to make a game where the button would light up and the user would have to press the button in a given time.

Currently, my program has 12 buttons that do something. I'm trying to make it so that these buttons are randomly called by the program. So far, I just have these for 12 buttons that just change the text when pressed by the user.

Now I need a way of making it so that they are randomly pressed the program itself and not the user. Any idea's on how this is done in java?

// **** Panels for buttons ****
        JPanel panelButtons = new JPanel();                         // making the panel for the buttons
        panelButtons.setLayout(new GridLayout(3, 4));               // setting the layout of the buttons to 3x4 as shown above

        b1 = new JButton(" ⃝");                                     // creating button and setting its default text
        b1.setFont(fontText);                                       // setting the font
        b1.addActionListener(new ActionListener(){                  // action listener to do something when pressed
            public void actionPerformed(ActionEvent e) {
                    sendMessage(user + "1" );                       // sends the name of the user that pressed the button and which button
                    String field1 = b1.getText();                   // gets the text from the button and stores it in a String
                    if(field1 == " ⃝"){                             // checks if the string is equal to an empty circle
                        b1.setText("⬤");                            // if true then change to a full circle
                    }
                    else if (field1 == "⬤"){                        // opposite of the above if statement
                        b1.setText(" ⃝");
                    }   
            }
        }); 
        panelButtons.add(b1);                                       // adding the button to the panel

        b2 = new JButton(" ⃝");                                     // creating button and setting its default text
        b2.setFont(fontText);                                       // setting the font
        b2.addActionListener(new ActionListener(){                  // action listener to do something when pressed
            public void actionPerformed(ActionEvent e) {            
                    sendMessage(user + "2" );                       // sends the name of the user that pressed the button and which button
                    String field2 = b2.getText();                   // gets the text from the button and stores it in a String
                    if(field2 == " ⃝"){                             // checks if the string is equal to an empty circle
                        b2.setText("⬤");                            // if true then change to a full circle
                    }
                    else if (field2 == "⬤"){                        // opposite of the above if statement
                        b2.setText(" ⃝");
                    }   
            }
        });
        panelButtons.add(b2);                                       // adding the button to the panel

Upvotes: 2

Views: 767

Answers (2)

charlieh_7
charlieh_7

Reputation: 334

  1. Put your twelve buttons in an ordered collection.
  2. Put your twelve corresponding actions in another ordered collection in form of a Consumer<JButton>'(useCallable`(and ignore the return) or just create something like that if you are not using java 8).
  3. Perform a mapping from the Button collection to the action collection.
  4. Create a class implementing ActionListener like this:

    private static class Listener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e) {
            button2action.get((JButton)e.getSource()).accept(a);
        }
    }
    
  5. pick up a random element from the value set of the map if you want to get random button pressed.

    int randomIndex = new random().nextInt(button2action.entrySet().size());
    Iterator<Entry<JButton, Consumer<JButton>>> iter = button2action.entrySet().iterator();
    for (int i = 0; i < randomIndex; i++){
        Entry<JButton, Consumer<JButton>> entry = iter.next();
    }
    entry.getValue().accept(entry.getKey()); 
    
  6. add new button action pairs to the map if you want to add new buttons.

Upvotes: 0

GhostCat
GhostCat

Reputation: 140457

You can create a list that holds your button. Use the random number generator to create a random number within the length of the list. Use that (random) index to modify the corresponding button.

Upvotes: 2

Related Questions