Java “随机”在对象的所有实例之间共享

Java “随机”在对象的所有实例之间共享,java,random,Java,Random,我想创建一组点,并沿随机方向移动每个点。问题是所有的点都指向同一个方向,所以即使有一百个点,它们看起来也像一个点。我试着用谷歌搜索,但什么也没找到。我打赌这只是我的愚蠢,但我真的不知道该怎么办。 谢谢你帮助我 Dot类别: public class Dot extends JPanel { private PVector pos; private PVector vel; private PVector acc; private Brain b

我想创建一组点,并沿随机方向移动每个点。问题是所有的点都指向同一个方向,所以即使有一百个点,它们看起来也像一个点。我试着用谷歌搜索,但什么也没找到。我打赌这只是我的愚蠢,但我真的不知道该怎么办。 谢谢你帮助我

Dot类别:

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;
            }
        }
    }
}
每个“点”实例都是一个单独的面板。你有100个点,所以你有100个面板。但是:在JFrame上设置的布局管理器只显示一个点(可能是最后一个点)

也许有一种方法可以通过与布局管理器一起玩来实现这一点,使100个面板都相互重叠,但解决这一问题的“正确”(在我看来)方法是让人口扩展JPanel,而不是Dot。这样,总体是显示整个总体的单个组件

以下是您需要进行的更改:

//添加“扩展JPanel”
公共阶级人口扩展到JPanel{
//将“show”更改为“paintComponent”
@凌驾
公共组件(图形g){
超级组件(g);
对于(int i=0;i
this.pos=新的PVector(Main.width/2,Main.height/2)中的
Main.width
是什么?如果
Main.width
不变,则所有点将具有相同的
pos
字段。这就是问题所在吗?您对“Random”的使用有些不理想(最好创建一个Random实例并继续查询它),但是,尽管如此,这段代码并不会导致在您的rnd.nextFloat()调用中出现完全相同的随机数。这是另外一种东西。这是哪种版本的java?以前在循环中调用Random()构造函数时会出现问题,但问题已经解决。您确定
fromAngle
使用弧度,而不是度数吗?我认为部分问题在于Dot扩展了JPanel。为什么Dot扩展JPanel?
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);
        }
    }
}
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();
        }
    }
}
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();
    }
}