I was trying something in my cs lecture then suddenly i have experienced an interesting problem.
this is my main code,
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);
}
}
}
}
and that's my ellipse class.
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--;
}
}
}
As you see, I am creating ellipses and giving them "remove cooldown" and after end of cooldown the ellipses destroy. The problem is if i remove println("asd") line, code does not work properly. That is, if i remove that line, the ellipses appear and disappear at the same time(cooldowns don't work).
so I wonder how "println" line can solve this problem?
Counting down from 100 to 0 is done in virtually zero time and that is essentially what you are doing before removing the ellipse. The reason you see the ellipse when adding the println, is because it takes some small amount of time to print. A hundred times, and you got yourself a couple of milliseconds worth of ellipse.
What you want to do is replace the raw count down with an actual timer of some kind. Stopwatch would do the job. (Apparantely DateTime, which I previously suggested, is not that accurate when down to a few milliseconds) I was thinking in C# this whole time. In java, using System.currentTimeMillis() is the way to go.
ninja: I'll provide code later today if you need it
EDIT: Right now, you are essentially doing this:
add ellipse
for(int i = 0; i < 100; i++)
{
// nothing happening in here, this entire loop takes zero time
}
remove ellipse
And with the println
:
add ellipse
for(int i = 0; i < 100; i++)
{
println("asd");
// something happening in here, this entire loop takes a little bit of time
}
remove ellipse
This kind of cooldown-system is going to be a problem in more than one way:
So, here's an option that does measure time:
public class elips extends GOval{
static int x, y, w, h;
int cd;
public boolean canRemove = false;
Random rand = new Random();
long timeStart;
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;
timeStart = System.currentTimeMillis();
}
public void cdRemove(){
if(System.currentTimeMillis() > timeStart + cd)
this.canRemove = true;
}
}
And:
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){
// now you can do stuff here that will not be delayed by the ellipse cooldowns
// neither will it become part of the ellipse cooldowns
for(int i=0; i< elipsler.size() -1; i++){
elipsler.get(i).cdRemove();
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);
}
}
}
}
This might be subject to a few changes, my proof read was a quick one.
EDIT 2: I was thinking in C# with the stopwatches and all, fixed that now.