Search code examples
javajbuttonjavax

Java code restructuring


as I progress with my little program I have 4 button who are rules by the same rules and I don't know how to reduce the code, I'm almost certain it can be simplify:

private void initGame(Quizz quizz) {

    jLabelScore = new javax.swing.JLabel();
    jLabelComptQ = new javax.swing.JLabel();
    jButtonA = new javax.swing.JButton();
    jButtonB = new javax.swing.JButton();
    jButtonC = new javax.swing.JButton();
    jButtonD = new javax.swing.JButton();

    int i = quizz.aleatQuestion();
    ArrayList<Reponse> listeRep = quizz.showAnswer(i);
    Reponse reponseA = listeRep.get(0);
    Reponse reponseB = listeRep.get(1);
    Reponse reponseC = listeRep.get(2);
    Reponse reponseD = listeRep.get(3);

    jButtonA.setText(reponseA.getReponse());
    jButtonB.setText(reponseB.getReponse());
    jButtonC.setText(reponseC.getReponse());
    jButtonD.setText(reponseD.getReponse());
    jLabelScore.setText("Score : " + quizz.getScore());
    int a = quizz.getCompteurQ()+1;
    jLabelComptQ.setText("Question n°" + a);

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setTitle("Jeu en Cours - StudenTest");
    setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
    setMaximumSize(new java.awt.Dimension(800, 600));
    setMinimumSize(new java.awt.Dimension(800, 600));
    setResizable(false);
    setSize(new java.awt.Dimension(800, 600));
    getContentPane().setLayout(null);
    getContentPane().removeAll();

    getContentPane().add(jLabelScore);
    jLabelScore.setBounds(0, 20, 250, 250);
    jLabelScore.setVerticalAlignment(SwingConstants.TOP);

    getContentPane().add(jLabelComptQ);
    jLabelComptQ.setBounds(0, 0, 250, 250);
    jLabelComptQ.setVerticalAlignment(SwingConstants.TOP);

    jButtonA.addMouseListener(new java.awt.event.MouseAdapter() {
        @Override
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            if (reponseA.getVerif()) {
                quizz.win();
            } else {
                quizz.lose();
            }
            quizz.endOfTurn();
            if (quizz.getCompteurQ() >= 5) {
                Component frame = null;
                JOptionPane.showMessageDialog(frame, "Score Final :" + quizz.getScore() + "\nBien Joué");
                initComponents();
            } else {
                initGame(quizz);
            }
        }

    });
    getContentPane().add(jButtonA);
    jButtonA.setBounds(425, 325, 150, 75);

    jButtonB.addMouseListener(new java.awt.event.MouseAdapter() {
        @Override
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            if (reponseB.getVerif()) {
                quizz.win();
            } else {
                quizz.lose();
            }
            quizz.endOfTurn();
            if (quizz.getCompteurQ() >= 5) {
                Component frame = null;
                JOptionPane.showMessageDialog(frame, "Score Final :" + quizz.getScore() + "\nBien Joué");
                initComponents();
            } else {
                initGame(quizz);
            }
        }
    });
    getContentPane().add(jButtonB);
    jButtonB.setBounds(225, 325, 150, 75);

    jButtonC.addMouseListener(new java.awt.event.MouseAdapter() {
        @Override
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            if (reponseC.getVerif()) {
                quizz.win();
            } else {
                quizz.lose();
            }
            quizz.endOfTurn();
            if (quizz.getCompteurQ() >= 5) {
                Component frame = null;
                JOptionPane.showMessageDialog(frame, "Score Final :" + quizz.getScore() + "\nBien Joué");
                initComponents();
            } else {
                initGame(quizz);
            }
        }
    });
    getContentPane().add(jButtonC);
    jButtonC.setBounds(425, 450, 150, 75);

    jButtonD.addMouseListener(new java.awt.event.MouseAdapter() {
        @Override
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            if (reponseD.getVerif()) {
                quizz.win();
            } else {
                quizz.lose();
            }
            quizz.endOfTurn();
            if (quizz.getCompteurQ() >= 5) {
                Component frame = null;
                JOptionPane.showMessageDialog(frame, "Score Final :" + quizz.getScore() + "\nBien Joué");
                initComponents();
            } else {
                initGame(quizz);
            }
        }
    });
    getContentPane().add(jButtonD);
    jButtonD.setBounds(225, 450, 150, 75);

    pack();

    setLocationRelativeTo(null);
}

You can see that reponseA/B/C/D and jButtonA/B/C/D follow the same instruction. I reviewed the lesson I had but I didn't find any help for this problem even if it helped me a lot. I have tried to make online research but it wasn't related or the solution didn't fit... I hope you have understand what I said (I'm a not fluent in english).

Ps: I have let all the code of the method but if needed i can remove the part who aren't fully necessary.


Solution

  • What you are looking for is actually called as code refactoring, so you need to search for how to refactor the code?

    You need to refactor your code and write the single responsibility classes and smaller methods so that it can be better understandable/readable/supportable.

    To start with, you can implement all your MouseListener into separate classes as below:

    public class ButtonAMouseListener implements java.awt.event.MouseAdapter {
         //implement ButtonA MouseListener code here
    }
    
    public class ButtonBMouseListener implements java.awt.event.MouseAdapter {
         //implement ButtonB MouseListener code here
    }
    
    public class ButtonCMouseListener implements java.awt.event.MouseAdapter {
         //implement ButtonC MouseListener code here
    }
    
    public class ButtonDMouseListener implements java.awt.event.MouseAdapter {
         //implement ButtonD MouseListener code here
    }
    

    Now, create the objects for these classes and use them in the original class.