dimanche 25 avril 2021

Java simplifying a repetitive code snippet to a function within ActionListener

I am wondering what is an elegant and effective way of simplifying the following snippet of code. Since I have many more buttons and their mechanics all behave in the same way, the symmetry of the methods, and the alternation from color.green -> color.red suggests there may exist a way of simplifying this down to a function?

I have been scratching my head on this design problem for a while, the way I am coding it seems definitely wrong and cumbersome.

GameFrame Class

public class GameFrame extends JFrame{

// (...)
static void initializeComponents(GameFrame frame, GamePanel GamePanel) {

// (...)
ArrayList<JGradientButton> buttons = new ArrayList<JGradientButton>();
Collections.addAll(buttons, b1,b2,b3,b4,b5);
for(JGradientButton button : buttons) {
            button.addActionListener(new ActionListener() {
                
                @Override
                public void actionPerformed(ActionEvent e) {
                    if(button == b1) {
                        GamePanel.b1Pressed();
                        
                    } else if (button == b2) {
                        GamePanel.b2Pressed();

                        if(GamePanel.removeFlag) {
                            button.color = Color.green;
                        } else {
                            button.color = Color.red;
                        }
                        button.repaint();
                        
                    } else if (button == b3) {
                        GamePanel.b3Pressed();  

                        if(!GamePanel.collisionFlag) {
                            button.color = Color.green;
                        } else {
                            button.color = Color.red;
                        }
                        button.repaint();
                     } else if (button == b4) {
                        GamePanel.b4Pressed();
                        if(!GamePanel.electricFlag) {
                            button.color = Color.green;
                        } else {
                            button.color = Color.red;
                        }
                        button.repaint();
                
                     } else {
                        GamePanel.b5Pressed();
                        if(!GamePanel.gravityFlag) {
                            button.color = Color.green;
                        } else {
                            button.color = Color.red;
                        }
                        button.repaint();
                     }
                } 
            });
        }
// (...)

}

I am unsatisfied with the above method since I have many buttons and the code for them alternating takes up easily ~100 lines of code. The symmetry of alternation suggests to me that there might exist a better approach for this design.

I have tried writing a function that takes the buttons list but the fact that we are overriding with actionPerformed confuses me a lot, and I don't know if there actually exists a way of simplifying this.

Aucun commentaire:

Enregistrer un commentaire