vendredi 7 avril 2017

Java - Click JButton to change value of int to a random number between specific range

I am working on a project that involves creating a captcha.

I have an array of captcha images:

Image[] CAPTCHAimageArray = {2G4QH, 4FTD2, 7BJHL, etc.};

I also have an array of captcha strings:

String[] CAPTCHAstringArray = {"2G4QH", "4FTD2", "7BJHL", etc.};

I want to generate an index int that would, in effect, randomly select a captcha image and its corresponding string. Seems simple enough - I created this bit of code thinking I was on the right path:

Random rn = new Random();
int i = rn.nextInt(46);

So far so good. I created a JLabel that displays the captcha image:

JLabel lblCAPTCHA = new JLabel("");
lblCAPTCHA.setBounds(162, 152, 300, 180);
panelCAPTCHA.add(lblCAPTCHA);
lblCAPTCHA.setIcon(new ImageIcon(CAPTCHAimageArray[i]));

Finally, I added a JTextfield where the user would input their interpretation of the captcha. The user clicks a JButton, and I am able to compare the user's input with the captcha string properly... but only once!

The problem is that I am unable to change the value of this int i:

int i = rn.nextInt(46);

I try to change its value to a newly generated number (i = rn.nextInt(46);) within the ActionListener section of the submit button, but I get the error "local variable defined in an enclosing scope". I am unable to change the original int i to a new random number. I was able to get around this in a very shoddy manner, which is by adding this bit of code to the end of the ActionListener:

int i = rn.nextInt(46);
lblCAPTCHA.setIcon(new ImageIcon(CAPTCHAimageArray[i]));

This is only able to change the image to a new captcha image, but the index still focuses on the original captcha string. Basically, the program is only able to focus on the first captcha string. I'm unable to change the value of the original int i.

Whats the best way to go about solving this?

If you'd like to see the entire section of code involving the captcha, here it is:

    /* CAPTCHA MENU CONTENTS */

        //"Account Creation" Text
            JLabel lblAccountCreation_3 = new JLabel("Account Creation");
            lblAccountCreation_3.setBounds(263, 89, 109, 16);
            panelCAPTCHA.add(lblAccountCreation_3);

        //"Please input the above message:" Text
            JLabel lblInputMessage = new JLabel("Please input the above message:");
            lblInputMessage.setBounds(207, 383, 209, 16);
            panelCAPTCHA.add(lblInputMessage);

        //Initiate CAPSLOCK Filter
            DocumentFilter filter = new UppercaseDocumentFilter();

        //CAPTCHA TextField
            CAPTCHAtextField = new JTextField();
            CAPTCHAtextField.setBounds(258, 411, 130, 26);
            panelCAPTCHA.add(CAPTCHAtextField);
            ((AbstractDocument) CAPTCHAtextField.getDocument()).setDocumentFilter(filter);

        //CAPTCHA Image Array
            Image[] CAPTCHAimageArray = {c2G4QH, c4FTD2, c7BJHL, c7JDFV, c9PB43, c9TVB4, cADVE8, cAZQRV,
                    cBLTFT, cBYF4D, cD8URH, cDBVFX, cDQAXC, cECD6A, cERTYA, cGTJRD, cGY67E, cHDP7R,
                    cJU4RV, cK8CRW, cKJPHL, cKMFDM, cL9MBP, cLGU3W, cLKMDR, cLMRTD, cLMUFX, cLPDT2,
                    cLPTY2, cLXF49, cMKNLH, cMY62A, cPT7W2, cRDAVH, cRTLPQ, cRVBAZ, cT7TMW, cUL4B7,
                    cUW2CZ, cVBCHY, cVF4TU, cW36X9, cWX2DT, cYT782, cYWRQZ, cZKGF8};

        //CAPTCHA String Array
            String[] CAPTCHAstringArray = {"2G4QH", "4FTD2", "7BJHL", "7JDFV", "9PB43", "9TVB4", "ADVE8", "AZQRV",
                    "BLTFT", "BYF4D", "D8URH", "DBVFX", "DQAXC", "ECD6A", "ERTYA", "GTJRD", "GY67E", "HDP7R",
                    "JU4RV", "K8CRW", "KJPHL", "KMFDM", "L9MBP", "LGU3W", "LKMDR", "LMRTD", "LMUFX", "LPDT2",
                    "LPTY2", "LXF49", "MKNLH", "MY62A", "PT7W2", "RDAVH", "RTLPQ", "RVBAZ", "T7TMW", "UL4B7",
                    "UW2CZ", "VBCHY", "VF4TU", "W36X9", "WX2DT", "YT782", "YWRQZ", "ZKGF8"};

        //CAPTCHA Image Generation
            Random rn = new Random();
            int i = rn.nextInt(46);
            JLabel lblCAPTCHA = new JLabel("");
            lblCAPTCHA.setBounds(162, 152, 300, 180);
            panelCAPTCHA.add(lblCAPTCHA);
            lblCAPTCHA.setIcon(new ImageIcon(CAPTCHAimageArray[i]));

        //"Submit" Button
            JButton btnSubmit_4 = new JButton("Submit");
            btnSubmit_4.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    String CAPTCHAinput = CAPTCHAtextField.getText();
                    //Check if input matches CAPTCHA
                    for (int j = 0; j <= 46; ++j) {
                        if (i == j) {
                            String CAPTCHA = CAPTCHAstringArray[i];
                        //System.out is to see which captcha the program is focusing on
                            System.out.println("i = " + i);
                            System.out.println("CAPTCHA is " + CAPTCHA);
                            if (compareCAPTCHA (CAPTCHA, CAPTCHAinput)) {
                                panelEmail.setVisible(true);
                                panelCAPTCHA.setVisible(false);
                                CAPTCHAtextField.setText("");
                                int i = rn.nextInt(46);
                                lblCAPTCHA.setIcon(new ImageIcon(CAPTCHAimageArray[i]));
                            }
                            else {
                                JOptionPane.showMessageDialog(null, "Error - CAPTCHA input incorrect!");
                                CAPTCHAtextField.setText("");
                                int i = rn.nextInt(46);
                                lblCAPTCHA.setIcon(new ImageIcon(CAPTCHAimageArray[i]));
                            }
                        }
                    }
                }
            });
            btnSubmit_4.setBounds(260, 463, 117, 29);
            panelCAPTCHA.add(btnSubmit_4);

        //"New CAPTCHA" BUTTON
            JButton btnNewButton = new JButton("New CAPTCHA");
            btnNewButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    int i = rn.nextInt(46);
                    lblCAPTCHA.setIcon(new ImageIcon(CAPTCHAimageArray[i]));
                }
            });
            btnNewButton.setBounds(255, 504, 130, 29);
            panelCAPTCHA.add(btnNewButton);




Aucun commentaire:

Enregistrer un commentaire