How to check the answered questions

The first half of the code to this questions is @:
http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_21085816.html

This is the 2nd part of the code. I need to be able to check the answered question the user enters. The correct answers are in an arraylist. But I can't figure out hoe to check the answers. I need the correct answers to be painted green and the wrong answers painted red so the user can see what matakes they made. I hope someone can help me with this?

package trainer;

import java.awt.Color;

public class QPanel extends javax.swing.JPanel {

    private javax.swing.JPanel Panel1;
    private javax.swing.JPanel Panel2;
    private javax.swing.JLabel theQLabel;
    private javax.swing.JCheckBox answer1CheckBox;
    private javax.swing.JCheckBox answer2CheckBox;
    private javax.swing.JCheckBox answer3CheckBox;
    private javax.swing.JCheckBox answer4CheckBox;
    private javax.swing.JCheckBox answer5CheckBox;
    private int nrOfAnswers = 5;

    /** Creates new form QPanel */
    public QPanel() {
        initComponents();
    }
   
    public void setQuestion(String question, int nr) {
        theQLabel.setText("<HTML><B>" + nr + ". " + question + "</B></HTML>");
    }


   
    public void setAnswer(String answer, int index) {
        if (index>nrOfAnswers)
            return;
        switch(index) {
            case 1: answer1CheckBox.setText(answer); break;
            case 2: answer2CheckBox.setText(answer); break;
            case 3: answer3CheckBox.setText(answer); break;
            case 4: answer4CheckBox.setText(answer); break;
            case 5: answer5CheckBox.setText(answer); break;
            default: break;
        }
    }

    public void setNumberOfAnswers(int nr) {
        nrOfAnswers = nr;
        adaptAnswers();
    }
   
    private void adaptAnswers() {
        if (nrOfAnswers<5)
          Panel2.remove(answer5CheckBox);
        if (nrOfAnswers<4)
          Panel2.remove(answer4CheckBox);
        if (nrOfAnswers<3)
          Panel2.remove(answer3CheckBox);
    }

    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        Panel1 = new javax.swing.JPanel();
        Panel1.setBackground(new Color(250, 250, 250));
        Panel2 = new javax.swing.JPanel();
        Panel2.setBackground(new Color(250, 250, 250));
        theQLabel = new javax.swing.JLabel();
        answer1CheckBox = new javax.swing.JCheckBox();
        answer1CheckBox.setBackground(new Color(250, 250, 250));
        answer2CheckBox = new javax.swing.JCheckBox();
        answer2CheckBox.setBackground(new Color(250, 250, 250));
        answer3CheckBox = new javax.swing.JCheckBox();
        answer3CheckBox.setBackground(new Color(250, 250, 250));
        answer4CheckBox = new javax.swing.JCheckBox();
        answer4CheckBox.setBackground(new Color(250, 250, 250));
        answer5CheckBox = new javax.swing.JCheckBox();
        answer5CheckBox.setBackground(new Color(250, 250, 250));
       
        setLayout(new java.awt.BorderLayout());

        Panel1.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));
        Panel2.setLayout(new java.awt.GridBagLayout());
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        Panel2.add(theQLabel, gridBagConstraints);

        gridBagConstraints.gridy++;
        Panel2.add(answer1CheckBox, gridBagConstraints);
        gridBagConstraints.gridy++;
        Panel2.add(answer2CheckBox, gridBagConstraints);
        gridBagConstraints.gridy++;
        Panel2.add(answer3CheckBox, gridBagConstraints);
        gridBagConstraints.gridy++;
        Panel2.add(answer4CheckBox, gridBagConstraints);
        gridBagConstraints.gridy++;
        Panel2.add(answer5CheckBox, gridBagConstraints);
       
        Panel1.add(Panel2);

        add(Panel1, java.awt.BorderLayout.CENTER);
    }
}
Drop_of_RainAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
zzynxConnect With a Mentor Software engineerCommented:
2b) oh yes, QPanel needs

import javax.swing.*;


3) Here's the complete TrainingOptions.java file:

/*
 * TrainingOptions.java
 *
 */

package trainer;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.List;
import java.text.MessageFormat;
/**
 *
 * @author zzynx
 */
public class TrainingOptions extends javax.swing.JPanel {
   
    static private int MAX_NR_OF_CORRECT_ANSWERS_PER_Q = 3;
    static private int MAX_NR_OF_QUESTIONS = 15;
   
    Question myQuestions[];
    TrainerMain theMain = null;
    List theDMsToPickFrom = new ArrayList();
    List theQsToPickFrom = new ArrayList();
    boolean beginAtTheHead = true;
   
    private int currentNrOfQuestions;
   
    private List theAnswers = new ArrayList();
    private List theQPanels = new ArrayList();
   
    /** Creates new form TrainingOptions */
    public TrainingOptions(TrainerMain main) {
        currentNrOfQuestions = 0;
        theMain = main;
        initComponents();
        initQuestions();
    }
   
    private void initQuestions() {
       // 1. Create the array
       myQuestions = new Question[26];
       
       // 2. Fill it
       
       // 2.a. questions with 1 parameter
        List paraTypes = new ArrayList();
        paraTypes.add(Question.STRIKE);
        myQuestions[0] = new Question(
        "Which defensive move or moves have a <FONT color=blue>{0}</b></FONT> in them?", 1, paraTypes );

        paraTypes = new ArrayList();
        paraTypes.add(Question.STRIKE_LOCATION);
        myQuestions[1] = new Question(
        "Which defensive move or moves use a handstrike to the <FONT color=yellow>{0}</b></FONT> in them? ", 1, paraTypes);

        paraTypes = new ArrayList();
        paraTypes.add(Question.KICK);
        myQuestions[2] = new Question(
        "Which defensive move or moves use a <FONT color=blue>{0}</b></FONT> in them?", 1, paraTypes);

        paraTypes = new ArrayList();
        paraTypes.add(Question.KICK_LOCATION);
        myQuestions[3] = new Question(
        "Which defensive move or moves have a kick to the <FONT color=yellow><b>{0}</FONT> in them?", 1, paraTypes);

        paraTypes = new ArrayList();
        paraTypes.add(Question.BLOCK);
        myQuestions[4] = new Question(
        "Which defensive move or moves use a <FONT color=blue><b>{0}</b></FONT> in them?", 1, paraTypes);

        paraTypes = new ArrayList();
        paraTypes.add(Question.STANCE);
        myQuestions[5] = new Question(
        "Which defensive move or moves have a <FONT color=blue>{0}</b></FONT> in them?", 1, paraTypes);

        // 2.b. questions with 2 parameters

        paraTypes = new ArrayList();
        paraTypes.add(Question.BLOCK);
        paraTypes.add(Question.STRIKE);
        myQuestions[6] = new Question(
        "Which defensive move or moves use a <FONT color=blue>{0}</b></FONT> and a <FONT color=blue>{1}</b></FONT> in them?", 2, paraTypes);

        paraTypes = new ArrayList();
        paraTypes.add(Question.BLOCK);
        paraTypes.add(Question.KICK);
        myQuestions[7] = new Question(
        "Which defensive move or moves use a <FONT color=blue>{0}</b></FONT> and a <FONT color=blue>{1}</b></FONT> in them?", 2, paraTypes);

        paraTypes = new ArrayList();
        paraTypes.add(Question.STANCE);
        paraTypes.add(Question.STRIKE);
        myQuestions[8] = new Question(
        "Which defensive move or moves have a <FONT color=blue>{0}</b></FONT> and a <FONT color=blue>{1}</b></FONT> in them?", 2, paraTypes);

        paraTypes = new ArrayList();
        paraTypes.add(Question.STANCE);
        paraTypes.add(Question.KICK);
        myQuestions[9] = new Question(
        "Which defensive move or moves use a <FONT color=blue>{0}</b></FONT> and a <FONT color=blue>{1}</b></FONT> in them?", 2, paraTypes);

        paraTypes = new ArrayList();
        paraTypes.add(Question.STRIKE);
        paraTypes.add(Question.KICK);
        myQuestions[10] = new Question(
        "Which defensive move or moves have a <FONT color=blue>{0}</b></FONT> and a <FONT color=blue>{1}</b></FONT> in them?", 2, paraTypes);

        paraTypes = new ArrayList();
        paraTypes.add(Question.KICK);
        paraTypes.add(Question.STRIKE);
        myQuestions[11] = new Question(
        "Which defensive move or moves have a <FONT color=blue>{0}</b></FONT> and a <FONT color=blue>{1}</b></FONT> in them?", 2, paraTypes);

        paraTypes = new ArrayList();
        paraTypes.add(Question.STRIKE);
        paraTypes.add(Question.STRIKE_LOCATION);
        myQuestions[12] = new Question(
        "Which defensive move or moves use a <FONT color=blue>{0}</b></FONT> to the <FONT color=yellow><b>{1}</b></FONT> in them?", 2, paraTypes);

        paraTypes = new ArrayList();
        paraTypes.add(Question.KICK);
        paraTypes.add(Question.KICK_LOCATION);
        myQuestions[13] = new Question(
        "Which defensive move or moves use a <FONT color=blue>{0}</b></FONT> to the <FONT color=yellow><b>{1}</b></FONT> in them?", 2, paraTypes);

        // 2.c. questions with 3 parameters

        paraTypes = new ArrayList();
        paraTypes.add(Question.BLOCK);
        paraTypes.add(Question.KICK);
        paraTypes.add(Question.KICK_LOCATION);
        myQuestions[14] = new Question(
        "Which defensive move or moves use a <FONT color=blue>{0}</b></FONT> and a <FONT color=blue>{1}</b></FONT> to the <FONT color=yellow><b>{2}</b></FONT> in them?", 3, paraTypes);

        paraTypes = new ArrayList();
        paraTypes.add(Question.BLOCK);
        paraTypes.add(Question.STRIKE);
        paraTypes.add(Question.STRIKE_LOCATION);
        myQuestions[15] = new Question(
        "Which defensive move or moves use a <FONT color=blue>{0}</b></FONT> and a <FONT color=blue>{1}</b></FONT> to the <FONT color=yellow><b>{2}</b></FONT> in them?", 3, paraTypes);

        paraTypes = new ArrayList();
        paraTypes.add(Question.STANCE);
        paraTypes.add(Question.STRIKE);
        paraTypes.add(Question.STRIKE_LOCATION);
        myQuestions[16] = new Question(
        "Which defensive move or moves have a <FONT color=blue>{0}</b></FONT> and a <FONT color=blue>{1}</b></FONT> to the <FONT color=yellow><b>{2}</b></FONT> in them?", 3, paraTypes);

        paraTypes = new ArrayList();
        paraTypes.add(Question.STANCE);
        paraTypes.add(Question.KICK);
        paraTypes.add(Question.KICK_LOCATION);
        myQuestions[17] = new Question(
        "Which defensive move or moves have a <FONT color=blue>{0}</b></FONT> and a <FONT color=blue>{1}</b></FONT> to the <FONT color=yellow><b>{2}</b></FONT> in them?", 3, paraTypes);

        paraTypes = new ArrayList();
        paraTypes.add(Question.STRIKE);
        paraTypes.add(Question.KICK);
        paraTypes.add(Question.KICK_LOCATION);
        myQuestions[18] = new Question(
        "Which defensive move or moves use a <FONT color=blue>{0}</b></FONT> and a<FONT color=blue>{1}</b></FONT> to the <FONT color=yellow><b>{2}</b></FONT> in them?", 3, paraTypes);

        paraTypes = new ArrayList();
        paraTypes.add(Question.KICK);
        paraTypes.add(Question.STRIKE);
        paraTypes.add(Question.STRIKE_LOCATION);
        myQuestions[19] = new Question(
        "Which defensive move or moves use a <FONT color=blue>{0}</b></FONT> and a <FONT color=blue>{1}</b></FONT> to the <FONT color=yellow><b>{2}</b></FONT> in them?", 3, paraTypes);

        // 2.d. questions with 4 parameters

        paraTypes = new ArrayList();
        paraTypes.add(Question.STRIKE);
        paraTypes.add(Question.STRIKE_LOCATION);
        paraTypes.add(Question.KICK);
        paraTypes.add(Question.KICK_LOCATION);
        myQuestions[20] = new Question(
        "Which defensive move or moves use a <FONT color=blue><b>{0}</b></FONT> to the <FONT color=yellow><b>{1}</b></FONT> and a <FONT color=blue>{2}</b></FONT> to the <FONT color=yellow><b>{3}</b></FONT> in them?", 4, paraTypes);

        paraTypes = new ArrayList();
        paraTypes.add(Question.KICK);
        paraTypes.add(Question.KICK_LOCATION);
        paraTypes.add(Question.STRIKE);
        paraTypes.add(Question.STRIKE_LOCATION);
        myQuestions[21] = new Question(
        "Which defensive move or moves use a <FONT color=blue><b>{0}</b></FONT> to the <FONT color=yellow><b>{1}</b></FONT> and a <FONT color=blue><b>{2}</b></FONT> to the <FONT color=yellow><b>{3}</b></FONT> in them?", 4, paraTypes);

        paraTypes = new ArrayList();
        paraTypes.add(Question.STRIKE);
        paraTypes.add(Question.STRIKE);
        paraTypes.add(Question.STRIKE_LOCATION);
        paraTypes.add(Question.STRIKE_LOCATION);
        myQuestions[22] = new Question(
        "Which defensive move or moves use a <FONT color=blue><b>{0}</b></FONT> to the <FONT color=yellow><b>{2}</b></FONT> and a <FONT color=blue><b>{1}</b></FONT> to the <FONT color=yellow><b>{3}</b></FONT> in them?", 4, paraTypes);

        paraTypes = new ArrayList();
        paraTypes.add(Question.KICK);
        paraTypes.add(Question.KICK);
        paraTypes.add(Question.KICK_LOCATION);
        paraTypes.add(Question.KICK_LOCATION);
        myQuestions[23] = new Question(
        "Which defensive move or moves use a <FONT color=blue><b>{0}</b></b></FONT> to the <FONT color=yellow><b>{2}</b></b></FONT> and a <FONT color=blue><b>{1}</b></FONT> to the <FONT color=yellow><b>{3}</b></b></FONT> in them?", 4, paraTypes);

        paraTypes = new ArrayList();
        paraTypes.add(Question.STANCE);
        paraTypes.add(Question.BLOCK);
        paraTypes.add(Question.STRIKE);
        paraTypes.add(Question.STRIKE_LOCATION);
        myQuestions[24] = new Question(
        "Which defensive move or moves have a <FONT color=blue><b>{0}</b></b></FONT> and a <FONT color=blue><b>{1}</b></b></FONT> a <FONT color=blue><b>{2}</b></b></FONT> to the <FONT color=yellow><b>{3}</b></b></FONT> in them?", 4, paraTypes);

        paraTypes = new ArrayList();
        paraTypes.add(Question.STANCE);
        paraTypes.add(Question.BLOCK);
        paraTypes.add(Question.KICK);
        paraTypes.add(Question.KICK_LOCATION);
        myQuestions[25] = new Question(
        "Which defensive move or moves have a <FONT color=blue><b>{0}</b></b></FONT> and a <FONT color=blue><b>{1}</b></b></FONT> and a <FONT color=blue><b>{2}</b></b></FONT> to the <FONT color=yellow><b><b>{3}</b></b></FONT> in them?", 4, paraTypes);

    }
   
    private String composeMyText(String format, Object[] arguments) {
         return MessageFormat.format(format, arguments);
    }

    public String composeMyText(String format, Object para1) {  // use this for one parameter
         return composeMyText(format, new Object[] { para1});
    }

    public String composeMyText(String format, Object para1, Object para2) {  // use this for 2 parameters
         return composeMyText(format, new Object[] { para1, para2 });
    }

    public String composeMyText(String format, Object para1, Object para2, Object para3) { // use this for 3 parameters
         return composeMyText(format, new Object[] { para1, para2 , para3});
    }

    public String composeMyText(String format, Object para1, Object para2, Object para3, Object para4) { // use this for 4 parameters
         return composeMyText(format, new Object[] { para1, para2, para3, para4});
    }
   
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {
        mainPanel = new javax.swing.JPanel();
        theScrollPane = new javax.swing.JScrollPane();
        insideScrollPanel = new javax.swing.JPanel();
        questionsPanel = new javax.swing.JPanel();
        southPanel = new javax.swing.JPanel();
        buttonPanel = new javax.swing.JPanel();
        startButton = new javax.swing.JButton();
        resetButton = new JButton("Reset");
        checkButton = new JButton("Check");

        setLayout(new java.awt.BorderLayout());

        mainPanel.setLayout(new java.awt.BorderLayout());

        insideScrollPanel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        questionsPanel.setLayout(new java.awt.GridBagLayout());

        insideScrollPanel.add(questionsPanel);

        theScrollPane.setViewportView(insideScrollPanel);

        mainPanel.add(theScrollPane, java.awt.BorderLayout.CENTER);

        add(mainPanel, java.awt.BorderLayout.CENTER);

        startButton.setText("DM's Test");
        startButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startButtonActionPerformed(evt);
                startButton.setEnabled(false);
                checkButton.setEnabled(true);
            }
        });

        buttonPanel.add(startButton);
       
        checkButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                checkButtonActionPerformed(evt);
                checkButton.setEnabled(false);
                resetButton.setEnabled(true);
            }
        });
        buttonPanel.add(checkButton);
        checkButton.setEnabled(false);

        resetButton.addActionListener( new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                questionsPanel.removeAll();
                questionsPanel.revalidate();
                questionsPanel.repaint();
                resetButton.setEnabled(false);
                startButton.setEnabled(true);
            }
        });
        buttonPanel.add(resetButton);
        resetButton.setEnabled(false);
       
        southPanel.add(buttonPanel);

        add(southPanel, java.awt.BorderLayout.SOUTH);

    }
   
    private void startButtonActionPerformed(java.awt.event.ActionEvent evt) {
        DM theDMs[] = theMain.getDMs(); // Get the DMs array we built up in TrainerMain
        theDMsToPickFrom = new ArrayList();
        for (int i=0; i<theDMs.length; i++)
            theDMsToPickFrom.add( theDMs[i] );
        theQsToPickFrom = new ArrayList();
        for (int i=0; i<myQuestions.length; i++)
            theQsToPickFrom.add( myQuestions[i] );
        System.out.println("Number of available DMs = " + theDMsToPickFrom.size());  // <<<<<<<<<<<<<<<<

        theAnswers.clear();
        theQPanels.clear();
       
        for (int i=1; i<=MAX_NR_OF_QUESTIONS; i++)
            pickAndPresentOneQuestion();
        questionsPanel.revalidate();
        questionsPanel.repaint();
    }
   
    private DM pickAndPresentOneQuestion() {
        Random random = new Random();
        // 1. Randomly pick out a question
        Question myQ = (Question) theQsToPickFrom.get( random.nextInt(theQsToPickFrom.size()) );
        theQsToPickFrom.remove(myQ);
        if (myQ==null) return null; // should never happen
       
//        System.out.println(myQ.getFormat());
       
        int nrOfParameters = myQ.getNumberOfParameters();
       
        // 2. Randomly pick a DM
        boolean theDMisOK = false;
        DM theDM = null;
        while ( !theDMisOK ) {
           theDM = (DM) theDMsToPickFrom.get( random.nextInt(theDMsToPickFrom.size()) );
           List paraTypes = myQ.getParameterTypes();
           Iterator it = paraTypes.iterator();
           int nrOfParaTypesOK = 0;
           Integer prevType = Question.STANCE;
           while (it.hasNext()) {
               Integer type = (Integer)it.next();
               if ( type.equals(Question.STRIKE) || type.equals(Question.STRIKE_LOCATION) ) {
                   if ( prevType.equals(Question.STRIKE) || prevType.equals(Question.STRIKE_LOCATION) ) {
                       if ( theDM.getStrike(1)!=null ) // DM needs to have two strike(loc)s
                           nrOfParaTypesOK++;
                   }
                   else if ( theDM.getStrike(0)!=null )
                       nrOfParaTypesOK++;
               }
               else if ( type.equals(Question.KICK) || type.equals(Question.KICK_LOCATION) ) {
                   if ( prevType.equals(Question.KICK) || prevType.equals(Question.KICK_LOCATION) ) {
                       if ( theDM.getKick(1)!=null ) // DM needs to have two kick(loc)s
                           nrOfParaTypesOK++;
                   }
                   else if ( theDM.getKick(0)!=null )
                       nrOfParaTypesOK++;
               }
               else if ( type.equals(Question.BLOCK) ) {
                   if ( theDM.getBlock(0)!=null )
                       nrOfParaTypesOK++;
               }
               else if ( type.equals(Question.STANCE) ) {
                   if ( theDM.getStance()!=null )
                       nrOfParaTypesOK++;
               }
           }
           theDMisOK = ( nrOfParameters==nrOfParaTypesOK );
        }
        System.out.println("The picked DM is " + theDM.getName());
//        theDMsToPickFrom.remove(theDM);
       
        // 3. Randomly pick a good answer (if multiple available in the picked DM)
        // and
        // 4. Compose the question with them
        List paraTypes = myQ.getParameterTypes();
        Iterator it = paraTypes.iterator();
        int nrOfParaTypesOK = 0;
        List correctAnswers = new ArrayList();
        Integer prevType = Question.STANCE;
        List kickLocationIndex = new ArrayList();
        List strikeLocationIndex = new ArrayList();
        while (it.hasNext()) {
           Integer type = (Integer)it.next();
           if ( type.equals(Question.STRIKE) ) {
              int i = random.nextInt(theDM.getNrOfStrikes());
              correctAnswers.add( theDM.getStrike(i) );
              strikeLocationIndex.add(0, new Integer(i)); // add a the head
           }
           else if ( type.equals(Question.STRIKE_LOCATION) ) {
              if (strikeLocationIndex.size()==0) {
                  int i = random.nextInt(theDM.getNrOfStrikes());
                  correctAnswers.add( theDM.getStrikeLocation(i) );
              }
              else {
                  Integer i = (Integer)strikeLocationIndex.get(strikeLocationIndex.size()-1);
                  correctAnswers.add( theDM.getStrikeLocation(i.intValue()) );
                  strikeLocationIndex.remove(strikeLocationIndex.size()-1); // remove at the tail
              }
           }
           else if ( type.equals(Question.KICK) ) {
              int i = random.nextInt(theDM.getNrOfKicks());
              correctAnswers.add( theDM.getKick(i) );
              kickLocationIndex.add(0, new Integer(i)); // add a the head
           }
           else if ( type.equals(Question.KICK_LOCATION) ) {
              if (kickLocationIndex.size()==0) {
                  int i = random.nextInt(theDM.getNrOfKicks());
                  correctAnswers.add( theDM.getKickLocation(i) );
              }
              else {
                  Integer i = (Integer)kickLocationIndex.get(kickLocationIndex.size()-1);
                  correctAnswers.add( theDM.getKickLocation(i.intValue()) );
                  kickLocationIndex.remove(kickLocationIndex.size()-1); // remove at the tail
              }
           }
           else if ( type.equals(Question.BLOCK) ) {
              correctAnswers.add(
                     theDM.getBlock( random.nextInt(theDM.getNrOfBlocks()) ) );
           }
           else if ( type.equals(Question.STANCE) ) {
               correctAnswers.add( theDM.getStance() );
           }
        }
       
        String theQuestion = composeMyText(myQ.getFormat(), correctAnswers.toArray());
//        System.out.println(theQuestion);
       
        // Search for other good answers
        DM allDMs[] = theMain.getDMs(); // Get the DMs array we built up in TrainerMain
        Answer thePossibleAnswers[] = new Answer[5];
        thePossibleAnswers[0] = new Answer(theDM, true); // put in the first good answer
        int nrOfGoodAnswers = 1;
       
        for ( int i=0;
              i<allDMs.length && nrOfGoodAnswers < MAX_NR_OF_CORRECT_ANSWERS_PER_Q;
              i++) {
           // Make sure we don't deal with an answer we already have
           boolean skipThisOne = false;
           for (int j=0; j<nrOfGoodAnswers; j++) {
               if ( allDMs[i].getName().equals(thePossibleAnswers[j].getDM().getName()) ) {
                   skipThisOne = true;
                   break;
               }
           }
           if (skipThisOne)
               continue; // with another one
           
           // allDMs.get(i) is the DM to test.
           DM theDMToTest = allDMs[i];
           // thePossibleAnswers[0] is the good one
           it = paraTypes.iterator();
           nrOfParaTypesOK = 0;
           boolean found = false;
           while (it.hasNext()) {
               Integer type = (Integer)it.next();
               if ( type.equals(Question.STRIKE) ) {
                   found = false;
                   for (int k=0; k<theDMToTest.getNrOfStrikes(); k++)
                       if ( theDMToTest.getStrike(k).equals(
                            (String)correctAnswers.get(nrOfParaTypesOK)) ) {
                           nrOfParaTypesOK++;
                           found = true;
                           break;
                       }
                   if (!found)
                       break;
               }
               if ( type.equals(Question.STRIKE_LOCATION) ) {
                   found = false;
                   for (int k=0; k<theDMToTest.getNrOfStrikes(); k++)
                       if ( theDMToTest.getStrikeLocation(k).equals(
                            (String)correctAnswers.get(nrOfParaTypesOK)) ) {
                           nrOfParaTypesOK++;
                           found = true;
                           break;
                       }
                   if (!found)
                       break;
               }
               if ( type.equals(Question.KICK) ) {
                   found = false;
                   for (int k=0; k<theDMToTest.getNrOfKicks(); k++)
                       if ( theDMToTest.getKick(k).equals(
                            (String)correctAnswers.get(nrOfParaTypesOK)) ) {
                           nrOfParaTypesOK++;
                           found = true;
                           break;
                       }
                   if (!found)
                       break;
               }
               if ( type.equals(Question.KICK_LOCATION) ) {
                   found = false;
                   for (int k=0; k<theDMToTest.getNrOfKicks(); k++)
                       if ( theDMToTest.getKickLocation(k).equals(
                            (String)correctAnswers.get(nrOfParaTypesOK)) ) {
                           nrOfParaTypesOK++;
                           found = true;
                           break;
                       }
                   if (!found)
                       break;
               }
               else if ( type.equals(Question.BLOCK) ) {
                   found = false;
                   for (int k=0; k<theDMToTest.getNrOfBlocks(); k++)
                       if ( theDMToTest.getBlock(k).equals(
                            (String)correctAnswers.get(nrOfParaTypesOK)) ) {
                           nrOfParaTypesOK++;
                           found = true;
                           break;
                       }
                   if (!found)
                       break;
               }
               else if ( type.equals(Question.STANCE) ) {
                   if ( theDMToTest.getStance()!=null &&
                        theDMToTest.getStance().equals(
                          (String)correctAnswers.get(nrOfParaTypesOK)) ) {
                       nrOfParaTypesOK++;
                   }
                   else
                      break;
               }
           }
           if ( nrOfParameters==nrOfParaTypesOK ) {
               // add the found DM to the possible answers:
               thePossibleAnswers[nrOfGoodAnswers] = new Answer(theDMToTest, true);
               nrOfGoodAnswers++;
           }
        } // for-loop over theDMs array
       
        // Now we have nrOfGoodAnswers good answers.
        // Let's fill up the rest of the array with wrong answers
        int nrOfAnswers = nrOfGoodAnswers;
        while (nrOfAnswers!=5) {
            DM anotherDM = allDMs[ random.nextInt(allDMs.length) ];
           
            // Make sure we don't deal with an answer we already have
            boolean skipThisOne = false;
            for (int j=0; j<nrOfAnswers; j++) {
               if ( anotherDM.getName().equals(thePossibleAnswers[j].getDM().getName()) ) {
                   skipThisOne = true;
                   break;
               }
            }
            if (skipThisOne)
               continue; // with another one
            else {
               // add the found DM to the possible answers:
               thePossibleAnswers[nrOfAnswers] = new Answer(anotherDM, false);
               nrOfAnswers++;
            }
        }
       
        // shuffle the answers
        List lstAnswers = Arrays.asList(thePossibleAnswers);
        Collections.shuffle(lstAnswers);
       
        // Present the question in an added panel
        QPanel qPanel = new QPanel();
        qPanel.setQuestion(theQuestion, currentNrOfQuestions+1);
        for (int i=0; i<5; i++)
            qPanel.setAnswer(thePossibleAnswers[i].getDM().getName(), i+1);
       
        GridBagConstraints gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = currentNrOfQuestions;
        currentNrOfQuestions++;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        questionsPanel.add(qPanel, gridBagConstraints);
       
        theAnswers.add( thePossibleAnswers );
        theQPanels.add( qPanel );
       
        return theDM;
    }
   
    private void checkButtonActionPerformed(java.awt.event.ActionEvent evt) {
        for (int i=0; i<theAnswers.size(); i++) {
            Answer thePossibleAnswers[] = (Answer []) theAnswers.get(i);
            QPanel panel = (QPanel) theQPanels.get(i);
           
            for (int j=0; j<thePossibleAnswers.length; j++)
                panel.check( thePossibleAnswers[j].isCorrect(), j+1 );
        }
        questionsPanel.revalidate();
        questionsPanel.repaint();
    }
   
    private javax.swing.JPanel buttonPanel;
    private javax.swing.JPanel insideScrollPanel;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JPanel questionsPanel;
    private javax.swing.JPanel southPanel;
    private javax.swing.JButton startButton;
    private javax.swing.JScrollPane theScrollPane;
    private javax.swing.JButton checkButton;
    private javax.swing.JButton resetButton;
}

Oh, I forced the user to press the buttons in the right order. Have a look.

Can you let me know if it is OK?
If it is I can go to sleep. It's midnight over here.

btw. No freeze problems detected over here.
0
 
zzynxSoftware engineerCommented:
The easiest part:

1) at the bottom of the source code add:

private javax.swing.JButton checkButton;

2) where the buttons are created (e.g. the startButton) add:

        checkButton.setText("Check");
        checkButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                checkButtonActionPerformed(evt);
            }
        });
        buttonPanel.add(checkButton);

3) add a function:

    private void checkButtonActionPerformed(java.awt.event.ActionEvent evt) {

    }

The second part is filling in that function.
Busy...
0
 
zzynxSoftware engineerCommented:
I forgot

        checkButton = new JButton("Check");


The checking will require quite some changes. Since we shuffle the possible answers (correct and wrong) we don't know what are the good anymore.
So, I guess that we will need to make some little class that can "remember" if it is good or wrong answer...
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Drop_of_RainAuthor Commented:
I had already got the button in the panel.
0
 
Drop_of_RainAuthor Commented:
So they will be put like into a map and the ket will be correct or wrong?
0
 
zzynxSoftware engineerCommented:
No, no.
I'm in the design phase, now.
Give me some time...
0
 
zzynxConnect With a Mentor Software engineerCommented:
You can already create this new class in trainer:

/*
 * Answer.java
 *
 * Created on 7 augustus 2004, 22:49
 */

package trainer;

/**
 *
 * @author  zzynx
 */
public class Answer {
    private DM theDM;
    private boolean correct;
       
    public Answer(DM dm, boolean correct) {
        theDM = dm;
        this.correct = correct;
    }
       
    public boolean isCorrect() { return correct; }
    public DM getDM() { return theDM; }
}
0
 
zzynxSoftware engineerCommented:
>> I need the correct answers to be painted green and the wrong answers painted red so the user can see what matakes they made.
Does that mean the following, if the correct answers are the 1st and the last

1. DM x     the user checked this check box  ==> Green

2. DM y     the user checked this check box ==> Red

3. DM z     the user didn't check this one  ==> Green

4. DM q    

5. DM r    the user didn't check this check box  ==> RED


1 & 5 are quite easy.
But what about 2 and 3? Are the colors like you want them to be?

0
 
zzynxSoftware engineerCommented:
So, does in fact each answer will get a color: red or green?
0
 
Drop_of_RainAuthor Commented:
Only the answers related to the users answers:

The user checks the 1st and 3rd checkbox
The correct answers are the 1st and 4th
The 1st would get a green and the 3rd would get a red
0
 
zzynxSoftware engineerCommented:
I think if *all* items of an answer are colored green, the question is answered correctly.
If some answers color red that mean that they are not selected if they had to, or that the user has selected them where they shouldn't.

For the moment I got it working like this. It looks nice that way.

Think about it, let me know
0
 
Drop_of_RainAuthor Commented:
Yes that is fine
0
 
zzynxSoftware engineerCommented:
>> It looks nice that way
Moreover, that also means that if you don't check any check box and press <Check>,
all answers that had to be checked, turn into red ones,
the others into green (because they shouldn't indeed be checked)

Well, the solution follows...

1) Make the Answer class (if not yet done)
0
 
zzynxConnect With a Mentor Software engineerCommented:
2) extend your class QPanel with the following function:

    public void check(boolean correct, int number) {
        StringBuffer newText = new StringBuffer();
        newText.append("<HTML><FONT COLOR=");
        JCheckBox checkBox = null;
        switch(number) {
            case 1 : checkBox = answer1CheckBox; break;
            case 2 : checkBox = answer2CheckBox; break;
            case 3 : checkBox = answer3CheckBox; break;
            case 4 : checkBox = answer4CheckBox; break;
            case 5 : checkBox = answer5CheckBox; break;
        }
        if ( (correct && checkBox.isSelected()) ||
             (!correct && !checkBox.isSelected()) )
           newText.append( "\"green\">" );
        else
            newText.append( "\"red\">" );
        switch(number) {
            case 1 : newText.append(answer1CheckBox.getText()); break;
            case 2 : newText.append(answer2CheckBox.getText()); break;
            case 3 : newText.append(answer3CheckBox.getText()); break;
            case 4 : newText.append(answer4CheckBox.getText()); break;
            case 5 : newText.append(answer5CheckBox.getText()); break;
            default : break;
        }
        newText.append("</HTML>");
        setAnswer(newText.toString(), number);
    }

0
 
Drop_of_RainAuthor Commented:
So you mean that if they just pressed the check answers button they will know all the correct answers?
0
 
zzynxSoftware engineerCommented:
>> So you mean that if they just pressed the check answers button they will know all the correct answers?
They can deduce them, yes. That's "checking the answers" isn't it?

Oh, I see, you would color only the answers that the user checked?
Can easily be done...
0
 
zzynxSoftware engineerCommented:
But in this scenario they can also deduce the correct answers you know.
It's just some more work:

Check all check boxes and press <Check>.
The ones that color green are all the correct ones.

You see, being able to deduce the correct answers is inherent to "checking your answers against the correct answers"...
0
 
zzynxSoftware engineerCommented:
If you don't want them to deduce all the correct answers,
you should indicate ***per question*** (not per answer) if it is answered correct.  
By e.g. letting appear an extra label saying "Correct" or "Wrong"

But then "correct" means that the user has checked *all* possible correct answers (without forgetting one)


How does the posted code look?
0
 
Drop_of_RainAuthor Commented:
It looks fine.
0
 
Drop_of_RainAuthor Commented:
I see what you mean, that is a good idea.
0
 
Drop_of_RainAuthor Commented:
How would we make it a lighter green?
0
 
zzynxSoftware engineerCommented:
>> It looks fine
Glad to hear :°)

>>I see what you mean, that is a good idea.
You want that to be implemented?
0
 
Drop_of_RainAuthor Commented:
Yes lets try it. How about the color to lighter green I tried this
newText.append( "\"124, 252, 0\">" );
0
 
Drop_of_RainAuthor Commented:
we won't need it with the new label, I just wanted to know how
0
 
zzynxSoftware engineerCommented:
>> How would we make it a lighter green?

Oh, that's a matter of changing the HTML code for green:

in the new QPanel function check

replace

           newText.append( "\"green\">" );
by
           newText.append( "\"#33FF33\">" );

or whatever Red/Green/Blue combination you want.
(Pick you color at e.g. http://www.pagetutor.com/pagetutor/makapage/picker/)
0
 
zzynxSoftware engineerCommented:
>> we won't need it with the new label,
OK
>> I just wanted to know how
I thought about adding a JLabel in the QPanel next to the question itself (but right aligned)
0
 
Drop_of_RainAuthor Commented:
Oh you use Hex.
0
 
Drop_of_RainAuthor Commented:
The only way sometimes is to see it.
0
 
zzynxSoftware engineerCommented:
>> Oh you use Hex.
Yes, that's how it is done in HTML: or you use the names (green, red, silver,...) or you use the hex codes
0
 
zzynxSoftware engineerCommented:
>> The only way sometimes is to see it.
Yeah, learning by example can be great
0
 
zzynxSoftware engineerCommented:
OK, think I'm going to bed now.
Is that OK for you ;°)
I mean do you have enough info/code to consider this one as closed?
0
 
Drop_of_RainAuthor Commented:
Yes, Go to Bed.
0
 
zzynxSoftware engineerCommented:
All right off we go :°)
See you
0
 
Drop_of_RainAuthor Commented:
GREAT WORK
0
 
zzynxSoftware engineerCommented:
Thanks for accepting
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.