java - Swing MapPanel updating Graphics -


trying create panel graphical map overview. map overview shows 10x10 tiles of complete map (so area). results in having 100 objects of maptile, each representing 1 tile in mappanel class. each maptile holds field object, displaying. field object saves lot of data if blocked, person standing on field, type field is, etc. (possible types grass, dirt, stone, etc.)

whenever changes need update mappanel shows actual situation , not 1 instantiated with. failing.

this have far:

public class maptile extends jpanel {     private bufferedimage backgroundimage;     private bufferedimage fighterimage;     private field field;      stuff...      public maptile(field field) throws midwargeneralexception {         loadbackgroundimage(field.gettype());         if(field.isoccupied()) {             loadfighterimage(field.getfighter().getteam());         }         settooltip(field);         this.field = field;     }      private void settooltip(field field) {         if(field.isoccupied()) {             settooltiptext(field.getx() + "/" + field.gety() + "\n"                     + "used by: " + field.getfighter().getname() + "(team: " + field.getfighter().getteam().getname()                     + ")\n" + "type: " + field.gettype().getname());         }         else {             settooltiptext(field.getx() + "/" + field.gety() + "\n" +                     "type: " + field.gettype().getname());         }     }      private void loadbackgroundimage(tiletype type) {         stuff...     }      private void loadfighterimage(team team) {        stuff ...     }      public void update(field field) {         this.field = field;         repaint();     }      @override     protected void paintcomponent(graphics graphics) {         super.paintcomponent(graphics);         graphics2d g2d = (graphics2d) graphics;         g2d.drawimage(backgroundimage, 0, 0, getwidth(), getheight(), this);         if(fighterimage != null) {             g2d.drawimage(fighterimage, 0, 0, getwidth(), getheight(), this);         }     } } 

both load() functions load appropriate image based upon current data current field object has. want is: maptile object has background (grass.png, stone.png, etc.) , if field occupied person draw person.jpg upon in team's color. work far, fail @ updating view.

the view needs update when:

  1. we change render area (the map example 200x200, render 10x10 area of it. can "walk" in each direction , render different region of map)
  2. a person moved (simply update old , new field)
  3. a person died (simply update old field)

this mappanel class:

public class mappanel extends jpanel {     private map map;     private int currentx;     private int currenty;     private list<maptile> tiles;      public mappanel(map map) {         this.map = map;         this.currentx = 0;         this.currenty = 0;         this.tiles = new arraylist<>();          init();     }      private int[] calcrenderarea() {         currentx = (currentx > map.getwidth()) ? map.getwidth() : currentx;         currenty = (currenty > map.getheight()) ? map.getheight() : currenty;          int difx = map.getwidth() - currentx;         int dify = map.getheight() - currenty;          currentx = (difx < 10) ? currentx - (10 - difx) : currentx;         currentx = (dify < 10) ? currenty - (10 - dify) : currenty;          return new int[]{currentx, currenty};     }      private void render() {         int[] offsets = calcrenderarea();         int tilecounter = 0;         for(int x = offsets[0]; x < 10; x++) {             for(int y = offsets[1]; y < 10; y++, tilecounter++) {                 tiles.get(tilecounter).update(map.getfield(x + offsets[0], y + offsets[1]));             }         }     }      private void init() {         setlayout(new gridlayout(10, 10));         for(int = 0; < 10; i++) {             for(int j = 0; j < 10; j++) {                 maptile tile = new maptile(map.getfield(i, j));                 tile.setborder(borderfactory.createlineborder(guiconfig.border_color));                 add(tile);                 tiles.add(tile);             }         }     }      public void toleft() {         currentx -= 1;         update();     }      public void toright() {         currentx += 1;         update();     }      public void totop() {         currenty -= 1;         update();     }      public void tobottom() {         currenty += 1;         update();     }      public void update() {         // todo :fix map panel updating         render();     } } 

so googled lot far , found few posts several images in jpanel none how update them... tried default repaint(); doesn't work. ideas? thanks!

it looks need improve maptile's update(field field) method such extracts information field object similar constructor does.

i.e.,

    public final void update(field field) {         // this.field = field;         loadbackgroundimage(field.gettype());         if(field.isoccupied()) {             loadfighterimage(field.getfighter().getteam());         }         settooltip(field);         this.field = field;         repaint();     } 

and

    public maptile(field field) throws midwargeneralexception {         updatefield(field);     } 

but again "side" recommendation, recommendation won't solve original problem solve future problems, recommend change program structure more of mvc (model-view-control) type structure, , have view -- gui above, listen changes model (the logical portion of program) , update when changes occur.


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -