java - Setting up a grid with JButtons -


i have fill in code project. have typed out think right first section of code, still confused not popping right graphics.

nestedlayoutpractice class

   /*  practice using layouts     anderson, franceschi */  import javax.swing.*; import java.awt.*; import java.awt.event.*;  public class nestedlayoutpractice extends jframe {  private container contents;  private game game;   private borderlayout bl;   private jlabel bottom;   // ***** task 1: declare jpanel named top  // declare 3 jbutton instance variables  // added jpanel top  // these buttons determine grid size of game:  //  3-by-3, 4-by-4, or 5-by-5 private jpanel top; private jbutton [] topbuttons; private string [] topgridsize =  {"3-by-3",  "4-by-4",  "5-by-5",       }; private jbutton reset;          // task 1 ends here   public nestedlayoutpractice( )  {   super( "practicing layout managers" );   contents = getcontentpane( );    // ***** task 2: student code starts here   // instantiate borderlayout manager bl     // set layout manager of content pane contents bl     game = new game( 3 ); // instantiating gamepanel object   // add panel center of content pane     // task 2 ends here    bottom = new jlabel( "have fun playing tile puzzle game",                         swingconstants.center );    // ***** task 3: student code restarts here   // instantiate jpanel component named top     // set layout of top 1-by-3 grid     // instantiate jbuttons determine grid size       // add buttons jpanel top        // add jpanel top content pane north component     // task 3 ends here    // ***** task 5: student code restarts here   // note: search , complete task 4 before performing task   // declare , instantiate actionlistener     // register listener on 3 buttons   // declared in task 1       // task 5 ends here    contents.add( bottom, borderlayout.south );    setsize( 325, 325 );   setvisible( true );  }   // ***** task 4: student code restarts here  // create private inner class implements actionlistener  // method should identify of 3 buttons  //    source of event  // depending on button pressed,  //    call setupgame method of game class  //    arguments 3, 4, or 5  // api of method is:  //   public void setupgame( int nsides )            // task 4 ends here   public static void main( string [] args )  {   nestedlayoutpractice nl = new nestedlayoutpractice( );   nl.setdefaultcloseoperation( jframe.exit_on_close );  } } 

tilepuzzle class

/* tilepuzzle class     anderson, franceschi  */   public class tilepuzzle  {   private int side; // grid size game 1   private string[][] tiles;   private int emptyrow;   private int emptycol;    public tilepuzzle( int newside )   {    setupgame( newside );   }    public void setupgame( int newside )   {    if ( side > 0 )      side = newside;    else      side = 3;    side = newside;    tiles = new string[side][side];    emptyrow = side - 1;    emptycol = side - 1;     ( int = 0; < side; i++ )    {     ( int j = 0; j < side; j++ )     {      tiles[i][j] = string.valueof( ( side * side )                              - ( side * + j + 1 ) );     }    }    // set empty tile blank    tiles[side - 1][side - 1] = "";   }    public int getside( )   {     return side;   } /*   public int getemptyrow( )   {     return emptyrow;   }    public int getemptycol( )   {     return emptycol;   } */   public string[][] gettiles( )   {     return tiles;   }    public boolean trytoplay( int row, int col )   {    if ( possibletoplay( row, col ) )    {     // play: switch empty string , tile label @ row, col     tiles[emptyrow][emptycol] = tiles[row][col];     tiles[row][col] = "";      emptyrow = row;     emptycol = col;     return true;    }    else     return false;   }    public boolean possibletoplay( int row, int col )   {    if ( ( col == emptycol && math.abs( row - emptyrow ) == 1 )           || ( row == emptyrow && math.abs( col - emptycol ) == 1 ) )      return true;    else      return false;   }    public boolean won( )   {    ( int = 0; < side ; i++ )    {     ( int j = 0; j < side; j++ )     {       if ( !( tiles[i][j].equals(                         string.valueof( * side + j + 1 ) ) )                         && ( != side - 1 || j != side - 1 ) )         return false;     }    }    return true;   }   } 

game class

/* using gridlayout organize our window    anderson, franceschi */  import javax.swing.*; import java.awt.*; import java.awt.event.*;  public class game extends jpanel {  private jbutton [][] squares;  private tilepuzzle game;   public game( int newside )  {   game = new tilepuzzle( newside );   setupgamegui( );  }   public void setupgame( int newside )  {   game.setupgame( newside );   setupgamegui( );  }   public void setupgamegui( )  {    removeall( ); // remove components    setlayout( new gridlayout( game.getside( ),               game.getside( ) ) );     squares = new jbutton[game.getside( )][game.getside( )];     buttonhandler bh = new buttonhandler( );     // each button: generate button label,    // instantiate button, add container,    // , register listener    ( int = 0; < game.getside( ); i++ )    {     ( int j = 0; j < game.getside( ); j++ )     {      squares[i][j] = new jbutton( game.gettiles( )[i][j] );      add( squares[i][j] );      squares[i][j].addactionlistener( bh );     }    }     setsize( 300, 300 );    setvisible( true );  }   private void update( int row, int col )  {    ( int = 0; < game.getside( ); i++ )    {     ( int j = 0; j < game.getside( ); j++ )     {      squares[i][j].settext( game.gettiles( )[i][j] );     }    }     if ( game.won( ) )    {     joptionpane.showmessagedialog( game.this,               "congratulations! won!\nsetting new game" );     // int sideofpuzzle = 3 + (int) ( 4 * math.random( ) );     // setupgamegui( );    }  }   private class buttonhandler implements actionlistener  {   public void actionperformed( actionevent ae )   {    for( int = 0; < game.getside( ); i++ )    {     for( int j = 0; j < game.getside( ); j++ )     {      if ( ae.getsource( ) == squares[i][j] )      {        if ( game.trytoplay( i, j ) )           update( i, j );        return;      } // end if      } // end inner loop    } // outer loop   } // end actionperformed method  } // end buttonhandler class } // end game class 


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -