java - Positioning graphics or jpanel in the center of the window on resize -


in program use cardlayout switch between panels ( each panel used separate class ). 1 of panels, lets call board, contains graphics.

as understand size of panel fit window size or jframe, when resize window jpanel size , graphics in change in size.

how can position graphics in center of window when resizes? or understand how can position jpanel in center graphics contains located in center of window also?

my graphics use in program simple square grid ( fixed in number of cells ), here's code, cols , rows constants while size ( size of cell ) adjusted size of window , can changed:

       g.setcolor(color.light_gray);         for( int = 1; < cols; ++ ){             for( int j = 1; j < rows; j ++ ){                 g.drawline(0, j*size, cols*size, j*size);                 g.drawline(i*size, 0, i*size, rows*size);             }         } 

possible solutions:

  • get jpanel's width , height within paintcomponent method , use information scale graphics.
  • nest drawing jpanel inside of jpanel, "wrapper" jpanel uses gridbaglayout. if on drawing jpanel added , without gridbagconstraints, remain centralized in wrapper jpanel.

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -