awt - How can I force a repaint() after every update() in a for loop in Java? -


i have written small, basic, kaleidoscope type program should gradually draw same pattern (over time) @ 6 different points , @ different orientations.

to have created array store each pixel's colour (it's initial colour being black , represented number 0) , 6 starting points in array have colour changed green (represented number 1). these points should appear on screen , then, based on previous 6 points' positions further 6 points created. updated screen should displayed. repeat, repeat, repeat...

my problem of updates, new pixels, being carried out before painting screen. have checked other posts , web tutorials etc, , gather awt kind enough avoid wasting time repainting minor changes. there seems called paintmanager involved in this. believe problem repainting within loop. finding frustrating as, in view, should simple thing do. there, indeed, simple way persuade java plot these minor changes in way desire?

i have included code in entirety below:

package paranoid;  import javax.swing.jframe;  public class masterframe {      public static void main(string[] args) {             // todo auto-generated method stub     new masterframe();     }       public masterframe(){         jframe f = new jframe();         f.settitle("kaleidoscope");         f.add(new trip2());         f.setsize(500,300);         f.setlocationrelativeto(null);         f.setvisible(true);         f.setresizable(false);         f.setdefaultcloseoperation(jframe.exit_on_close);      }  } 

and...

package paranoid;  import java.awt.color; import java.awt.graphics;  import javax.swing.jpanel;   public class trip2 extends jpanel {      private static final long serialversionuid = 1l;      private int xmin = 0,                 xmax = 499,                 ymin = 0,                 ymax = 279;     private int x = 120;     private int y = 80;     private int dx = 1;     private int dy = 1;     private int temp = 0;     private int update_counter = 0;     private int repaint_counter = 0;     private int x_pos[] = new int[6];     private int y_pos[] = new int[6];     private int screen[][] = new int[500][280];       public trip2() {          initialisation();         for(int = 0; < 5000; i++)         {             update();             system.out.println("just returned update()");              repaint();                                          //this repaint not being activated until updates              system.out.println("just returned paint()");   //have been completed, want repaint after every update.         }     }      public void initialisation(){         system.out.println("initialising...");         x_pos[0] = x;         y_pos[0] = y;          x_pos[1] = xmax - x;         y_pos[1] = y;          x_pos[2] = x;         y_pos[2] = ymax - y;          x_pos[3] = xmax - x;         y_pos[3] = ymax - y;          x_pos[4] = (int)(xmax/2)-50;         y_pos[4] = (int)(ymax/2);          x_pos[5] = (int)(xmax/2)+50;         y_pos[5] = (int)(ymax/2);          for(int j = 0; j<280; j++){             for(int = 0; i<500; i++){                 screen[i][j] = 0;             }         }     } //end of initialisation()      public void update(){         system.out.println("updating... "+update_counter+"th time");          temp = (int)math.floor(math.random()*100);         if(temp < 40){ // 40% chance direction changed             dx = (int)math.floor(math.random()*3);             dy = (int)math.floor(math.random()*3);             dx = dx - 1;             dy = dy - 1;         }           x_pos[0] = x_pos[0]+dx;         y_pos[0] = y_pos[0]+dy;          x_pos[1] = x_pos[1]-dx;         y_pos[1] = y_pos[1]+dy;          x_pos[2] = x_pos[2]+dx;         y_pos[2] = y_pos[2]-dy;          x_pos[3] = x_pos[3]-dx;         y_pos[3] = y_pos[3]-dy;          x_pos[4] = x_pos[4]-dy;         y_pos[4] = y_pos[4]-dx;          x_pos[5] = x_pos[5]+dy;         y_pos[5] = y_pos[5]+dx;          for(int k = 0; k < 6; k++){             if(x_pos[k] < 0){                 x_pos[k] = 0;             }             if(x_pos[k] > 499){                 x_pos[k] = 499;             }         }          for(int k = 0; k < 6; k++){             if(y_pos[k] < 0){                 y_pos[k] = 0;             }             if(y_pos[k] > 279){                 y_pos[k] = 279;             }         }           screen[x_pos[0]][y_pos[0]] = 1;         screen[x_pos[1]][y_pos[1]] = 1;         screen[x_pos[2]][y_pos[2]] = 1;         screen[x_pos[3]][y_pos[3]] = 1;         screen[x_pos[4]][y_pos[4]] = 1;         screen[x_pos[5]][y_pos[5]] = 1;          update_counter = update_counter + 1;      } //end of update()      public void paint(graphics g){         system.out.println("painting screen "+repaint_counter+"th time");              g.setcolor(color.black);             g.fillrect(xmin, ymin, xmax, ymax);              for(int j = 0; j<280; j++){                 for(int = 0; i<500; i++){                     if(screen[i][j] == 0){                         g.setcolor(color.black);                     } else                      {                            g.setcolor(color.green);                         }                     g.drawline(i,j,i,j); //plots pixel                 }             }               try{                 thread.sleep(100);             }             catch(interruptedexception e){               }              repaint_counter = repaint_counter + 1;       }//end of paint(graphics g)  }//end of trip2 class 

one of main issues here calling thread.sleep() in paint method - not idea halt further repainting of application period. (the event dispatch thread/painting thread must not use slow operations)

the usual flow want achieve here follows (poss way more detail need):

  1. create model class contains number variables , just numbers (no ui code) create various getters allow ui access these numbers later.
  2. allow model class passed view class (trip1 in case) , set instance variable.
  3. create new thread or timer main controlling class periodically adjusts model numbers/internals desire.
  4. create listener interface changes on model. (e.g. modelchangedlistener or such like)
  5. add in list of listeners model - register method add listener list.
  6. make such changes model i.e. when numbers updated, these fire calls registered listeners.
  7. in main controlling class, register listener model solely calls: trip2panel.repaint();

  8. in paint() method of panel... draw model stands.

full code posted:

package paranoid;  import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.jframe; import javax.swing.timer;  public class masterframe {  public static void main(string[] args) {         // todo auto-generated method stub new masterframe(); }   public masterframe(){     jframe f = new jframe();     f.settitle("kaleidoscope");     final trip2 trip2ui = new trip2();     final tripmodel model = new tripmodel();     model.update();     timer timer = new timer(1, new actionlistener() {         @override         public void actionperformed(actionevent e) {             model.update();         }     });     timer.setrepeats(true);     timer.start();      model.addlistener(new tripmodellistener() {         @override         public void modelchanged() {             trip2ui.repaint();         }     });     trip2ui.setmodel(model);      f.add(trip2ui);     f.setsize(500,300);     f.setlocationrelativeto(null);     f.setvisible(true);     f.setresizable(false);      f.setdefaultcloseoperation(jframe.exit_on_close);      } } 

tripmodellistener

package paranoid;  public interface tripmodellistener {     void modelchanged(); } 

trip2 (the ui)

package paranoid;  import java.awt.color; import java.awt.graphics;  import javax.swing.jpanel;   public class trip2 extends jpanel {  private static final long serialversionuid = 1l;  private tripmodel model;  public void paint(graphics g){      g.setcolor(color.black);     g.fillrect(model.getxmin(), model.getymin(), model.getxmax(), model.getymax());      (int j = 0; j < 280; j++) {         (int = 0; < 500; i++) {             if (model.getscreen()[i][j] == 0) {                 g.setcolor(color.black);             } else {                 g.setcolor(color.green);             }             g.drawline(i, j, i, j); //plots pixel         }     } }  public void setmodel(tripmodel model) {     this.model = model; }  }//en 

the trip model

package paranoid;  import java.awt.color; import java.awt.graphics; import java.util.list; import java.util.concurrent.copyonwritearraylist;  public class tripmodel { private list<tripmodellistener> listeners = new copyonwritearraylist<tripmodellistener>(); private int xmin = 0,             xmax = 499,             ymin = 0,             ymax = 279; private int x = 120; private int y = 80; private int dx = 1; private int dy = 1; private int temp = 0; private int update_counter = 0; private int x_pos[] = new int[6]; private int y_pos[] = new int[6]; private int screen[][] = new int[500][280];   public tripmodel() {      initialisation(); }  public void initialisation(){     system.out.println("initialising...");     x_pos[0] = x;     y_pos[0] = y;      x_pos[1] = xmax - x;     y_pos[1] = y;      x_pos[2] = x;     y_pos[2] = ymax - y;      x_pos[3] = xmax - x;     y_pos[3] = ymax - y;      x_pos[4] = (int)(xmax/2)-50;     y_pos[4] = (int)(ymax/2);      x_pos[5] = (int)(xmax/2)+50;     y_pos[5] = (int)(ymax/2);      for(int j = 0; j<280; j++){         for(int = 0; i<500; i++){             screen[i][j] = 0;         }     } } //end of initialisation()  public void update(){     //system.out.println("updating... "+update_counter+"th time");      temp = (int)math.floor(math.random()*100);     if(temp < 40){ // 40% chance direction changed         dx = (int)math.floor(math.random()*3);         dy = (int)math.floor(math.random()*3);         dx = dx - 1;         dy = dy - 1;     }       x_pos[0] = x_pos[0]+dx;     y_pos[0] = y_pos[0]+dy;      x_pos[1] = x_pos[1]-dx;     y_pos[1] = y_pos[1]+dy;      x_pos[2] = x_pos[2]+dx;     y_pos[2] = y_pos[2]-dy;      x_pos[3] = x_pos[3]-dx;     y_pos[3] = y_pos[3]-dy;      x_pos[4] = x_pos[4]-dy;     y_pos[4] = y_pos[4]-dx;      x_pos[5] = x_pos[5]+dy;     y_pos[5] = y_pos[5]+dx;      for(int k = 0; k < 6; k++){         if(x_pos[k] < 0){             x_pos[k] = 0;         }         if(x_pos[k] > 499){             x_pos[k] = 499;         }     }      for(int k = 0; k < 6; k++){         if(y_pos[k] < 0){             y_pos[k] = 0;         }         if(y_pos[k] > 279){             y_pos[k] = 279;         }     }       screen[x_pos[0]][y_pos[0]] = 1;     screen[x_pos[1]][y_pos[1]] = 1;     screen[x_pos[2]][y_pos[2]] = 1;     screen[x_pos[3]][y_pos[3]] = 1;     screen[x_pos[4]][y_pos[4]] = 1;     screen[x_pos[5]][y_pos[5]] = 1;      update_counter = update_counter + 1;     firemodelchangedlistener(); } //end of update()  private void firemodelchangedlistener() {     (tripmodellistener listener : listeners) {         listener.modelchanged();     } }   public int getxmin() {     return xmin; }  public int getymin() {     return ymin; }  public int getymin() {     return ymin; }  public void setymin(int ymin) {     this.ymin = ymin; }  public int getxmax() {     return xmax; }  public int getxmax() {     return xmax; }  public void setxmax(int xmax) {     this.xmax = xmax; }  public int getymax() {     return ymax; }  public int[][] getscreen() {     return screen; }  public void addlistener( tripmodellistener listener) {     listeners.add(listener); } } 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -