I want to create a bunch of dots and move each of them in a random direction. Problem is that all dots are heading the same direction, so they look like a single dot even if there are a hundred of them. I tried to google it but found nothing. I bet it's just my stupidity, but I really don't know what to do with it. Thanks for helping me.
Dot class:
public class Dot extends JPanel {
private PVector pos;
private PVector vel;
private PVector acc;
private Brain brain;
private boolean dead = false;
public Dot(){
this.brain = new Brain(400);
this.pos = new PVector(Main.width/2, Main.height/2);
this.vel = new PVector(0, 0);
this.acc = new PVector(0, 0);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.fillOval((int)pos.x, (int)pos.y, 4, 4);
}
public void show(JFrame frame) {
frame.add(this);
}
public void move() {
if(brain.directions.length > brain.step) {
acc = brain.directions[brain.step];
brain.step++;
} else {
dead = true;
}
vel.add(acc);
vel.limit(5);
pos.add(vel);
}
public void update() {
if(!dead) {
move();
if(pos.y < 2 || pos.y < 2 || pos.x > Main.width - 2 || pos.y > Main.height - 2) {
dead = true;
}
}
}
}
Brain class:
public class Brain {
public PVector[] directions;
public int step = 0;
public Brain(int size) {
directions = new PVector[size];
randomize();
}
private void randomize() {
for(int i = 0; i < directions.length; i++) {
Random random = new Random();
float randomAngle = (float)(random.nextFloat() * 2 * Math.PI);
directions[i] = PVector.fromAngle(randomAngle);
}
}
}
Population class:
public class Population {
public Dot[] dots;
public Population(int size) {
dots = new Dot[size];
for(int i = 0; i < size; i++) {
dots[i] = new Dot();
}
}
public void show(JFrame frame) {
for(int i = 0; i < dots.length; i++) {
dots[i].show(frame);
}
}
public void update() {
for(int i = 0; i < dots.length; i++) {
dots[i].update();
}
}
}
Gui class:
public class Gui {
JFrame frame = new JFrame();
Population test = new Population(100);
public Gui() {
startup();
}
private void startup() {
frame.setSize(Main.width, Main.height);
frame.setLocation(200, 200);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
ScheduledExecutorService executor = Executors
.newSingleThreadScheduledExecutor();
executor.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
update();
}
}, 0, 1000 / 60, TimeUnit.MILLISECONDS);
test.show(frame);
}
private void update() {
frame.repaint();
test.update();
}
}
Aucun commentaire:
Enregistrer un commentaire