我试图在原始数据类型short的范围(-32768,32767)上生成随机整数.java Random对象仅生成正数.如何在该间隔内随机创建数字?谢谢.
绘制存储在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) 我正在尝试使用摆动计时器来补偿帧速率不一致.我试过了:
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();无济于事.如何解决这个问题?