java - Swing - Issue with lists not working inside paintComponent method -


i'm trying render positions of multiple fighters onscreen. relevant code follows:

public void run() {         double ns = 1000000000.0 / tps;         double delta = 0;          int frames = 0;         int updates = 0;          long lasttime = system.nanotime();         long timer = system.currenttimemillis();          while (running) {             long = system.nanotime();              delta += (now - lasttime) / ns;             lasttime = now;              while(delta >= 1) {                 update();                 updates++;                 delta--;             }              frame.getcontentpane().repaint();             frames++;              if(system.currenttimemillis() - timer >= 1000) {                 timer += 1000;                 frame.settitle(title + "  |  " + updates + " ups, " + frames + " fps");                 frames = 0;                 updates = 0;             }         }          stop();     }      private void update() {           if (math.random() < .1) {              fighter newfighter = new fighter();             fighterlist.add(newfighter);          }       }     public void paintcomponent(graphics g) {           super.paintcomponent(g);  // paint background           setbackground(color.blue);           system.out.println(fighterlist.size());           (int = 0; i<fighters; i++) {               system.out.println("attempted");               g.setcolor(color.green);               g.drawrect((int) fighterlist.get(i).xpos,                       (int) fighterlist.get(i).ypos,                       fighterlist.get(i).radius,                       fighterlist.get(i).radius);               system.out.println("rendered");           }         } public static void main(string[] args) {         game game = new game();         game.frame.setcontentpane(new game());         game.frame.setresizable(false);         game.frame.setdefaultcloseoperation(jframe.exit_on_close);         game.frame.setlocationrelativeto(null);          game.frame.setvisible(true);           game.start();       }  

issue is, nothing being drawn screen. additionally, running system.out.println(fighterlist.size()); gives different outputs based on it's run - when run inside paintcomponent returns zero, wheras when run inside update returns proper amount. issue scope, or there else i'm missing?

most synchronization issue. paintcomponent() method called edt (event dispatch thread) while run() method runs in own separate thread. update() gets called adds new fighters list.

you need proper synchronization both (or all) threads see same consistent data.

also since model (data) may modified during repaint, should "clone" model avoid inconsistent model being painted. or if don't want clone it, synchronize access model can't modified while painted.


Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -