Java AWT/Swing : paintComponent issue with custom JPanel(s) -


ok, basically, have far:

  • a main class creates custom jframe (applicationwindow).
  • an applicationwindow class extends jframe , acts window.
  • a mapdisplaypanel class extends jpanel , meant (using gridlayout) display 8x8 grid of:
  • a mapblock class extends jpanel.
  • mapblocks contained in class contain game data, gamedata.java

it seems work, except 1 mapblock painted screen.

code:

main.java

public class main {  public static void main(string[] args) {     final applicationwindow window = new applicationwindow();      window.setvisible(true); } } 

applicationwindow.java

public class applicationwindow extends jframe {  public applicationwindow() {     settitle("heroes!");     setlocationrelativeto(null);     setsize(800,600); //  setlayout(new borderlayout());      jpanel map = new mapdisplaypanel();      add(map);//, borderlayout.center); } } 

mapdisplaypanel.java

public class mapdisplaypanel extends jpanel{ gamedata game = null;  public mapdisplaypanel() {     game = new gamedata();     setlayout(new gridlayout(game.getwidth(),game.getheight()));     setbackground(color.cyan);      mapblock[][] map = game.getmap();     for(mapblock[] ablk : map)     {         for(mapblock blk : ablk)         {             if(blk != null){add(blk);}         }     } } } 

mapblock.java

public class mapblock extends jpanel{ private int xpos = -1, ypos = -1;  //constructors public mapblock(int x, int y, int terrain) {     this.xpos = x;     this.ypos = y;     this.terrain = terrain;     setpreferredsize(new dimension(50,50)); }  //methods @override public void paintcomponent(graphics g) {     //setbackground(color.green);     super.paintcomponent(g);     g.setcolor(color.green);     g.fillrect(0, 0, this.getwidth(), this.getheight());     g.setcolor(color.magenta);     g.fillrect(10, 10, this.getwidth() - 20, this.getheight() - 20);      /*string out = integer.tostring(this.getx()) + integer.tostring(this.gety());     system.out.println(out); debug*/ }  //accessors, mutators public int getterrain() {return terrain;}  public int getx() {return xpos;} public int gety() {return ypos;} 

}

and finally, gamedata.java

public class gamedata{  //members private mapblock[][] map = null; private int mapwidth = 8; private int mapheight = 8;  //constructors public gamedata() {     map = new mapblock[mapwidth][mapheight];     for(int x = 0; x < mapwidth; x++)     {         for(int y = 0; y < mapheight; y++)         {             map[x][y] = new mapblock(x,y,1);         }     } }  //accessors, mutators public mapblock[][] getmap() {return map;}  public int getwidth() {return mapwidth;}  public int getheight() {return mapheight;}  } 

as said, problem top left mapblock object being drawn screen. have tested hardcore, seems components being added, , paintcomponent @ least invoked every one. here picture of output:

http://imgur.com/vxgaiel

please help!!

you're overriding getx , gety in mapblock being used layout manager position instances of component

public int getx() {    return xpos; }  public int gety() {    return ypos; } 

either remove them or rename methods.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -