Как удалить копии JLabel, которые я делаю после определенного количества времени? - PullRequest
0 голосов
/ 28 февраля 2020

Я делаю линкор в Java, и я пытаюсь создать визуальный эффект, когда корабль разрушен. Я пытаюсь сделать копии JLabel, а затем удалить их через короткое время. У меня есть код для метода, который создает взрыв здесь:

//"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);




}

Фрагмент кода, который вызывает метод здесь:

//"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);
                        }

Я хочу дублировать JLabel когда-либо 100 миллисекунды 10 раз, затем удалите его через 1,4 секунды, но код, похоже, ничего не делает. Что я делаю не так?

...