CS の講義で何かを試していたところ、突然、興味深い問題が発生しました。
これが私のメインコードです。
public void run(){
setSize(800, 600);
for(int i=0; i<= 30; i++){
elips el = new elips();
el.setFilled(true);
el.setColor(Color.RED);
elipsler.add(el);
add(el);
}
while(!stopthat){
for(int i=0; i< elipsler.size() -1; i++){
elipsler.get(i).cdRemove();
println("asd");
if(elipsler.get(i).canRemove == true){
remove(elipsler.get(i));
elipsler.remove(i);
elips el = new elips();
el.setFilled(true);
add(el);
elipsler.add(el);
}
}
}
}
それが私の楕円クラスです。
public class elips extends GOval{
static int x, y, w, h;
int cd;
public boolean canRemove = false;
Random rand = new Random();
public elips(){
super(x, y, w, h);
canRemove = false;
cd = rand.nextInt(100);
x = rand.nextInt(780) + 20;
y = rand.nextInt(580) + 20;
w = rand.nextInt(80) + 20;
h = rand.nextInt(80) + 20;
}
public void cdRemove(){
if(this.cd <= 0){
this.canRemove = true;
}else{
this.cd--;
}
}
}
ご覧のとおり、楕円を作成して「クールダウンを削除」し、クールダウンの終了後に楕円が破壊されます。問題は、println("asd") 行を削除すると、コードが正しく機能しないことです。つまり、その行を削除すると、楕円が同時に表示および非表示になります (クールダウンは機能しません)。
それで、「println」行がこの問題をどのように解決できるのだろうか?