java - saving a button for later use -
this question has answer here:
- what nullpointerexception, , how fix it? 12 answers
i want save button , change it's icon next time clicks on button, gives me nullpointer exception on line button2.seticon(icon); . how can save button , change it's icon next time? or how can access button array in constructor? possible?
package tictactoe; // import necessary classes , interfaces import java.awt.borderlayout; import java.awt.dimension; import java.awt.font; import java.awt.gridlayout; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.util.linkedhashset; import java.util.random; import java.util.set; import javax.swing.icon; import javax.swing.imageicon; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.joptionpane; import javax.swing.jpanel; public class tictactoegame extends jframe { /** * */ boolean lock=false; //int for_the_first_time=1; int temp1; int temp2; int selected=0; icon ic; int row ; int col ; icon[] array = new icon[12]; icon[] arrayc = new icon[12]; int[] random_array = new int[12]; int[] arrayint = new int[12]; int[] arrayintc = new int[12]; private static final long serialversionuid = 1l; private final static int rows = 3; private final static int columns = 4; private jbutton button[]; private jpanel buttonspanel; private jbutton exitbutton; icon icon = new imageicon(getclass().getresource("scene.jpg")); public tictactoegame() { // set frame's title this.settitle( "remember me" ); buttonspanel = new jpanel(); // set panel's layout manager buttonspanel.setlayout( new gridlayout( rows , columns , 1 , 1 ) ); // create jbutton object named exitbutton exitbutton = new jbutton("exit"); //register actionlistener exitbutton exitbutton.addactionlistener( new actionlistener() { public void actionperformed( actionevent ae ) { // normal exit system.exit( 0 ); } } ); // add exitbutton frame this.add( exitbutton , borderlayout.south ); // create array of jbuttons button = new jbutton[ rows * columns ]; // initialize each button , add buttonspanel ( int = 0 ; < 12 ; i++ ) { array[i]= new imageicon(getclass().getresource(i+".jpg")); arrayint[i]=i; } //create not repeating random numbers: random rng = new random(); // ideally create 1 instance globally // note: use linkedhashset maintain insertion order set<integer> generated = new linkedhashset<integer>(); while (generated.size() < 12) { integer next = rng.nextint(12); // we're adding set, automatically containment check generated.add(next); } string str = generated.tostring(); str = str.replaceall("\\s", ""); str = str.replace("[", ""); str = str.replace("]", ""); string[] str2= str.split(","); (int i=0;i<12;i++){ random_array[i]= integer.parseint(str2[i]); } (int i=0;i<12;i++){ arrayc[i]=array[random_array[i]]; arrayintc[i]=arrayint[random_array[i]]; } ( int = 0 ; < rows * columns ; i++ ) { button[ ] = new jbutton(); button[ ].setfocuspainted( false ); button[ ].setactioncommand( integer.tostring( ) ); button[ ].setfont( new font( "tahoma" , font.bold , 15 ) ); button[ ].setpreferredsize( new dimension( 100 , 100 ) ); button[ ].settooltiptext( "click make move" ); button[ ].addactionlistener( new buttonclickhandler() ); button[ ].seticon(arrayc[i]); buttonspanel.add( button[ ] ); } // add buttonspanel frame this.add(buttonspanel, borderlayout.north); // set of jframe's propeties this.setdefaultcloseoperation( jframe.exit_on_close ); // don't allow user resize jframe this.setresizable( false ); // resize frame suitable size this.pack(); // don't allow user resize game window this.setvisible( true ); try{ thread.sleep(2000); ( int = 0 ; < rows * columns ; i++ ) { button[ ].seticon(icon); } }catch(interruptedexception e){ } // resetgame(); } // end of class constructor private class buttonclickhandler implements actionlistener { public void actionperformed( actionevent event ) { int count=0; jbutton button = (jbutton) event.getsource(); set_rows_columns(button); check_if_possible_to_show(count, button); } jbutton button2; int for_the_first_time=1; boolean stable=false; private void check_if_possible_to_show(int count, jbutton button) { if (selected==1){ button2.seticon(icon); is_going_to_be_stable(temp1,arrayintc[count],count,button); } else{ temp1=arrayintc[count]; button2=button; selected=1; button.seticon(arrayc[count]); } } private void is_going_to_be_stable(int temp1, int temp2,int count, jbutton button) { int temp1v; int temp2v; boolean bool=false; if (temp1==0){ if (temp2==6){ bool= true; } } if (temp1==1){ if (temp2==7){ bool= true; } } if (temp1==2){ if (temp2==8){ bool= true; } } if (temp1==3){ if (temp2==9){ bool= true; } } if (temp1==4){ if (temp2==10){ bool= true; } } if (temp1==5){ if (temp2==11){ bool= true; } } if (bool==true){ button.seticon(arrayc[count]); button2.seticon(arrayc[1]); } else{ button2.seticon(icon); button.seticon(arrayc[count]); } } public void set_rows_columns(jbutton button) { if(button.getactioncommand().equals("0")){ row=0; col=0; } if(button.getactioncommand().equals("1")){ row=0; col=1; } if (button.getactioncommand().equals("2")){ row=0; col=2; } if (button.getactioncommand().equals("3")){ row=0; col=3; } if (button.getactioncommand().equals("4")){ row=1; col=0; } if (button.getactioncommand().equals("5")){ row=1; col=1; } if (button.getactioncommand().equals("6")){ row=1; col=2; } if (button.getactioncommand().equals("7")){ row=1; col=3; } if (button.getactioncommand().equals("8")){ row=2; col=0; } if (button.getactioncommand().equals("9")){ row=2; col=1; } if (button.getactioncommand().equals("10")){ row=2; col=2; } if (button.getactioncommand().equals("11")){ row=2; col=3; } } } } // end of class tictactoegame
first thing say: can short code. instead of is_going_to_be_stable method can use this:
if ( (temp1 == 0 && temp2 == 6) || (temp1 == 1 && temp2 == 7) || (temp1 == 2 && temp2 == 8) || (temp1 == 3 && temp2 == 9) || (temp1 == 4 && temp2 == 10) || (temp1 == 5 && temp2 == 11) ){ button.seticon(arrayc[count]); button2.seticon(arrayc[1]); } else{ button2.seticon(icon); button.seticon(arrayc[count]); }
and instead of set_rows_columns method can use this
switch(button.getactioncommand()){ case "0": row=0; col=0; break; //and on }
and shouldn't say
private jbutton button[];
you should say
private jbutton[] button;
i don't know if problem fixed or not.
Comments
Post a Comment