I'm new to Java Swing, and coding in general and a code I wrote today is having an odd glitch involving the button.doClick() method. -
if review code me, give me pointers etc, grateful! in game in case you've never played mastermind, 3 colours hidden in specific order , user has guess out of 4 possible colours. code appears work well, though unfinished, odd glitch occurs once user wins. when player guesses 3 hidden colours, prompted press "play again" button, supposed reset game, appears do, once user inputs colour jtextfields designated so, words last entered in fields revealed. have implemented small line displays word "working" on nearby jlabel show doclick() methods activating, reset button doesn't work intended, though newgame button does. odd glitch, if has fix or reason glitch or other general improvements make day!
import java.awt.color; import java.awt.componentorientation; import java.awt.dimension; import java.awt.gridbagconstraints; import java.awt.gridbaglayout; import java.awt.insets; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; import javax.swing.jtextfield; public class summative2 extends jframe { static int guessnum; static string colourguess[] = new string[3]; static string colourargument; static int[] solution = new int[3]; public summative2() { (int x=0;x<3;x++) { solution[x] = solution(); //setting solution first game } displayintro(); //activating intro screen start program } public void displayintro() { jpanel intropanel = new jpanel(new gridbaglayout()); this.getcontentpane().add(intropanel); gridbagconstraints c = new gridbagconstraints(); //the variable name being defined layout's constraints intropanel.setcomponentorientation(componentorientation.left_to_right); //setting orientation of grid layout c.insets = new insets(2,2,2,2); //basic spacing , anchoring c.anchor = gridbagconstraints.center; jlabel welcome = new jlabel("welcome mastermind"); c.ipady = 40; c.gridx = 1; //large welcome label, introducing game c.gridy = 0; c.gridwidth = 3; intropanel.add(welcome, c); c.ipady = 0; //resetting characteristics of components normal after large intro message c.gridwidth = 1; jbutton start = new jbutton ("start"); c.gridx = 1; //the start button c.gridy = 1; intropanel.add(start, c); jlabel space = new jlabel(" "); c.gridx = 2; c.ipadx = 10; //space between start , rules buttons c.gridy = 1; intropanel.add(space, c); c.ipadx= 0; jbutton rulesbutton = new jbutton("rules"); c.gridx = 3; //rules button c.gridy = 1; intropanel.add(rulesbutton, c); rulesbutton.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent arg0) { intropanel.setvisible(false); displayrules("intro"); //makes intro panel invisible , activates rules panel, along argument tells displayrules method return user intro screen when press after reading rules } }); start.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent arg0) { intropanel.setvisible(false); //makes intro invisible , starts main game. displaygame(false); } }); } public void displayrules(string previousscreen) { jpanel rulespanel = new jpanel(new gridbaglayout()); gridbagconstraints r = new gridbagconstraints(); rulespanel.setcomponentorientation(componentorientation.left_to_right); this.getcontentpane().add(rulespanel); jtextfield rulestext = new jtextfield("insert rules here"); rulestext.seteditable(false); r.gridx = 0; r.gridy = 0; r.gridwidth = 3; r.ipady=100; //big rules text box` not yet finished r.ipadx = 100; rulespanel.add(rulestext, r); r.gridwidth =1; r.ipadx=1; r.ipady=1; jbutton backfromrules = new jbutton("back"); r.gridx = 2; //back button r.gridy=1; rulespanel.add(backfromrules, r); backfromrules.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { rulespanel.setvisible(false); if (previousscreen.compareto("intro")==0) //when user presses button returned screen activated rules screen { displayintro(); } if (previousscreen.compareto("game")==0) displaygame(false); } }); } public void displaygame(boolean restart) { jpanel gamepanel = new jpanel(new gridbaglayout()); gridbagconstraints g = new gridbagconstraints(); gamepanel.setcomponentorientation(componentorientation.left_to_right); this.getcontentpane().add(gamepanel); g.anchor=gridbagconstraints.west; g.weightx =1; int coloursy = 0; jbutton redbutton = new jbutton("red"); redbutton.setbackground(color.red); //the red button, red g.gridx= 0; g.gridy=coloursy; gamepanel.add(redbutton, g); jbutton greenbutton = new jbutton("green"); greenbutton.setbackground(color.green); g.gridx = 1; //the green button, green g.gridy = coloursy; gamepanel.add(greenbutton, g); jbutton bluebutton = new jbutton("blue"); bluebutton.setbackground(color.cyan); //the blue button, cyan g.gridx =2; g.gridy=coloursy; gamepanel.add(bluebutton, g); jbutton yellowbutton = new jbutton("yellow"); yellowbutton.setbackground(color.yellow); //the yellow button yellow g.gridx=3; g.gridy=coloursy; gamepanel.add(yellowbutton, g); g.weightx=0; g.weighty=0; jlabel firstguess = new jlabel("first block guess:");//the label first guess in each guessing sequence, followed labels seconds , third g.gridx = 0; g.gridy=1; g.gridwidth = 2; gamepanel.add(firstguess, g); jlabel secondguess = new jlabel("second block guess:"); g.gridx = 0; g.gridy=2; gamepanel.add(secondguess, g); jlabel thirdguess = new jlabel("third block guess:"); g.gridx = 0; g.gridy=3; gamepanel.add(thirdguess, g); jtextfield guessone = new jtextfield(""); //the text field user can enter thier guess first colour in possible solution guessone.setpreferredsize(new dimension(50,24)); g.gridx = 2; g.gridy = 1; guessone.setcomponentorientation(componentorientation.left_to_right); gamepanel.add(guessone, g); jtextfield guesstwo = new jtextfield(""); //second colour guesstwo.setpreferredsize(new dimension(50,24)); g.gridx = 2; g.gridy = 2; gamepanel.add(guesstwo, g); jtextfield guessthree = new jtextfield(""); //third guessthree.setpreferredsize(new dimension(50,24)); g.gridx = 2; g.gridy = 3; gamepanel.add(guessthree, g); jbutton update = new jbutton();//the update button, doesn't exist used type of repeatable method whenever user presses colour button jlabel goneindicator = new jlabel("<--"); //these arrows move when user presses colour button, letting them know next colour guess applied g.gridx = 3; g.gridy = 1; gamepanel.add(goneindicator, g); jlabel gtwoindicator = new jlabel("<--"); g.gridx = 3; g.gridy = 2; gamepanel.add(gtwoindicator, g); gtwoindicator.setvisible(false); jlabel gthreeindicator = new jlabel("<--"); g.gridx = 3; g.gridy = 3; gamepanel.add(gthreeindicator, g); gthreeindicator.setvisible(false); g.gridwidth = 2; g.fill = gridbagconstraints.horizontal; jbutton submitbutton = new jbutton("submit"); //submit guess g.gridx = 0; g.gridy = 4; gamepanel.add(submitbutton, g); jbutton resetbutton = new jbutton("reset"); //reset guess jtextfields g.gridx = 2; g.gridy = 4; gamepanel.add(resetbutton, g); jbutton newgame = new jbutton("new game"); //generates new solution , presses reset button g.gridx = 0; g.gridy = 5; gamepanel.add(newgame, g); jbutton rulesbutton = new jbutton("rules"); g.gridx = 2; //displays rules g.gridy = 5; gamepanel.add(rulesbutton, g); submitbutton.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { string guess[] = new string[3]; boolean proper = true; guess[0]= guessone.gettext(); guess[1] = guesstwo.gettext(); guess[2] = guessthree.gettext(); (int y = 0;y<3;y++) { if ((guess[y].comparetoignorecase("blue")!=0) && (guess[y].comparetoignorecase("red")!=0) && (guess[y].comparetoignorecase("green")!=0) && (guess[y].comparetoignorecase("yellow")!=0)) { proper = false; //if 1 of text fields had word wasn't 1 of colours available guessing, user told so. } } if (proper) check(guess);//if in order, check guess against solution else errorwindow();//otherwise, nope } }); resetbutton.addactionlistener(new actionlistener() {//sets of textfields blank, guessnumber 1 , makes fiirst arrow appear if first guess. @override public void actionperformed(actionevent e) { guessone.settext(""); guesstwo.settext(""); guessthree.settext(""); guessnum=1; goneindicator.setvisible(true); gtwoindicator.setvisible(false); gthreeindicator.setvisible(false); } }); newgame.addactionlistener(new actionlistener () { @override public void actionperformed(actionevent e) {//clicks reset , generates new solution set. resetbutton.doclick(); for(int d=0;d<3;d++) solution[d]= solution(); } }); if (restart)//if screen generated user pressing play again button after winning, game should automatically reset , new solutions generated, bugging out somehow. { newgame.doclick(); goneindicator.settext("working"); resetbutton.doclick(); restart=false; } rulesbutton.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) {//more rules display isnt finished yet gamepanel.setvisible(false); displayrules("game"); } }); update.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) {//when colour button pressed, next guess field filled in order, depending on last 1 filled colourguess[guessnum-1] = colourargument; if (guessnum ==1 ){ guessone.settext(colourargument); goneindicator.setvisible(false); gtwoindicator.setvisible(true); guessnum++; } else if (guessnum ==2){ guesstwo.settext(colourargument); gtwoindicator.setvisible(false); gthreeindicator.setvisible(true); guessnum++; } else if (guessnum==3){ guessthree.settext(colourargument); gthreeindicator.setvisible(false); goneindicator.setvisible(true); guessnum = 1; } } }); redbutton.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent arg0) { //red put next guess slot0 colourargument = "red"; update.doclick(); } }); bluebutton.addactionlistener(new actionlistener() {//then blue @override public void actionperformed(actionevent arg0) { colourargument = "blue"; update.doclick(); } }); yellowbutton.addactionlistener(new actionlistener() {//or yellow @override public void actionperformed(actionevent arg0) { colourargument = "yellow"; update.doclick(); } }); greenbutton.addactionlistener(new actionlistener() {//or green @override public void actionperformed(actionevent arg0) { colourargument = "green"; update.doclick(); } }); } public void check(string guess[]) { jframe checkwindow = new jframe(); checkwindow.setsize(300,100); jpanel pane = new jpanel(new gridbaglayout()); checkwindow.getcontentpane().add(pane); //this set initial window generally, , activate layouts , whatnot gridbagconstraints c = new gridbagconstraints(); pane.setcomponentorientation(componentorientation.left_to_right); checkwindow.setvisible(true); c.insets = new insets(2,2,2,2); int numguess[] = new int[3]; // int colourcount=0; int positioncount=0; //converts information in textfields numbers can more compared (int x =0;x<3;x++) { if (guess[x].comparetoignorecase("red")==0) { numguess[x] = 1; } if (guess[x].comparetoignorecase("blue")==0) { numguess[x] = 2; } if (guess[x].comparetoignorecase("green")==0) { numguess[x] = 3; } if (guess[x].comparetoignorecase("yellow")==0) { numguess[x] = 4; } } (int z=0;z<3;z++) //runs through inputs compared solution, finding out how many of colours correct, , of colours in correct positions { boolean guessed = false; (int k=0;k<3;k++) { if (solution[z] == numguess[k]) { guessed = true; } } if (solution[z] == numguess[z]) positioncount++; if (guessed) colourcount++; } c.fill=gridbagconstraints.horizontal; c.anchor = gridbagconstraints.center; if (positioncount ==3) //if 3 positions correct user wins { jlabel colours = new jlabel(guess[0] + ", " + guess[1] + ", " + guess[2] + " correct!"); c.gridx=0; c.gridy=0; pane.add(colours, c); jlabel winner = new jlabel("you win!"); c.gridx=0; c.gridy=1; pane.add(winner, c); jbutton playagain = new jbutton("play again"); c.gridx=0; c.gridy=1; pane.add(playagain, c); playagain.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) {//glitch causing button for(int x=0;x<3;x++) { solution[x] = (int) (math.random()*4)+1; } checkwindow.dispose(); displaygame(true); } }); } else { jlabel labelone = new jlabel(guess[0] + ", " + guess[1] + ", " +guess[2]);//if user doesn't win, how many correct colours , positions had displayed c.gridx = 0; c.gridy = 0; pane.add(labelone, c); jlabel colourmessage = new jlabel("you had " + colourcount + " correct colours."); c.gridx=0; c.gridy=1; pane.add(colourmessage, c); jlabel positionmessage = new jlabel("you had " + positioncount + " in correct positions"); c.gridx=0; c.gridy=2; pane.add(positionmessage, c); } } public void errorwindow() { jframe checkwindow = new jframe(); checkwindow.setsize(200,100); jpanel pane = new jpanel(new gridbaglayout());//this window displayed if user inputs impossible values colour guesses checkwindow.getcontentpane().add(pane); gridbagconstraints c = new gridbagconstraints(); pane.setcomponentorientation(componentorientation.left_to_right); checkwindow.setvisible(true); jlabel whoops = new jlabel("try again valid colours."); c.gridx=0; c.gridy=0; pane.add(whoops, c); } public static void main(string[] args) { summative2 frame = new summative2(); //main method making jframe work guessnum = 1; frame.pack(); frame.setvisible(true); frame.setsize(300, 225); frame.setdefaultcloseoperation(exit_on_close); } public static int solution() { return (int) (math.random()*4+1);//solution method returning random numbers between 1 , 4 solutions. } }
every time call check()
method, you're creating new actionlistener
, registering on playagain
button.
this way, lots , lots of action listeners, , once user clicks playagain
button, called, 1 one.
you can see setting breakpoint inside actionlistener
code , debugging application.
the solution move logic out of check()
method, someplace form initialized.
Comments
Post a Comment