Java - Click JButton to change value of int to a random number between specific range -
i working on project involves creating captcha.
i have array of captcha images:
image[] captchaimagearray = {2g4qh, 4ftd2, 7bjhl, etc.}; i have array of captcha strings:
string[] captchastringarray = {"2g4qh", "4ftd2", "7bjhl", etc.}; i want generate index int would, in effect, randomly select captcha image , corresponding string. seems simple enough - created bit of code thinking on right path:
random rn = new random(); int = rn.nextint(46); so far good. created jlabel displays captcha image:
jlabel lblcaptcha = new jlabel(""); lblcaptcha.setbounds(162, 152, 300, 180); panelcaptcha.add(lblcaptcha); lblcaptcha.seticon(new imageicon(captchaimagearray[i])); finally, added jtextfield user input interpretation of captcha. user clicks jbutton, , am able compare user's input captcha string properly... once!
the problem unable change value of int i:
int = rn.nextint(46); i try change value newly generated number (i = rn.nextint(46);) within actionlistener section of submit button, error "local variable defined in enclosing scope". unable change original int new random number. was able around in shoddy manner, adding bit of code end of actionlistener:
int = rn.nextint(46); lblcaptcha.seticon(new imageicon(captchaimagearray[i])); this able change image new captcha image, index still focuses on original captcha string. basically, program able focus on first captcha string. i'm unable change value of original int i.
whats best way go solving this?
if you'd see entire section of code involving captcha, here 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 above message:" text jlabel lblinputmessage = new jlabel("please input 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 = 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 (int j = 0; j <= 46; ++j) { if (i == j) { string captcha = captchastringarray[i]; //system.out see captcha program focusing on system.out.println("i = " + i); system.out.println("captcha " + captcha); if (comparecaptcha (captcha, captchainput)) { panelemail.setvisible(true); panelcaptcha.setvisible(false); captchatextfield.settext(""); int = rn.nextint(46); lblcaptcha.seticon(new imageicon(captchaimagearray[i])); } else { joptionpane.showmessagedialog(null, "error - captcha input incorrect!"); captchatextfield.settext(""); int = 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 = rn.nextint(46); lblcaptcha.seticon(new imageicon(captchaimagearray[i])); } }); btnnewbutton.setbounds(255, 504, 130, 29); panelcaptcha.add(btnnewbutton);
you appear have god-class, class responsibility , unnecessary complexity, , that, consider refactoring code eye towards simplifying , distributing responsibility smaller grained classes. m-v-c or model-view-controller based program structure start.
as main problem, how access i within inner class, make array index instance field of class resides in, not method or constructor-local variable.
other issues: create class hold image , string single logical unit, i'd consider using imageicons , not images can swap icons in , out of jlabels. i'd avoid null layouts , setbounds(...). note imageicons can hold both image , string, string "description"` complete getters , setters. use items of class , use description string captcha string result.
Comments
Post a Comment