Java how to make a circle slowly move around? -


hello have app there circles floating around. @ moment don't float around, problem. want them move around in random directions. how can this? here circle class:

    public class data {      public int x,y, size,id;      public data(int x,int y){         this.x = x;         this.y = y;         size = new random().nextint(50);         id = new random().nextint(10);      }      public void tick(){     }      public void render(graphics g){         g.setcolor(new color(38,127,0));         g.filloval(x, y, size, size);         g.setcolor(color.black);         g.drawoval(x, y, size, size);      } } 

you can have random movement adding random value x , y every tick:

private random random = new random();  public void tick() {     x = x + random.nextfloat();     y = y + random.nextfloat(); } 

this result in fuzzy motion.

another option have 2 variables: motionx , motiony. added x , y every tick, after add random values motionx , motiony:

private random random = new random(); private float xmotion = 0f, ymotion = 0f; private float factor = 0.5f;    //just value reduce speed  private void tick() {     x = x + xmotion;     y = y + ymotion;     xmotion = xmotion + random.nextfloat() * factor;     ymotion = ymotion + random.nextfloat() * factor; } 

Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -