I am making Battleship in Java and I am trying to make a visual effect when a ship is destroyed. I am trying to make copies of a JLabel, then delete them after a short amount of time. I have the code for the method that creates the explosion here:
//"scheduler" is a single-thread ScheduledExecutorService that has already been declared in the class constructor method
//"layer" is a JLayeredPane, "explosion" is the JLabel I am duplicating
public synchronized void bigExplosion(Ship s) throws InterruptedException {
Random rand = new Random();
ArrayList<Coordinate> c = s.getCoordinates();
int x = rand.nextInt(((c.get(c.size()-1)).getX() - c.get(0).getX()) + 1) + c.get(0).getX();
int y = rand.nextInt(((c.get(c.size()-1)).getY() - c.get(0).getY()) + 1) + c.get(0).getY();
JLabel temp = new JLabel(explosion.getIcon());
temp.setBounds(x, y, 58, 58);
layer.add(temp,JLayeredPane.MODAL_LAYER);
playSound("Sounds/Explosion.wav");
scheduler.schedule(new Thread(() -> layer.remove(temp)), 1400, TimeUnit.MILLISECONDS);
}
The snippet of code that calls the method is here:
//"t" is a variable that has been already declared that is the index of which ship's coordinates the explosions will be at
for(int a = 0; a < 10; a++) {
scheduler.schedule(new Thread(()-> {
try {
bigExplosion(shipList[t]);
} catch (InterruptedException e) {
e.printStackTrace();
}
}), 100, TimeUnit.MILLISECONDS);
}
I want to duplicate the JLabel ever 100 milliseconds 10 times, then delete it after 1.4 seconds, but the code does not seem to be not doing anything. What am I doing wrong?