java - Graphics not displaying on JFrame -


i'm new graphics in java , reason graphics not displaying on jframe. confused of how set , instantiate graphics. there stupid error in code im not seeing. feedback!

map class

public class map extends jpanel{  private static int width; private static int height; private static int rows; private static int cols; private static int tile_size; private static int clear = 0; private static int blocked = 1;  private static int[][] grid;  public map(int w, int h, int t){      width = w;     height = h;     tile_size = t;     rows = height/tile_size;     cols = width/tile_size;      grid = new int[rows][cols];      (int row = 0; row < rows; row++){         (int col = 0; col < cols; col++){             grid[row][col] = blocked;         }     }      randommap(); }  public void randommap(){     int row = 0;     int col = 0;     int turn;      random rand = new random();      grid[row][col] = clear;      do{     turn = rand.nextint(2)+1;     if (turn == 1)         row++;     else         col++;     grid[row][col] = clear;     }while(row<rows-1 && col<cols-1);      if (row == rows-1){         (int = col; < cols; i++){             grid[row][i] = clear;         }     }     else{         (int = row; < rows; i++){             grid[i][col] = clear;         }     }   }   public void paintcomponent(graphics g) {      super.paintcomponent(g);     graphics2d g2d = (graphics2d) g;      (int row = 0; row < width; row++){         (int col = 0; col < height; col++){             if (grid[row][col] == 1){                 g2d.setcolor(color.black);                 g2d.fillrect(row*tile_size, col*tile_size, tile_size, tile_size);             }else{                 g2d.setcolor(color.white);                 g2d.fillrect(row*tile_size, col*tile_size, tile_size, tile_size);             }         }     } }  public void displayconsole(){      (int row = 0; row < rows; row++){         (int col = 0; col < cols; col++){              system.out.print(grid[row][col] + "   ");         }         system.out.println("");         system.out.println("");     } }  } 

game class

public class game extends jframe{  private map map;  public game(){      setlayout(null);     setbounds(0,0,500,500);     setsize(500,500);     setresizable(false);     setdefaultcloseoperation(jframe.exit_on_close);      map map = new map(500,500,50);     map.displayconsole();      add(map);     repaint();     setvisible(true); }  public static void main(string[] args) {     // todo auto-generated method stub      game game = new game();  }  } 

it painted component of size 0x0. custom painted component should return preferred size of component.

after component added frame, pack frame ensure frame exact size needed display component.

of course, either use or set appropriate layout/constraint in frame. in case, use default layout of borderlayout , default constraint of center.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -