java - move a ball one after another -


i writing code of game bean machine(galton box), mechanism 1 ball fall , move randomly until 1 of slots (randomly) , ball comes doing same thing , on, problem balls fall @ same time not want, want each ball fall when current 1 finishes travel..can helps me ! thanks..

here code:

import java.awt.*; import java.awt.event.*; import java.util.arraylist; import java.util.collection; import java.util.random; import java.util.scanner; import java.lang.iterable;  import javax.swing.*;   public class ballpanel extends jpanel  {  int x=1, y=1; collection <ball> ball; public static void main(string [] args){       // create list     swingutilities.invokelater(new runnable() {         public void run() {     jframe frame= new jframe("bean game");     frame.add(new ballpanel());     frame.setsize(300,500);     frame.setlocationrelativeto(null);     frame.setdefaultcloseoperation(jframe.exit_on_close);     frame.setvisible(true);         }     });     }  public ballpanel() {      jpanel panel = new jpanel();     ball=new arraylist<>();      for(int i=1;i<=4;i++){         ball.add(new ball(x,y));         x++;         y++;     }      timer timer = new timer(400, new actionlistener()     {          public void actionperformed(actionevent e) {             (ball b: ball) {                   b.move();repaint();             }          }     });       timer.start(); }  protected void paintcomponent(graphics g) {     super.paintcomponent(g);     int raduis=10;     int  i=0;     int width=(getwidth()/8);     int space=(getwidth()-8*30)/2;     g.setcolor(color.black);     for(i=0;i<=8;i++){         g.drawline(space+(30*i),getheight()-5*(raduis) ,space+(30*i) ,getheight() );         if(i!=0 && i!=8)             g.filloval(space+(30*i)-5, getheight()-5*(raduis)-5, 10, 10);         if(i==0){             g.drawline(space,getheight()-5*(raduis) , space+((8)*15)-(raduis/2)-15 , getheight()-15-(5*raduis)-(30*(8-2))-raduis/2);             g.drawline(space+((8)*15)-(raduis/2)-15 , getheight()-15-(5*raduis)-(30*(8-2))-raduis/2 , space+((8)*15)-(raduis/2)-15 , getheight()/2-8*8);//vertical line          }         if(i==8){             g.drawline(space+(30*i), getheight()-5*(raduis), space+((8)*15)+(raduis/2)+15, getheight()-15-(5*raduis)-(30*(8-2))-raduis/2);             g.drawline(space+((8)*15)+(raduis/2)+15 , getheight()-15-(5*raduis)-(30*(8-2))-raduis/2 , space+((8)*15)+(raduis/2)+15 , getheight()/2-8*8);//vertical line          }      }       int o=1;     for(i=1;i<=8-2;i++){         int k=2;         for(int j=i;j<=8-2;j++){             g.filloval(space+((i+k)*15)-raduis/2, getheight()-(5*raduis)-(30*o)-raduis/2, raduis, raduis);              k=k+2;             }o++;     }     (ball b : ball) {          b.draw(g);     } } public class ball {     random rand=new random();     private int n;     private int raduis = 10;     private int moveraduis=12;     private int l = 0;     private int r = 0;     private int d = 0;     int x, y;       public ball(int x, int y){         this.x=x;         this.y=y;     }        public void draw(graphics g) {         g.setcolor(color.red);         g.filloval(getwidth()/2 -raduis/2  + r + l, 0 + d , moveraduis, moveraduis);      }     public void move() {         n=1+rand.nextint(100);         if(n<=51){             d+=15;              if(d<=(getheight()-15-(5*raduis)-(30*(8-2))-raduis/2))                 l=0;             else if(d>getheight()-15-(5*raduis)-(30*(8-2))-raduis/2 && d<getheight()-5*(raduis))             {                 d+=15;                 l-=15;              }             else if(d>=getheight()-5*(raduis))             {                 if(d==31*15)d-=15;                 d=d;              }          }         else if (n>=51 && n<=100){             d+=15;             if(d<=getheight()-15-(5*raduis)-(30*(8-2))-raduis/2)                 r=0;             else if(d>getheight()-15-(5*raduis)-(30*(8-2))-raduis/2 && d<getheight()-5*(raduis))                 {                 d+=15;                 r+=15;             }             else if(d>=getheight()-5*(raduis)){                 if(d==31*15)d-=15;                 d=d;              }          }      } } } 

so, movement code moves balls on each iteration, need have "active" ball, example...

private ball activeball;  timer timer = new timer(400, new actionlistener() {      public void actionperformed(actionevent e) {         if (activeball != null && activeball.hascompleted()) {             activeball = null;         }         if (activeball == null && ball.size() > 0) {             activeball = ball.remove(0);         }         if (activeball != null) {             activeball.move();             repaint();         }     } }); 

you'll need add in kind of check determine ball has completed it's run though


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -