小编use*_*497的帖子

如何在Java中生成随机的正数和负数

我试图在原始数据类型short的范围(-32768,32767)上生成随机整数.java Random对象仅生成正数.如何在该间隔内随机创建数字?谢谢.

java random numbers

29
推荐指数
3
解决办法
10万
查看次数

2010年java并发修改异常崩溃

绘制存储在ArrayList中的一些粒子.这段代码工作正常:


 super.paintComponent(g);
             for (Particle b: particleArr){
                  g.setColor(b.getColor());
                  g.fillOval(b.getXCoor() + 5,b.getYCoor(),
                             b.getParticleSize(),b.getParticleSize());
             }
但是,此代码会引发并发修改异常:

        public void paintComponent(Graphics g){
            //paint particles
             super.paintComponent(g);
             for (Particle b: particleArr){
                  g.setColor(b.getColor());
                  if (b.isDead())
                      particleArr.remove(b);
                  else if (!b.isVanishing())
                      g.fillOval(b.getXCoor(),b.getYCoor(),
                            b.getParticleSize(),b.getParticleSize());
                  else {
                      g.fillOval(b.getXCoor() + 5,b.getYCoor(),
                             b.getParticleSize(),b.getParticleSize());
                      g.fillOval(b.getXCoor() - 5,b.getYCoor(),
                             b.getParticleSize(),b.getParticleSize());
                      g.fillOval(b.getXCoor(),b.getYCoor() + 5,
                             b.getParticleSize(),b.getParticleSize());
                      g.fillOval(b.getXCoor(),b.getYCoor() - 5,
                             b.getParticleSize(),b.getParticleSize());
                  }
             }
我很困惑.这是迭代器的乱码,运行缓慢.

            itr = particleArr.iterator();

         super.paintComponent(g);
         while (itr.hasNext()){
             particle=itr.next();
              g.setColor(particle.getColor());
              if (particle.isDead())
                  itr.remove();
              else if (particle.isVanishing())
                  g.fillOval(particle.getXCoor(),particle.getYCoor(),
                        particle.getParticleSize(),particle.getParticleSize());
              else {
                  g.fillOval(particle.getXCoor() + 5,particle.getYCoor(),
                         particle.getParticleSize(),particle.getParticleSize());
                  g.fillOval(particle.getXCoor() - 5,particle.getYCoor(),
                         particle.getParticleSize(),particle.getParticleSize()); …
Run Code Online (Sandbox Code Playgroud)
Run Code Online (Sandbox Code Playgroud)

java thread-safety

4
推荐指数
1
解决办法
685
查看次数

java swing计时器......感叹

我正在尝试使用摆动计时器来补偿帧速率不一致.我试过了:


class TimerListener implements ActionListener {
   public void actionPerformed(ActionEvent e){
         timeCur = System.currentTimeMillis();
         timer.setDelay(speed - (int)(timeCur - timePrev));
         repaint();
    }
}
...
public void paintComponent(Graphics g){...
        timePrev = System.currentTimeMillis();
}
 speed=30;
 private long timePrev=System.currentTimeMillis(), 
 timeCur=System.currentTimeMillis();
无济于事.如何解决这个问题?

java swing

1
推荐指数
1
解决办法
351
查看次数

标签 统计

java ×3

numbers ×1

random ×1

swing ×1

thread-safety ×1